]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
(execute): Use player->comtail[1] instead of player->argp[1]. This
[empserver] / src / lib / player / player.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  player.c: Main command loop for a player
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <stdio.h>
38 #include "com.h"
39 #include "empio.h"
40 #include "empthread.h"
41 #include "file.h"
42 #include "journal.h"
43 #include "misc.h"
44 #include "nat.h"
45 #include "optlist.h"
46 #include "player.h"
47 #include "proto.h"
48 #include "prototypes.h"
49 #include "sect.h"
50 #include "tel.h"
51
52
53 static int command(void);
54 static int status(void);
55
56 struct player *player;
57
58 void
59 player_main(struct player *p)
60 {
61     struct natstr *natp;
62     int secs;
63     char buf[128];
64
65     p->state = PS_PLAYING;
66     player = p;
67     time(&player->lasttime);
68     time(&player->curup);
69     show_motd();
70     if (init_nats() < 0) {
71         pr("Server confused, try again later\n");
72         return;
73     }
74     natp = getnatp(player->cnum);
75     if (!gamehours(player->curup)) {
76         pr("Empire hours restriction in force\n");
77         if (natp->nat_stat != STAT_GOD)
78             return;
79     }
80     daychange(player->curup);
81     if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
82         pr("Time exceeded today\n");
83         return;
84     }
85     if (natp->nat_stat != STAT_VIS
86         && natp->nat_last_login
87         && (strcmp(natp->nat_hostaddr, player->hostaddr)
88             || strcmp(natp->nat_userid, player->userid))) {
89         pr("Last connection from: %s", ctime(&natp->nat_last_login));
90         pr("                  to: %s",
91            natp->nat_last_login <= natp->nat_last_logout
92            ? ctime(&natp->nat_last_logout) : "?");
93         pr("                  by: %s@%s\n",
94            natp->nat_userid,
95            *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
96     }
97     strcpy(natp->nat_userid, player->userid);
98     strcpy(natp->nat_hostname, player->hostname);
99     strcpy(natp->nat_hostaddr, player->hostaddr);
100
101     time(&natp->nat_last_login);
102     putnat(natp);
103     journal_login();
104     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
105         if (natp->nat_tgms == 1)
106             pr("You have a new telegram waiting ...\n");
107         else
108             pr("You have %s new telegrams waiting ...\n",
109                numstr(buf, natp->nat_tgms));
110         natp->nat_tgms = 0;
111     }
112
113     while (status()) {
114         if (command() == 0 && !player->aborted)
115             break;
116         player->aborted = 0;
117         empth_yield();
118     }
119     /* #*# I put the following line in to prevent server crash -KHS */
120     natp = getnatp(player->cnum);
121     /*
122      * randomly round up to the nearest minute,
123      * charging at least 15 seconds.
124      */
125     time(&natp->nat_last_logout);
126     secs = MAX(natp->nat_last_logout - player->lasttime, 15);
127     natp->nat_minused += secs / 60;
128     secs = secs % 60;
129     if (chance(secs / 60.0))
130         natp->nat_minused += 1;
131     putnat(natp);
132     pr("Bye-bye\n");
133     journal_logout();
134 }
135
136 static int
137 command(void)
138 {
139     char *redir;                /* UTF-8 */
140     char scanspace[1024];
141
142     if (getcommand(player->combuf) < 0)
143         return 0;
144     if (parse(player->combuf, scanspace, player->argp, player->comtail,
145               &player->condarg, &redir) < 0) {
146         pr("See \"info Syntax\"?\n");
147     } else {
148         if (dispatch(player->combuf, redir) < 0)
149             pr("Try \"list of commands\" or \"info\"\n");
150     }
151     return 1;
152 }
153
154 static int
155 status(void)
156 {
157     struct natstr *natp;
158     int minute;
159     struct sctstr sect;
160     char buf[128];
161
162     if (player->state == PS_SHUTDOWN)
163         return 0;
164     natp = getnatp(player->cnum);
165     if (io_error(player->iop) || io_eof(player->iop)) {
166         putnat(natp);
167         return 0;
168     }
169     if (player->dolcost != 0.0) {
170         if (player->dolcost > 100.0)
171             pr("That just cost you $%.2f\n", player->dolcost);
172         else if (player->dolcost < -100.0)
173             pr("You just made $%.2f\n", -player->dolcost);
174         if (natp->nat_money < player->dolcost && !player->broke) {
175             player->broke = 1;
176             player->nstat &= ~MONEY;
177             pr("You are now broke; industries are on strike.\n");
178         } else if (player->broke && natp->nat_money - player->dolcost > 0) {
179             player->broke = 0;
180             player->nstat |= MONEY;
181             pr("You are no longer broke!\n");
182         }
183         natp->nat_money -= roundavg(player->dolcost);
184         player->dolcost = 0.0;
185     } else {
186         if (natp->nat_money < 0.0 && !player->broke) {
187             player->broke = 1;
188             player->nstat &= ~MONEY;
189             pr("You are now broke; industries are on strike.\n");
190         }
191         if (player->broke && natp->nat_money > 0) {
192             player->broke = 0;
193             player->nstat |= MONEY;
194             pr("You are no longer broke!\n");
195         }
196     }
197     getsect(natp->nat_xcap, natp->nat_ycap, &sect);
198     if (influx(natp))
199         player->nstat &= ~CAP;
200     else
201         player->nstat |= CAP;
202     player->ncomstat = player->nstat;
203     if (player->god)
204         player->ncomstat |= CAP | MONEY;
205     time(&player->curup);
206     minute = (player->curup - player->lasttime) / 60;
207     if (minute > 0) {
208         player->minleft -= minute;
209         if (player->minleft <= 0) {
210             /*
211              * countdown timer "player->minleft" has expired.
212              * either day change, or hours restriction
213              */
214             daychange(player->curup);
215             if (!gamehours(player->curup)) {
216                 pr("Empire hours restriction in force\n");
217                 if (natp->nat_stat != STAT_GOD) {
218                     putnat(natp);
219                     return 0;
220                 }
221             }
222             player->minleft = getminleft(player->curup, m_m_p_d);
223         }
224         player->lasttime += minute * 60;
225         natp->nat_minused += minute;
226     }
227     if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
228         pr("Max minutes per day limit exceeded.\n");
229         player->ncomstat = VIS;
230     }
231     if (player->btused) {
232         natp->nat_btu -= player->btused;
233         player->btused = 0;
234     }
235     if (natp->nat_tgms > 0) {
236         if (!(natp->nat_flags & NF_INFORM)) {
237             if (natp->nat_tgms == 1)
238                 pr("You have a new telegram waiting ...\n");
239             else
240                 pr("You have %s new telegrams waiting ...\n",
241                    numstr(buf, natp->nat_tgms));
242             natp->nat_tgms = 0;
243         }
244     }
245     if (natp->nat_ann > 0) {
246         if (natp->nat_ann == 1)
247             pr("You have a new announcement waiting ...\n");
248         else
249             pr("You have %s new announcements waiting ...\n",
250                numstr(buf, natp->nat_ann));
251         natp->nat_ann = 0;
252     }
253     if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
254         pr("You lost your capital... better designate one\n");
255     putnat(natp);
256     if (gamedown() && !player->god) {
257         pr("gamedown\n");
258         return 0;
259     }
260     return 1;
261 }
262
263 /*
264  * actually a command; redirection and piping ignored.
265  * XXX This whole mess should be redone; execute block should
266  * start with "exec start", and should end with "exec end".
267  * We'll wait until 1.2 I guess.
268  */
269 int
270 execute(void)
271 {
272     char buf[1024];
273     int failed;
274     char *p;
275     char *redir;                /* UTF-8 */
276     char scanspace[1024];
277
278     failed = 0;
279     redir = NULL;
280
281     if (player->comtail[1])
282         p = player->comtail[1];
283     else
284         p = getstring("File? ", buf);
285     if (p == NULL || *p == '\0')
286         return RET_SYN;
287     prexec(p);
288
289     while (!failed && status()) {
290         if (recvclient(buf, sizeof(buf)) < 0)
291             break;
292         if (parse(buf, scanspace, player->argp, player->comtail,
293                   &player->condarg, &redir) < 0) {
294             failed = 1;
295             continue;
296         }
297         if (redir == NULL)
298             pr("\nExecute : %s\n", buf);
299         if (dispatch(buf, redir) < 0)
300             failed = 1;
301     }
302     if (failed) {
303         while (recvclient(buf, sizeof(buf)) >= 0) ;
304     }
305     if (redir == NULL)
306         pr("Execute : %s\n", failed ? "aborted" : "terminated");
307     player->eof = 0;
308     return RET_OK;
309 }
310
311 int
312 show_motd(void)
313 {
314     FILE *motd_fp;
315     struct telstr tgm;
316     char buf[MAXTELSIZE + 1];   /* UTF-8 */
317
318     if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
319         if (errno == ENOENT)
320             return RET_OK;
321         else {
322             pr ("Could not open motd.\n");
323             logerror("Could not open motd (%s).\n", motdfil);
324             return RET_SYS;
325         }
326     }
327     if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
328         logerror("bad header on login message (motdfil)");
329         fclose(motd_fp);
330         return RET_FAIL;
331     }
332     if (tgm.tel_length >= (long)sizeof(buf)) {
333         logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
334         fclose(motd_fp);
335         return RET_FAIL;
336     }
337     if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
338         logerror("bad length %ld on login message", tgm.tel_length);
339         fclose(motd_fp);
340         return RET_FAIL;
341     }
342     buf[tgm.tel_length] = 0;
343     uprnf(buf);
344     fclose(motd_fp);
345     return RET_OK;
346 }
347
348 int
349 quit(void)
350 {
351     player->state = PS_SHUTDOWN;
352     return RET_OK;
353 }
354
355 char *
356 praddr(struct player *p)
357 {
358     return prbuf("%s@%s", p->userid,
359                  *p->hostname ? p->hostname : p->hostaddr);
360 }