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