]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
Update known contributors comment.
[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
36 #include "prototypes.h"
37 #include <string.h>
38 #include "misc.h"
39 #include "player.h"
40 #include "proto.h"
41 #include "com.h"
42 #include "nat.h"
43 #include "sect.h"
44 #include "file.h"
45 #include "proto.h"
46 #include "empio.h"
47 #include "empthread.h"
48 #include "tel.h"
49 #include "gen.h"
50 #include "subs.h"
51 #include "common.h"
52 #include "optlist.h"
53
54 #if !defined(_WIN32)
55 #include <unistd.h>
56 #include <sys/time.h>
57 #endif
58 #include <stdio.h>
59 #include <errno.h>
60 #include <fcntl.h>
61
62 static int status(void);
63
64 struct player *player;
65
66 void
67 player_main(struct player *p)
68 {
69     struct natstr *natp;
70     int secs;
71     char buf[128];
72
73     p->state = PS_PLAYING;
74     player = p;
75     time(&player->lasttime);
76     time(&player->curup);
77     show_motd();
78     if (init_nats() < 0) {
79         pr("Server confused, try again later\n");
80         return;
81     }
82     natp = getnatp(player->cnum);
83     if (!gamehours(player->curup)) {
84         pr("Empire hours restriction in force\n");
85         if (natp->nat_stat != STAT_GOD)
86             return;
87     }
88     daychange(player->curup);
89     if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
90         pr("Time exceeded today\n");
91         return;
92     }
93     if (natp->nat_stat != STAT_VIS
94         && natp->nat_last_login
95         && (strcmp(natp->nat_hostaddr, player->hostaddr)
96             || strcmp(natp->nat_userid, player->userid))) {
97         pr("Last connection from: %s", ctime(&natp->nat_last_login));
98         pr("                  to: %s",
99            natp->nat_last_login <= natp->nat_last_logout
100            ? ctime(&natp->nat_last_logout) : "?");
101         pr("                  by: %s@%s\n",
102            natp->nat_userid,
103            *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
104     }
105     strcpy(natp->nat_userid, player->userid);
106     strcpy(natp->nat_hostname, player->hostname);
107     strcpy(natp->nat_hostaddr, player->hostaddr);
108
109     time(&natp->nat_last_login);
110     putnat(natp);
111     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
112         if (natp->nat_tgms == 1)
113             pr("You have a new telegram waiting ...\n");
114         else
115             pr("You have %s new telegrams waiting ...\n",
116                numstr(buf, natp->nat_tgms));
117         natp->nat_tgms = 0;
118     }
119
120     while (status()) {
121         if (command() == 0 && !player->aborted)
122             break;
123         player->aborted = 0;
124         empth_yield();
125     }
126     /* #*# I put the following line in to prevent server crash -KHS */
127     natp = getnatp(player->cnum);
128     /*
129      * randomly round up to the nearest minute,
130      * charging at least 15 seconds.
131      */
132     time(&natp->nat_last_logout);
133     secs = MAX(natp->nat_last_logout - player->lasttime, 15);
134     natp->nat_minused += secs / 60;
135     secs = secs % 60;
136     if (chance(secs / 60.0))
137         natp->nat_minused += 1;
138     putnat(natp);
139     pr("Bye-bye\n");
140 }
141
142 int
143 command(void)
144 {
145     unsigned int x;
146     char *redir;                /* UTF-8 */
147     char scanspace[1024];
148
149     if (getcommand(player->combuf) < 0)
150         return 0;
151     if (parse(player->combuf, player->argp, &player->condarg,
152               scanspace, &redir) < 0) {
153         pr("See \"info Syntax\"?\n");
154     } else {
155         /* XXX don't use alarm; use a scavenger thread */
156         /* DONT USE IT!!!! alarm and sleep may and dont work
157            together -- Sasha */
158         /* alarm((unsigned int)60*60); 1 hour */
159         if (player->condarg != NULL)
160             for (x = 0; x < strlen(player->condarg); x++)
161                 if (isupper(*(player->condarg + x)))
162                     *(player->condarg + x) =
163                         tolower(*(player->condarg + x));
164         if (dispatch(player->combuf, redir) < 0)
165             pr("Try \"list of commands\" or \"info\"\n");
166     }
167     return 1;
168 }
169
170 static int
171 status(void)
172 {
173     struct natstr *natp;
174     int minute;
175     struct sctstr sect;
176     char buf[128];
177
178     if (player->state == PS_SHUTDOWN)
179         return 0;
180     natp = getnatp(player->cnum);
181     if (io_error(player->iop) || io_eof(player->iop)) {
182         putnat(natp);
183         return 0;
184     }
185     player->visitor = natp->nat_stat < STAT_SANCT;
186     if (player->dolcost != 0.0) {
187         if (player->dolcost > 100.0)
188             pr("That just cost you $%.2f\n", player->dolcost);
189         else if (player->dolcost < -100.0)
190             pr("You just made $%.2f\n", -player->dolcost);
191         if (natp->nat_money < player->dolcost && !player->broke) {
192             player->broke = 1;
193             player->nstat &= ~MONEY;
194             pr("You are now broke; industries are on strike.\n");
195         } else if (player->broke && natp->nat_money - player->dolcost > 0) {
196             player->broke = 0;
197             player->nstat |= MONEY;
198             pr("You are no longer broke!\n");
199         }
200         natp->nat_money -= roundavg(player->dolcost);
201         player->dolcost = 0.0;
202     } else {
203         if (natp->nat_money < 0.0 && !player->broke) {
204             player->broke = 1;
205             player->nstat &= ~MONEY;
206             pr("You are now broke; industries are on strike.\n");
207         }
208         if (player->broke && natp->nat_money > 0) {
209             player->broke = 0;
210             player->nstat |= MONEY;
211             pr("You are no longer broke!\n");
212         }
213     }
214     getsect(natp->nat_xcap, natp->nat_ycap, &sect);
215     if (influx(natp))
216         player->nstat &= ~CAP;
217     else
218         player->nstat |= CAP;
219     player->ncomstat = player->nstat;
220     if (player->god)
221         player->ncomstat |= CAP | MONEY;
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) {
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 (natp->nat_stat == STAT_ACTIVE && 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 quit(void)
366 {
367     player->state = PS_SHUTDOWN;
368     return RET_OK;
369 }
370
371 char *
372 praddr(struct player *p)
373 {
374     return prbuf("%s@%s", p->userid,
375                  *p->hostname ? p->hostname : p->hostaddr);
376 }