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