]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
(status): Round money before checking solvency, and consistently treat
[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 > 100.0)
170         pr("That just cost you $%.2f\n", player->dolcost);
171     else if (player->dolcost < -100.0)
172         pr("You just made $%.2f\n", -player->dolcost);
173     natp->nat_money -= roundavg(player->dolcost);
174     player->dolcost = 0.0;
175     if (natp->nat_money < 0 && !player->broke) {
176         player->broke = 1;
177         player->nstat &= ~MONEY;
178         pr("You are now broke; industries are on strike.\n");
179     } else if (player->broke && natp->nat_money >= 0) {
180         player->broke = 0;
181         player->nstat |= MONEY;
182         pr("You are no longer broke!\n");
183     }
184     getsect(natp->nat_xcap, natp->nat_ycap, &sect);
185     if (influx(natp))
186         player->nstat &= ~CAP;
187     else
188         player->nstat |= CAP;
189     player->ncomstat = player->nstat;
190     if (player->god)
191         player->ncomstat |= CAP | MONEY;
192     time(&player->curup);
193     minute = (player->curup - player->lasttime) / 60;
194     if (minute > 0) {
195         player->minleft -= minute;
196         if (player->minleft <= 0) {
197             /*
198              * countdown timer "player->minleft" has expired.
199              * either day change, or hours restriction
200              */
201             daychange(player->curup);
202             if (!gamehours(player->curup)) {
203                 pr("Empire hours restriction in force\n");
204                 if (natp->nat_stat != STAT_GOD) {
205                     putnat(natp);
206                     return 0;
207                 }
208             }
209             player->minleft = getminleft(player->curup, m_m_p_d);
210         }
211         player->lasttime += minute * 60;
212         natp->nat_minused += minute;
213     }
214     if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
215         pr("Max minutes per day limit exceeded.\n");
216         player->ncomstat = VIS;
217     }
218     if (player->btused) {
219         natp->nat_btu -= player->btused;
220         player->btused = 0;
221     }
222     if (natp->nat_tgms > 0) {
223         if (!(natp->nat_flags & NF_INFORM)) {
224             if (natp->nat_tgms == 1)
225                 pr("You have a new telegram waiting ...\n");
226             else
227                 pr("You have %s new telegrams waiting ...\n",
228                    numstr(buf, natp->nat_tgms));
229             natp->nat_tgms = 0;
230         }
231     }
232     if (natp->nat_ann > 0) {
233         if (natp->nat_ann == 1)
234             pr("You have a new announcement waiting ...\n");
235         else
236             pr("You have %s new announcements waiting ...\n",
237                numstr(buf, natp->nat_ann));
238         natp->nat_ann = 0;
239     }
240     if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
241         pr("You lost your capital... better designate one\n");
242     putnat(natp);
243     if (gamedown() && !player->god) {
244         pr("gamedown\n");
245         return 0;
246     }
247     return 1;
248 }
249
250 /*
251  * actually a command; redirection and piping ignored.
252  * XXX This whole mess should be redone; execute block should
253  * start with "exec start", and should end with "exec end".
254  * We'll wait until 1.2 I guess.
255  */
256 int
257 execute(void)
258 {
259     char buf[1024];
260     int failed;
261     char *p;
262     char *redir;                /* UTF-8 */
263     char scanspace[1024];
264
265     failed = 0;
266     redir = NULL;
267
268     if (player->comtail[1])
269         p = player->comtail[1];
270     else
271         p = getstring("File? ", buf);
272     if (p == NULL || *p == '\0')
273         return RET_SYN;
274     prexec(p);
275
276     while (!failed && status()) {
277         if (recvclient(buf, sizeof(buf)) < 0)
278             break;
279         if (parse(buf, scanspace, player->argp, player->comtail,
280                   &player->condarg, &redir) < 0) {
281             failed = 1;
282             continue;
283         }
284         if (redir == NULL)
285             pr("\nExecute : %s\n", buf);
286         if (dispatch(buf, redir) < 0)
287             failed = 1;
288     }
289     if (failed) {
290         while (recvclient(buf, sizeof(buf)) >= 0) ;
291     }
292     if (redir == NULL)
293         pr("Execute : %s\n", failed ? "aborted" : "terminated");
294     player->eof = 0;
295     return RET_OK;
296 }
297
298 int
299 show_motd(void)
300 {
301     FILE *motd_fp;
302     struct telstr tgm;
303     char buf[MAXTELSIZE + 1];   /* UTF-8 */
304
305     if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
306         if (errno == ENOENT)
307             return RET_OK;
308         else {
309             pr ("Could not open motd.\n");
310             logerror("Could not open motd (%s).\n", motdfil);
311             return RET_SYS;
312         }
313     }
314     if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
315         logerror("bad header on login message (motdfil)");
316         fclose(motd_fp);
317         return RET_FAIL;
318     }
319     if (tgm.tel_length >= (long)sizeof(buf)) {
320         logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
321         fclose(motd_fp);
322         return RET_FAIL;
323     }
324     if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
325         logerror("bad length %ld on login message", tgm.tel_length);
326         fclose(motd_fp);
327         return RET_FAIL;
328     }
329     buf[tgm.tel_length] = 0;
330     uprnf(buf);
331     fclose(motd_fp);
332     return RET_OK;
333 }
334
335 int
336 quit(void)
337 {
338     player->state = PS_SHUTDOWN;
339     return RET_OK;
340 }
341
342 char *
343 praddr(struct player *p)
344 {
345     return prbuf("%s@%s", p->userid,
346                  *p->hostname ? p->hostname : p->hostaddr);
347 }