2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * player.c: Main command loop for a player
30 * Known contributors to this file:
32 * Markus Armbruster, 2004-2008
33 * Ron Koenderink, 2004-2007
42 #include "empthread.h"
50 #include "prototypes.h"
54 static int command(void);
55 static int status(void);
57 struct player *player;
60 player_main(struct player *p)
65 p->state = PS_PLAYING;
68 update_timeused_login(player->curup);
70 if (init_nats() < 0) {
71 pr("Server confused, try again later\n");
74 natp = getnatp(player->cnum);
75 if (!may_play_now(natp, player->curup, 0))
77 if (natp->nat_stat != STAT_VIS
78 && natp->nat_last_login
79 && (strcmp(natp->nat_hostaddr, player->hostaddr)
80 || strcmp(natp->nat_userid, player->userid))) {
81 pr("Last connection from: %s", ctime(&natp->nat_last_login));
83 natp->nat_last_login <= natp->nat_last_logout
84 ? ctime(&natp->nat_last_logout) : "?");
87 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
89 strcpy(natp->nat_userid, player->userid);
90 strcpy(natp->nat_hostname, player->hostname);
91 strcpy(natp->nat_hostaddr, player->hostaddr);
92 natp->nat_last_login = player->curup;
95 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
96 if (natp->nat_tgms == 1)
97 pr("You have a new telegram waiting ...\n");
99 pr("You have %s new telegrams waiting ...\n",
100 numstr(buf, natp->nat_tgms));
104 while (status() && command()) {
105 player->aborted = player->eof;
108 /* #*# I put the following line in to prevent server crash -KHS */
109 natp = getnatp(player->cnum);
110 time(&natp->nat_last_logout);
112 update_timeused(natp->nat_last_logout);
113 enforce_minimum_session_time();
121 char *redir; /* UTF-8 */
122 char scanspace[1024];
125 if (getcommand(player->combuf) < 0)
129 update_timeused(now);
130 if (!may_play_now(getnatp(player->cnum), now, 1))
133 if (parse(player->combuf, scanspace, player->argp, player->comtail,
134 &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 if (player->eof || player->state == PS_SHUTDOWN)
152 natp = getnatp(player->cnum);
153 if (player->dolcost > 100.0)
154 pr("That just cost you $%.2f\n", player->dolcost);
155 else if (player->dolcost < -100.0)
156 pr("You just made $%.2f\n", -player->dolcost);
157 if (player->dolcost != 0.0) {
159 * Hackish work around for a race condition in the nightly
160 * build's regression tests: sometimes the update starts right
161 * after the force command yields, sometimes a bit later. If
162 * it is late, we use one random number here, for the bye,
163 * and throwing off the random sequence.
165 natp->nat_money -= roundavg(player->dolcost);
166 player->dolcost = 0.0;
169 old_nstat = player->nstat;
170 player_set_nstat(player, natp);
171 if ((old_nstat & MONEY) && !(player->nstat & MONEY))
172 pr("You are now broke; industries are on strike.\n");
173 if (!(old_nstat & MONEY) && (player->nstat & MONEY))
174 pr("You are no longer broke!\n");
176 time(&player->curup);
177 update_timeused(player->curup);
178 if (!may_play_now(natp, player->curup, 0))
180 if (player->btused) {
181 natp->nat_btu -= player->btused;
184 if (natp->nat_tgms > 0) {
185 if (!(natp->nat_flags & NF_INFORM)) {
186 if (natp->nat_tgms == 1)
187 pr("You have a new telegram waiting ...\n");
189 pr("You have %s new telegrams waiting ...\n",
190 numstr(buf, natp->nat_tgms));
194 if (natp->nat_ann > 0) {
195 if (natp->nat_ann == 1)
196 pr("You have a new announcement waiting ...\n");
198 pr("You have %s new announcements waiting ...\n",
199 numstr(buf, natp->nat_ann));
202 if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
203 pr("You lost your capital... better designate one (see info capital)\n");
209 * XXX This whole mess should be redone; execute block should
210 * start with "exec start", and should end with "exec end".
211 * We'll wait until 1.2 I guess.
219 char *redir; /* UTF-8 */
220 char scanspace[1024];
224 if (player->comtail[1])
225 p = player->comtail[1];
227 p = getstring("File? ", buf);
228 if (p == NULL || *p == '\0')
232 while (!failed && status()) {
233 player->nstat &= ~EXEC;
234 if (recvclient(buf, sizeof(buf)) < 0)
236 if (parse(buf, scanspace, player->argp, player->comtail,
237 &player->condarg, &redir) < 0) {
241 pr("\nExecute : %s\n", buf);
243 pr("Execute : redirection not supported\n");
245 } else if (dispatch(buf, NULL) < 0)
249 while (recvclient(buf, sizeof(buf)) >= 0) ;
252 pr("Execute : %s\n", failed ? "aborted" : "terminated");
260 show_first_tel(motdfil);
265 * Print first telegram in file FNAME.
268 show_first_tel(char *fname)
272 char buf[MAXTELSIZE + 1]; /* UTF-8 */
274 if ((fp = fopen(fname, "rb")) == NULL) {
278 logerror("Could not open %s.\n", fname);
282 if (fread(&tgm, sizeof(tgm), 1, fp) != 1) {
283 logerror("bad header on login message (%s)", fname);
287 if (tgm.tel_length >= (long)sizeof(buf)) {
288 logerror("text length (%ld) is too long for login message (%s)",
289 tgm.tel_length, fname);
293 if (fread(buf, tgm.tel_length, 1, fp) != 1) {
294 logerror("bad length %ld on login message", tgm.tel_length);
298 buf[tgm.tel_length] = 0;
307 player->state = PS_SHUTDOWN;
312 praddr(struct player *p)
314 return prbuf("%s@%s", p->userid,
315 *p->hostname ? p->hostname : p->hostaddr);