]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
(command): Remove unused local variable i.
[empserver] / src / lib / player / player.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 "prototypes.h"
37 #include <string.h>
38 #include "misc.h"
39 #include "player.h"
40 #include "proto.h"
41 #include "com.h"
42 #include "nat.h"
43 #include "sect.h"
44 #include "file.h"
45 #include "proto.h"
46 #include "empio.h"
47 #include "empthread.h"
48 #include "tel.h"
49 #include "gen.h"
50 #include "subs.h"
51 #include "common.h"
52 #include "optlist.h"
53
54 #if !defined(_WIN32)
55 #include <unistd.h>
56 #include <sys/time.h>
57 #endif
58 #include <stdio.h>
59 #include <errno.h>
60 #include <fcntl.h>
61
62 static int command(void);
63 static int status(void);
64
65 struct player *player;
66
67 void
68 player_main(struct player *p)
69 {
70     struct natstr *natp;
71     int secs;
72     char buf[128];
73
74     p->state = PS_PLAYING;
75     player = p;
76     time(&player->lasttime);
77     time(&player->curup);
78     show_motd();
79     if (init_nats() < 0) {
80         pr("Server confused, try again later\n");
81         return;
82     }
83     natp = getnatp(player->cnum);
84     if (!gamehours(player->curup)) {
85         pr("Empire hours restriction in force\n");
86         if (natp->nat_stat != STAT_GOD)
87             return;
88     }
89     daychange(player->curup);
90     if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
91         pr("Time exceeded today\n");
92         return;
93     }
94     if (natp->nat_stat != STAT_VIS
95         && natp->nat_last_login
96         && (strcmp(natp->nat_hostaddr, player->hostaddr)
97             || strcmp(natp->nat_userid, player->userid))) {
98         pr("Last connection from: %s", ctime(&natp->nat_last_login));
99         pr("                  to: %s",
100            natp->nat_last_login <= natp->nat_last_logout
101            ? ctime(&natp->nat_last_logout) : "?");
102         pr("                  by: %s@%s\n",
103            natp->nat_userid,
104            *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
105     }
106     strcpy(natp->nat_userid, player->userid);
107     strcpy(natp->nat_hostname, player->hostname);
108     strcpy(natp->nat_hostaddr, player->hostaddr);
109
110     time(&natp->nat_last_login);
111     putnat(natp);
112     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
113         if (natp->nat_tgms == 1)
114             pr("You have a new telegram waiting ...\n");
115         else
116             pr("You have %s new telegrams waiting ...\n",
117                numstr(buf, natp->nat_tgms));
118         natp->nat_tgms = 0;
119     }
120
121     while (status()) {
122         if (command() == 0 && !player->aborted)
123             break;
124         player->aborted = 0;
125         empth_yield();
126     }
127     /* #*# I put the following line in to prevent server crash -KHS */
128     natp = getnatp(player->cnum);
129     /*
130      * randomly round up to the nearest minute,
131      * charging at least 15 seconds.
132      */
133     time(&natp->nat_last_logout);
134     secs = MAX(natp->nat_last_logout - player->lasttime, 15);
135     natp->nat_minused += secs / 60;
136     secs = secs % 60;
137     if (chance(secs / 60.0))
138         natp->nat_minused += 1;
139     putnat(natp);
140     pr("Bye-bye\n");
141 }
142
143 static int
144 command(void)
145 {
146     char *redir;                /* UTF-8 */
147     char scanspace[1024];
148
149     if (getcommand(player->combuf) < 0)
150         return 0;
151     if (parse(player->combuf, player->argp, &player->condarg,
152               scanspace, &redir) < 0) {
153         pr("See \"info Syntax\"?\n");
154     } else {
155         if (dispatch(player->combuf, redir) < 0)
156             pr("Try \"list of commands\" or \"info\"\n");
157     }
158     return 1;
159 }
160
161 static int
162 status(void)
163 {
164     struct natstr *natp;
165     int minute;
166     struct sctstr sect;
167     char buf[128];
168
169     if (player->state == PS_SHUTDOWN)
170         return 0;
171     natp = getnatp(player->cnum);
172     if (io_error(player->iop) || io_eof(player->iop)) {
173         putnat(natp);
174         return 0;
175     }
176     player->visitor = natp->nat_stat < STAT_SANCT;
177     if (player->dolcost != 0.0) {
178         if (player->dolcost > 100.0)
179             pr("That just cost you $%.2f\n", player->dolcost);
180         else if (player->dolcost < -100.0)
181             pr("You just made $%.2f\n", -player->dolcost);
182         if (natp->nat_money < player->dolcost && !player->broke) {
183             player->broke = 1;
184             player->nstat &= ~MONEY;
185             pr("You are now broke; industries are on strike.\n");
186         } else if (player->broke && natp->nat_money - player->dolcost > 0) {
187             player->broke = 0;
188             player->nstat |= MONEY;
189             pr("You are no longer broke!\n");
190         }
191         natp->nat_money -= roundavg(player->dolcost);
192         player->dolcost = 0.0;
193     } else {
194         if (natp->nat_money < 0.0 && !player->broke) {
195             player->broke = 1;
196             player->nstat &= ~MONEY;
197             pr("You are now broke; industries are on strike.\n");
198         }
199         if (player->broke && natp->nat_money > 0) {
200             player->broke = 0;
201             player->nstat |= MONEY;
202             pr("You are no longer broke!\n");
203         }
204     }
205     getsect(natp->nat_xcap, natp->nat_ycap, &sect);
206     if (influx(natp))
207         player->nstat &= ~CAP;
208     else
209         player->nstat |= CAP;
210     player->ncomstat = player->nstat;
211     if (player->god)
212         player->ncomstat |= CAP | MONEY;
213     time(&player->curup);
214     minute = (player->curup - player->lasttime) / 60;
215     if (minute > 0) {
216         player->minleft -= minute;
217         if (player->minleft <= 0) {
218             /*
219              * countdown timer "player->minleft" has expired.
220              * either day change, or hours restriction
221              */
222             daychange(player->curup);
223             if (!gamehours(player->curup)) {
224                 pr("Empire hours restriction in force\n");
225                 if (natp->nat_stat != STAT_GOD) {
226                     putnat(natp);
227                     return 0;
228                 }
229             }
230             player->minleft = getminleft(player->curup, m_m_p_d);
231         }
232         player->lasttime += minute * 60;
233         natp->nat_minused += minute;
234     }
235     if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
236         pr("Max minutes per day limit exceeded.\n");
237         player->ncomstat = VIS;
238     }
239     if (player->btused) {
240         natp->nat_btu -= player->btused;
241         player->btused = 0;
242     }
243     if (natp->nat_tgms > 0) {
244         if (!(natp->nat_flags & NF_INFORM)) {
245             if (natp->nat_tgms == 1)
246                 pr("You have a new telegram waiting ...\n");
247             else
248                 pr("You have %s new telegrams waiting ...\n",
249                    numstr(buf, natp->nat_tgms));
250             natp->nat_tgms = 0;
251         }
252     }
253     if (natp->nat_ann > 0) {
254         if (natp->nat_ann == 1)
255             pr("You have a new announcement waiting ...\n");
256         else
257             pr("You have %s new announcements waiting ...\n",
258                numstr(buf, natp->nat_ann));
259         natp->nat_ann = 0;
260     }
261     if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
262         pr("You lost your capital... better designate one\n");
263     putnat(natp);
264     if (gamedown() && !player->god) {
265         pr("gamedown\n");
266         return 0;
267     }
268     return 1;
269 }
270
271 /*
272  * actually a command; redirection and piping ignored.
273  * XXX This whole mess should be redone; execute block should
274  * start with "exec start", and should end with "exec end".
275  * We'll wait until 1.2 I guess.
276  */
277 int
278 execute(void)
279 {
280     char buf[1024];
281     int failed;
282     char *p;
283     char *redir;                /* UTF-8 */
284     char scanspace[1024];
285
286     failed = 0;
287     redir = NULL;
288
289     p = getstarg(player->argp[1], "File? ", buf);
290
291     if (p == NULL || *p == '\0')
292         return RET_SYN;
293
294     /* FIXME should use raw argument here, to support UTF-8 file names */
295     prexec(player->argp[1]);
296
297     while (!failed && status()) {
298         if (recvclient(buf, sizeof(buf)) < 0)
299             break;
300         if (parse(buf, player->argp, &player->condarg,
301                   scanspace, &redir) < 0) {
302             failed = 1;
303             continue;
304         }
305         if (redir == NULL)
306             pr("\nExecute : %s\n", buf);
307         if (dispatch(buf, redir) < 0)
308             failed = 1;
309     }
310     if (failed) {
311         while (recvclient(buf, sizeof(buf)) >= 0) ;
312     }
313     if (redir == NULL)
314         pr("Execute : %s\n", failed ? "aborted" : "terminated");
315     return RET_OK;
316 }
317
318 int
319 show_motd(void)
320 {
321     FILE *motd_fp;
322     struct telstr tgm;
323     char buf[MAXTELSIZE + 1];   /* UTF-8 */
324
325     if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
326         if (errno == ENOENT)
327             return RET_OK;
328         else {
329             pr ("Could not open motd.\n");
330             logerror("Could not open motd (%s).\n", motdfil);
331             return RET_SYS;
332         }
333     }
334     if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
335         logerror("bad header on login message (motdfil)");
336         fclose(motd_fp);
337         return RET_FAIL;
338     }
339     if (tgm.tel_length >= (long)sizeof(buf)) {
340         logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
341         fclose(motd_fp);
342         return RET_FAIL;
343     }
344     if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
345         logerror("bad length %ld on login message", tgm.tel_length);
346         fclose(motd_fp);
347         return RET_FAIL;
348     }
349     buf[tgm.tel_length] = 0;
350     uprnf(buf);
351     fclose(motd_fp);
352     return RET_OK;
353 }
354
355 int
356 quit(void)
357 {
358     player->state = PS_SHUTDOWN;
359     return RET_OK;
360 }
361
362 char *
363 praddr(struct player *p)
364 {
365     return prbuf("%s@%s", p->userid,
366                  *p->hostname ? p->hostname : p->hostaddr);
367 }