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