]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
deity.h is redundant, remove it.
[empserver] / src / lib / player / player.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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
35 #include "prototypes.h"
36 #include <string.h>
37 #include "gamesdef.h"
38 #include "misc.h"
39 #include "player.h"
40 #include "proto.h"
41 #include "var.h"
42 #include "com.h"
43 #include "nat.h"
44 #include "sect.h"
45 #include "file.h"
46 #include "proto.h"
47 #include "empio.h"
48 #include "empthread.h"
49 #include "tel.h"
50 #include "gen.h"
51 #include "subs.h"
52 #include "common.h"
53 #include "optlist.h"
54
55 #if !defined(_WIN32)
56 #include <unistd.h>
57 #include <sys/time.h>
58 #endif
59 #include <stdio.h>
60 #include <fcntl.h>
61
62 struct player *player;
63
64 void
65 player_main(struct player *p)
66 {
67     struct natstr *natp;
68     int hour[2];
69     int secs;
70     s_char buf[128];
71
72     p->state = PS_PLAYING;
73     player = p;
74     time(&player->lasttime);
75     (void)time(&player->curup);
76     showvers(CLIENTPROTO);
77     show_motd();
78     if (init_nats() < 0)
79         return;
80     natp = getnatp(player->cnum);
81     if (player->god && !match_user(authfil, player)) {
82         logerror("NON-AUTHed Login attempted by %s", praddr(player));
83         pr("You're not a deity!\n");
84         return;
85     }
86     if (!gamehours(player->curup, hour)) {
87         pr("Empire hours restriction in force\n");
88         if ((natp->nat_stat & STAT_GOD) == 0)
89             return;
90     }
91     daychange(player->curup);
92     if ((player->minleft = getminleft(player->curup, hour, &m_m_p_d)) <= 0) {
93         pr("Time exceeded today\n");
94         return;
95     }
96     if ((*natp->nat_hostaddr &&
97          *player->hostaddr &&
98          strcmp(natp->nat_hostaddr, player->hostaddr)) ||
99         (*natp->nat_userid &&
100          *player->userid && strcmp(natp->nat_userid, player->userid))) {
101         if (natp->nat_stat != VIS) {
102             pr("Last connection from: %s", ctime(&natp->nat_last_login));
103             pr("                  to: %s", natp->nat_last_login <
104                natp->nat_last_logout ? ctime(&natp->
105                                              nat_last_logout) : "?");
106             pr("                  by: %s@%s\n",
107                *natp->nat_userid ? natp->nat_userid : (s_char *)"nobody",
108                *natp->nat_hostname ? natp->nat_hostname : *natp->
109                nat_hostaddr ? natp->nat_hostaddr : (s_char *)"nowhere");
110         }
111     }
112     if (*player->userid)
113         strcpy(natp->nat_userid, player->userid);
114     else
115         strcpy(natp->nat_userid, "nobody");
116
117     if (*player->hostname)
118         strcpy(natp->nat_hostname, player->hostname);
119     else
120         strcpy(natp->nat_hostname, "nowhere");
121
122     if (*player->hostaddr)
123         strcpy(natp->nat_hostaddr, player->hostaddr);
124
125     time(&natp->nat_last_login);
126     natp->nat_connected = 1;
127     putnat(natp);
128     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
129         if (natp->nat_tgms == 1)
130             pr("You have a new telegram waiting ...\n");
131         else
132             pr("You have %s new telegrams waiting ...\n",
133                numstr(buf, natp->nat_tgms));
134         natp->nat_tgms = 0;
135     }
136
137     while (status()) {
138         if (command() == 0 && !player->aborted)
139             break;
140         player->aborted = 0;
141     }
142     /* #*# I put the following line in to prevent server crash -KHS */
143     natp = getnatp(player->cnum);
144     /*
145      * randomly round up to the nearest minute,
146      * charging at least 15 seconds.
147      */
148     time(&natp->nat_last_logout);
149     secs = max(natp->nat_last_logout - player->lasttime, 15);
150     natp->nat_minused += secs / 60;
151     secs = secs % 60;
152     if (chance(secs / 60.0))
153         natp->nat_minused += 1;
154     natp->nat_connected = 0;
155     putnat(natp);
156     pr("Bye-bye\n");
157 }
158
159 int
160 command(void)
161 {
162     register unsigned int x;
163     s_char *redir;
164     s_char scanspace[1024];
165
166     if (getcommand(player->combuf) < 0)
167         return 0;
168     if (parse(player->combuf, player->argp, &player->condarg,
169               scanspace, &redir) < 0) {
170         pr("See \"info Syntax\"?\n");
171     } else {
172         /* XXX don't use alarm; use a scavenger thread */
173         /* DONT USE IT!!!! alarm and sleep may and dont work
174            together -- Sasha */
175         /* alarm((unsigned int)60*60); 1 hour */
176         if (player->condarg != (s_char *)0)
177             for (x = 0; x < strlen(player->condarg); x++)
178                 if (isupper(*(player->condarg + x)))
179                     *(player->condarg + x) =
180                         tolower(*(player->condarg + x));
181         if (dispatch(player->combuf, redir) < 0)
182             pr("Try \"list of commands\" or \"info\"\n");
183     }
184     return 1;
185 }
186
187 int
188 status(void)
189 {
190     struct natstr *natp;
191     int minute;
192     struct sctstr sect;
193     int hour[2];
194     s_char buf[128];
195
196     if (player->state == PS_SHUTDOWN)
197         return 0;
198     natp = getnatp(player->cnum);
199     if (io_error(player->iop) || io_eof(player->iop)) {
200         putnat(natp);
201         return 0;
202     }
203     player->visitor = (natp->nat_stat & (STAT_NORM | STAT_GOD)) == 0;
204     if (player->dolcost != 0.0) {
205         if (player->dolcost > 100.0)
206             pr("That just cost you $%.2f\n", player->dolcost);
207         else if (player->dolcost < -100.0)
208             pr("You just made $%.2f\n", -player->dolcost);
209         if (natp->nat_money < player->dolcost && !player->broke) {
210             player->broke = 1;
211             player->nstat &= ~MONEY;
212             pr("You are now broke; industries are on strike.\n");
213         } else if (player->broke && natp->nat_money - player->dolcost > 0) {
214             player->broke = 0;
215             player->nstat |= MONEY;
216             pr("You are no longer broke!\n");
217         }
218         natp->nat_money -= roundavg(player->dolcost);
219         player->dolcost = 0.0;
220     } else {
221         if (natp->nat_money < 0.0 && !player->broke) {
222             player->broke = 1;
223             player->nstat &= ~MONEY;
224             pr("You are now broke; industries are on strike.\n");
225         }
226         if (player->broke && natp->nat_money > 0) {
227             player->broke = 0;
228             player->nstat |= MONEY;
229             pr("You are no longer broke!\n");
230         }
231     }
232     getsect(natp->nat_xcap, natp->nat_ycap, &sect);
233     if ((sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT ||
234          sect.sct_type == SCT_SANCT) && sect.sct_own == player->cnum)
235         player->nstat |= CAP;
236     else
237         player->nstat &= ~CAP;
238     /* Ok, has the country owner reset his capital yet after it was sacked? */
239     if (natp->nat_flags & NF_SACKED)
240         player->nstat &= ~CAP;  /* No capital yet */
241     player->ncomstat = player->nstat;
242     (void)time(&player->curup);
243     minute = (player->curup - player->lasttime) / 60;
244     if (minute > 0) {
245         player->minleft -= minute;
246         if (player->minleft <= 0) {
247             /*
248              * countdown timer "player->minleft" has expired.
249              * either day change, or hours restriction
250              */
251             daychange(player->curup);
252             if (!gamehours(player->curup, hour)) {
253                 pr("Empire hours restriction in force\n");
254                 if ((natp->nat_stat & STAT_GOD) == 0) {
255                     putnat(natp);
256                     return 0;
257                 }
258             }
259             player->minleft = getminleft(player->curup, hour, &m_m_p_d);
260         }
261         player->lasttime += minute * 60;
262         natp->nat_minused += minute;
263     }
264     if ((player->nstat & NORM) && natp->nat_minused > m_m_p_d) {
265         pr("Max minutes per day limit exceeded.\n");
266         player->ncomstat = VIS;
267     }
268     if (player->btused) {
269         natp->nat_btu -= player->btused;
270         player->btused = 0;
271     }
272     if (natp->nat_tgms > 0) {
273         if (!(natp->nat_flags & NF_INFORM)) {
274             if (natp->nat_tgms == 1)
275                 pr("You have a new telegram waiting ...\n");
276             else
277                 pr("You have %s new telegrams waiting ...\n",
278                    numstr(buf, natp->nat_tgms));
279             natp->nat_tgms = 0;
280         }
281     }
282     if (natp->nat_ann > 0) {
283         if (natp->nat_ann == 1)
284             pr("You have a new announcement waiting ...\n");
285         else
286             pr("You have %s new announcements waiting ...\n",
287                numstr(buf, natp->nat_ann));
288         natp->nat_ann = 0;
289     }
290     if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
291         pr("You lost your capital... better designate one\n");
292     putnat(natp);
293     if (gamedown() && !player->god) {
294         pr("gamedown\n");
295         return 0;
296     }
297     return 1;
298 }
299
300 /*
301  * actually a command; redirection and piping ignored.
302  * XXX This whole mess should be redone; execute block should
303  * start with "exec start", and should end with "exec end".
304  * We'll wait until 1.2 I guess.
305  */
306 int
307 execute(void)
308 {
309     s_char buf[512];
310     int abort;
311     s_char *p;
312     s_char *redir;
313     s_char scanspace[1024];
314
315     abort = 0;
316     redir = 0;
317
318     p = getstarg(player->argp[1], "File? ", buf);
319
320     if (p == (s_char *)0 || p == '\0')
321         return RET_SYN;
322
323     prexec(player->argp[1]);
324     while (!abort && status()) {
325         if (recvclient(buf, sizeof(buf)) < 0)
326             break;
327         if (parse(buf, player->argp, &player->condarg,
328                   scanspace, &redir) < 0) {
329             abort = 1;
330             continue;
331         }
332         if (redir == 0)
333             pr("\nExecute : %s\n", buf);
334         if (dispatch(buf, redir) < 0)
335             abort = 1;
336     }
337     if (abort) {
338         while (recvclient(buf, sizeof(buf)) >= 0) ;
339     }
340     if (redir == 0)
341         pr("Execute : %s\n", abort ? "aborted" : "terminated");
342     return RET_OK;
343 }
344
345 int
346 show_motd(void)
347 {
348     int upf;
349     struct telstr tgm;
350     s_char buf[MAXTELSIZE];
351
352 #if !defined(_WIN32)
353     if ((upf = open(upfil, O_RDONLY, 0)) < 0)
354 #else
355     if ((upf = open(upfil, O_RDONLY | O_BINARY, 0)) < 0)
356 #endif
357         return RET_FAIL;
358     if (read(upf, (s_char *)&tgm, sizeof(tgm)) != sizeof(tgm)) {
359         logerror("bad header on login message (upfil)");
360         close(upf);
361         return RET_FAIL;
362     }
363     if (read(upf, buf, tgm.tel_length) != tgm.tel_length) {
364         logerror("bad length %d on login message", tgm.tel_length);
365         close(upf);
366         return RET_FAIL;
367     }
368     if (tgm.tel_length >= (long)sizeof(buf))
369         tgm.tel_length = sizeof(buf) - 1;
370     buf[tgm.tel_length] = 0;
371     pr(buf);
372     (void)close(upf);
373     return RET_OK;
374 }
375
376 int
377 match_user(char *file, struct player *player)
378 {
379     FILE *fp;
380     int match = 0;
381     s_char host[256];
382     s_char user[256];
383
384     if ((fp = fopen(file, "r")) == NULL) {
385         /*logerror("Cannot find file %s", file); */
386         return 0;
387     }
388     match = 0;
389     while (!feof(fp) && !match) {
390         if (fgets(host, sizeof(host) - 1, fp) == NULL)
391             break;
392         if (host[0] == '#')
393             continue;
394         if (fgets(user, sizeof(user) - 1, fp) == NULL)
395             break;
396         host[strlen(host) - 1] = '\0';
397         user[strlen(user) - 1] = '\0';
398         if (strstr(player->userid, user) &&
399             (strstr(player->hostaddr, host) ||
400              strstr(player->hostname, host)))
401             ++match;
402     }
403     fclose(fp);
404     return match;
405 }
406
407 int
408 quit(void)
409 {
410     player->state = PS_SHUTDOWN;
411     return RET_OK;
412 }
413
414 s_char *
415 praddr(struct player *player)
416 {
417     return prbuf("%s@%s", player->userid,
418                  *player->hostname ? player->hostname : player->hostaddr);
419 }