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
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.
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.
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/>.
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.
27 * player.c: Main command loop for a player
29 * Known contributors to this file:
31 * Markus Armbruster, 2004-2012
32 * Ron Koenderink, 2004-2009
39 #include "empthread.h"
47 #include "prototypes.h"
51 static int command(void);
52 static int status(void);
54 struct player *player;
57 player_main(struct player *p)
62 p->state = PS_PLAYING;
65 update_timeused_login(player->curup);
67 if (init_nats() < 0) {
68 pr("Server confused, try again later\n");
71 natp = getnatp(player->cnum);
72 if (!may_play_now(natp, player->curup))
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));
80 natp->nat_last_login <= natp->nat_last_logout
81 ? ctime(&natp->nat_last_logout) : "?");
84 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
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;
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");
96 pr("You have %s new telegrams waiting ...\n",
97 numstr(buf, natp->nat_tgms));
101 while (status() && command()) {
102 if (player->got_ctld)
103 io_set_eof(player->iop);
107 /* #*# I put the following line in to prevent server crash -KHS */
108 natp = getnatp(player->cnum);
109 time(&natp->nat_last_logout);
111 update_timeused(natp->nat_last_logout);
112 enforce_minimum_session_time();
120 struct natstr *natp = getnatp(player->cnum);
121 char *redir; /* UTF-8 */
124 prprompt(natp->nat_timeused / 60, natp->nat_btu);
125 if (getcommand(player->combuf) < 0)
126 return player->aborted;
129 update_timeused(now);
130 if (!player->god && !may_play_now(natp, now))
133 if (parse(player->combuf, player->argbuf, player->argp,
134 player->comtail, &player->condarg, &redir) < 0) {
135 pr("See \"info Syntax\"?\n");
137 if (dispatch(player->combuf, redir) < 0)
138 pr("Try \"list of commands\" or \"info\"\n");
150 natp = getnatp(player->cnum);
151 if (player->dolcost > 100.0)
152 pr("That just cost you $%.2f\n", player->dolcost);
153 else if (player->dolcost < -100.0)
154 pr("You just made $%.2f\n", -player->dolcost);
155 if (player->dolcost != 0.0) {
157 * Hackish work around for a race condition in the nightly
158 * build's regression tests: sometimes the update starts right
159 * after the force command yields, sometimes a bit later. If
160 * it is late, we use one random number here, for the bye,
161 * and throwing off the random sequence.
163 natp->nat_money -= roundavg(player->dolcost);
164 player->dolcost = 0.0;
167 old_nstat = player->nstat;
168 player_set_nstat(player, natp);
169 if ((old_nstat & MONEY) && !(player->nstat & MONEY))
170 pr("You are now broke; industries are on strike.\n");
171 if (!(old_nstat & MONEY) && (player->nstat & MONEY))
172 pr("You are no longer broke!\n");
174 time(&player->curup);
175 update_timeused(player->curup);
176 if (io_error(player->iop) || io_eof(player->iop)
177 || player->state == PS_SHUTDOWN
178 || !may_play_now(natp, player->curup))
181 if (player->btused) {
182 natp->nat_btu -= player->btused;
185 if (natp->nat_tgms > 0) {
186 if (!(natp->nat_flags & NF_INFORM)) {
187 if (natp->nat_tgms == 1)
188 pr("You have a new telegram waiting ...\n");
190 pr("You have %s new telegrams waiting ...\n",
191 numstr(buf, natp->nat_tgms));
195 if (natp->nat_ann > 0) {
196 if (natp->nat_ann == 1)
197 pr("You have a new announcement waiting ...\n");
199 pr("You have %s new announcements waiting ...\n",
200 numstr(buf, natp->nat_ann));
203 if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
204 pr("You lost your capital... better designate one (see info capital)\n");
210 * Make all objects stale if ARG is one of the player's command arguments.
211 * See ef_make_stale() for what "making stale" means.
212 * Useful for functions that prompt for missing arguments.
213 * These can yield the processor, so we'd like to call ef_make_stale()
214 * there. Except that leads to false positives when the caller passes
215 * an argument that is never null, and relies on the fact that the
216 * function doesn't yield then. We can't know that in general. But
217 * we do know in the common special case of command arguments.
220 make_stale_if_command_arg(char *arg)
222 if (player->argbuf <= arg
223 && arg <= player->argbuf + sizeof(player->argbuf))
228 * XXX This whole mess should be redone; execute block should
229 * start with "exec start", and should end with "exec end".
230 * We'll wait until 1.2 I guess.
235 char buf[1024]; /* UTF-8 */
238 char *redir; /* UTF-8 */
242 p = player->comtail[1];
244 p = ugetstring("File? ", buf);
245 if (p == NULL || *p == '\0')
249 while (!failed && status() && !player->got_ctld) {
250 player->nstat &= ~EXEC;
251 if (getcommand(player->combuf) < 0)
253 if (parse(player->combuf, player->argbuf, player->argp,
254 player->comtail, &player->condarg, &redir) < 0) {
262 pr("Execute : redirection not supported\n");
264 } else if (dispatch(buf, NULL) < 0)
269 while (recvclient(buf, sizeof(buf)) >= 0) ;
272 pr("Execute : %s\n", failed ? "aborted" : "terminated");
273 player->got_ctld = 0;
280 show_first_tel(motdfil);
287 player->state = PS_SHUTDOWN;
292 praddr(struct player *p)
294 return prbuf("%s@%s", p->userid,
295 *p->hostname ? p->hostname : p->hostaddr);