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