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