]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
660c877359e188db5b46fb7a5cfd6d5485ba1768
[empserver] / src / lib / player / player.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  player.c: Main command loop for a player
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 2000
31  *     Markus Armbruster, 2004-2014
32  *     Ron Koenderink, 2004-2009
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "empio.h"
39 #include "empthread.h"
40 #include "misc.h"
41 #include "nat.h"
42 #include "optlist.h"
43 #include "player.h"
44 #include "prototypes.h"
45
46
47 static int command(void);
48 static int status(void);
49
50 struct player *player;
51
52 void
53 player_main(struct player *p)
54 {
55     struct natstr *natp;
56     char buf[128];
57
58     player = p;
59     time(&player->curup);
60     update_timeused_login(player->curup);
61     show_motd();
62     if (running_test_suite)
63         pr("\n"
64            "***          Server configured for testing          ***\n"
65            "*** If you see this in a game, it is misconfigured! ***\n");
66     if (init_nats() < 0) {
67         pr("Server confused, try again later\n");
68         return;
69     }
70     natp = getnatp(player->cnum);
71     if (!may_play_now(natp, player->curup))
72         return;
73     if (natp->nat_stat != STAT_VIS
74         && natp->nat_last_login
75         && (strcmp(natp->nat_hostaddr, player->hostaddr)
76             || strcmp(natp->nat_userid, player->userid))) {
77         pr("Last connection from: %s", ctime(&natp->nat_last_login));
78         pr("                  to: %s",
79            natp->nat_last_login <= natp->nat_last_logout
80            ? ctime(&natp->nat_last_logout) : "?");
81         pr("                  by: %s@%s\n",
82            natp->nat_userid, natp->nat_hostaddr);
83     }
84     strcpy(natp->nat_userid, player->userid);
85     strcpy(natp->nat_hostaddr, player->hostaddr);
86     natp->nat_last_login = player->curup;
87     putnat(natp);
88     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
89         if (natp->nat_tgms == 1)
90             pr("You have a new telegram waiting ...\n");
91         else
92             pr("You have %s new telegrams waiting ...\n",
93                numstr(buf, natp->nat_tgms));
94         natp->nat_tgms = 0;
95     }
96
97     while (status() && command()) {
98         if (player->got_ctld)
99             io_set_eof(player->iop);
100         player->aborted = 0;
101         empth_yield();
102     }
103     /* #*# I put the following line in to prevent server crash -KHS */
104     natp = getnatp(player->cnum);
105     time(&natp->nat_last_logout);
106     putnat(natp);
107     update_timeused(natp->nat_last_logout);
108     enforce_minimum_session_time();
109     pr("Bye-bye\n");
110 }
111
112 static int
113 command(void)
114 {
115     struct natstr *natp = getnatp(player->cnum);
116     char *redir;                /* UTF-8 */
117     time_t now;
118
119     prprompt(natp->nat_timeused / 60, natp->nat_btu);
120     if (getcommand(player->combuf) < 0)
121         return player->aborted;
122
123     now = time(NULL);
124     update_timeused(now);
125     if (!player->god && !may_play_now(natp, now))
126         return 0;
127
128     if (parse(player->combuf, player->argbuf, player->argp,
129               player->comtail, &player->condarg, &redir) < 0) {
130         pr("See \"info Syntax\"?\n");
131     } else {
132         if (dispatch(player->combuf, redir) < 0)
133             pr("Try \"list of commands\" or \"info\"\n");
134     }
135     return 1;
136 }
137
138 static int
139 status(void)
140 {
141     struct natstr *natp;
142     int old_nstat;
143     char buf[128];
144
145     natp = getnatp(player->cnum);
146     if (player->dolcost > 100.0)
147         pr("That just cost you $%.2f\n", player->dolcost);
148     else if (player->dolcost < -100.0)
149         pr("You just made $%.2f\n", -player->dolcost);
150     if (player->dolcost != 0.0) {
151         /*
152          * Hackish work around for a race condition in the nightly
153          * build's regression tests: sometimes the update starts right
154          * after the force command yields, sometimes a bit later.  If
155          * it is late, we use one random number here, for the bye,
156          * and throwing off the random sequence.
157          */
158         natp->nat_money -= roundavg(player->dolcost);
159         player->dolcost = 0.0;
160     }
161
162     old_nstat = player->nstat;
163     player_set_nstat(player, natp);
164     if ((old_nstat & MONEY) && !(player->nstat & MONEY))
165         pr("You are now broke; industries are on strike.\n");
166     if (!(old_nstat & MONEY) && (player->nstat & MONEY))
167         pr("You are no longer broke!\n");
168
169     time(&player->curup);
170     update_timeused(player->curup);
171     if (io_error(player->iop) || io_eof(player->iop)
172         || !may_play_now(natp, player->curup))
173         return 0;
174
175     if (player->btused) {
176         natp->nat_btu -= player->btused;
177         player->btused = 0;
178     }
179     if (natp->nat_tgms > 0) {
180         if (!(natp->nat_flags & NF_INFORM)) {
181             if (natp->nat_tgms == 1)
182                 pr("You have a new telegram waiting ...\n");
183             else
184                 pr("You have %s new telegrams waiting ...\n",
185                    numstr(buf, natp->nat_tgms));
186             natp->nat_tgms = 0;
187         }
188     }
189     if (natp->nat_ann > 0) {
190         if (natp->nat_ann == 1)
191             pr("You have a new announcement waiting ...\n");
192         else
193             pr("You have %s new announcements waiting ...\n",
194                numstr(buf, natp->nat_ann));
195         natp->nat_ann = 0;
196     }
197     if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
198         pr("You lost your capital... better designate one (see info capital)\n");
199     putnat(natp);
200     return 1;
201 }
202
203 /*
204  * Make all objects stale if @arg is one of the player's command arguments.
205  * See ef_make_stale() for what "making stale" means.
206  * Useful for functions that prompt for missing arguments.
207  * These can yield the processor, so we'd like to call ef_make_stale()
208  * there.  Except that leads to false positives when the caller passes
209  * an argument that is never null, and relies on the fact that the
210  * function doesn't yield then.  We can't know that in general.  But
211  * we do know in the common special case of command arguments.
212  */
213 void
214 make_stale_if_command_arg(char *arg)
215 {
216     if (player->argbuf <= arg
217         && arg <= player->argbuf + sizeof(player->argbuf))
218         ef_make_stale();
219 }
220
221 /*
222  * XXX This whole mess should be redone; execute block should
223  * start with "exec start", and should end with "exec end".
224  * We'll wait until 1.2 I guess.
225  */
226 int
227 execute(void)
228 {
229     char buf[1024];             /* UTF-8 */
230     int failed;
231     char *p;                    /* UTF-8 */
232     char *redir;                /* UTF-8 */
233
234     failed = 0;
235
236     p = player->comtail[1];
237     if (!p)
238         p = ugetstring("File? ", buf);
239     if (p == NULL || *p == '\0')
240         return RET_SYN;
241     prexec(p);
242
243     while (!failed && status() && !player->got_ctld) {
244         player->nstat &= ~EXEC;
245         if (getcommand(player->combuf) < 0)
246             break;
247         if (parse(player->combuf, player->argbuf, player->argp,
248                   player->comtail, &player->condarg, &redir) < 0) {
249             failed = 1;
250             continue;
251         }
252         pr("\nExecute : ");
253         uprnf(player->combuf);
254         pr("\n");
255         if (redir) {
256             pr("Execute : redirection not supported\n");
257             failed = 1;
258         } else if (dispatch(player->combuf, NULL) < 0)
259             failed = 1;
260         /* player->aborted not reset; makes next getcommand() fail */
261         empth_yield();
262     }
263     if (failed) {
264         while (recvclient(buf, sizeof(buf)) >= 0) ;
265     }
266
267     pr("Execute : %s\n", failed ? "aborted" : "terminated");
268     player->got_ctld = 0;
269     return RET_OK;
270 }
271
272 int
273 show_motd(void)
274 {
275     show_first_tel(motdfil);
276     return RET_OK;
277 }
278
279 int
280 quit(void)
281 {
282     io_set_eof(player->iop);
283     return RET_OK;
284 }
285
286 char *
287 praddr(struct player *p)
288 {
289     return prbuf("%s@%s", p->userid, p->hostaddr);
290 }