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