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
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 (!gamehours(player->curup)) {
76 pr("Empire hours restriction in force\n");
77 if (natp->nat_stat != STAT_GOD)
80 if ((natp->nat_stat == STAT_ACTIVE || natp->nat_stat == STAT_SANCT)
81 && natp->nat_timeused > m_m_p_d * 60) {
82 pr("Max minutes per day limit exceeded.\n");
85 if (natp->nat_stat != STAT_VIS
86 && natp->nat_last_login
87 && (strcmp(natp->nat_hostaddr, player->hostaddr)
88 || strcmp(natp->nat_userid, player->userid))) {
89 pr("Last connection from: %s", ctime(&natp->nat_last_login));
91 natp->nat_last_login <= natp->nat_last_logout
92 ? ctime(&natp->nat_last_logout) : "?");
95 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
97 strcpy(natp->nat_userid, player->userid);
98 strcpy(natp->nat_hostname, player->hostname);
99 strcpy(natp->nat_hostaddr, player->hostaddr);
100 natp->nat_last_login = player->curup;
103 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
104 if (natp->nat_tgms == 1)
105 pr("You have a new telegram waiting ...\n");
107 pr("You have %s new telegrams waiting ...\n",
108 numstr(buf, natp->nat_tgms));
112 while (status() && command()) {
113 player->aborted = player->eof;
116 /* #*# I put the following line in to prevent server crash -KHS */
117 natp = getnatp(player->cnum);
118 time(&natp->nat_last_logout);
120 update_timeused(natp->nat_last_logout);
121 enforce_minimum_session_time();
129 char *redir; /* UTF-8 */
130 char scanspace[1024];
134 if (getcommand(player->combuf) < 0)
138 update_timeused(now);
139 natp = getnatp(player->cnum);
140 if ((natp->nat_stat == STAT_ACTIVE || natp->nat_stat == STAT_SANCT)
141 && natp->nat_timeused > m_m_p_d * 60) {
142 pr("Max minutes per day limit exceeded.\n");
146 if (parse(player->combuf, scanspace, player->argp, player->comtail,
147 &player->condarg, &redir) < 0) {
148 pr("See \"info Syntax\"?\n");
150 if (dispatch(player->combuf, redir) < 0)
151 pr("Try \"list of commands\" or \"info\"\n");
163 if (player->eof || player->state == PS_SHUTDOWN)
165 natp = getnatp(player->cnum);
166 if (player->dolcost > 100.0)
167 pr("That just cost you $%.2f\n", player->dolcost);
168 else if (player->dolcost < -100.0)
169 pr("You just made $%.2f\n", -player->dolcost);
170 if (player->dolcost != 0.0) {
172 * Hackish work around for a race condition in the nightly
173 * build's regression tests: sometimes the update starts right
174 * after the force command yields, sometimes a bit later. If
175 * it is late, we use one random number here, for the bye,
176 * and throwing off the random sequence.
178 natp->nat_money -= roundavg(player->dolcost);
179 player->dolcost = 0.0;
182 old_nstat = player->nstat;
183 player_set_nstat(player, natp);
184 if ((old_nstat & MONEY) && !(player->nstat & MONEY))
185 pr("You are now broke; industries are on strike.\n");
186 if (!(old_nstat & MONEY) && (player->nstat & MONEY))
187 pr("You are no longer broke!\n");
189 time(&player->curup);
190 update_timeused(player->curup);
191 if ((natp->nat_stat == STAT_ACTIVE || natp->nat_stat == STAT_SANCT)
192 && natp->nat_timeused > m_m_p_d * 60) {
193 pr("Max minutes per day limit exceeded.\n");
196 if (player->btused) {
197 natp->nat_btu -= player->btused;
200 if (natp->nat_tgms > 0) {
201 if (!(natp->nat_flags & NF_INFORM)) {
202 if (natp->nat_tgms == 1)
203 pr("You have a new telegram waiting ...\n");
205 pr("You have %s new telegrams waiting ...\n",
206 numstr(buf, natp->nat_tgms));
210 if (natp->nat_ann > 0) {
211 if (natp->nat_ann == 1)
212 pr("You have a new announcement waiting ...\n");
214 pr("You have %s new announcements waiting ...\n",
215 numstr(buf, natp->nat_ann));
218 if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
219 pr("You lost your capital... better designate one (see info capital)\n");
221 if (gamedown() && !player->god) {
229 * XXX This whole mess should be redone; execute block should
230 * start with "exec start", and should end with "exec end".
231 * We'll wait until 1.2 I guess.
239 char *redir; /* UTF-8 */
240 char scanspace[1024];
244 if (player->comtail[1])
245 p = player->comtail[1];
247 p = getstring("File? ", buf);
248 if (p == NULL || *p == '\0')
252 while (!failed && status()) {
253 player->nstat &= ~EXEC;
254 if (recvclient(buf, sizeof(buf)) < 0)
256 if (parse(buf, scanspace, player->argp, player->comtail,
257 &player->condarg, &redir) < 0) {
261 pr("\nExecute : %s\n", buf);
263 pr("Execute : redirection not supported\n");
265 } else if (dispatch(buf, NULL) < 0)
269 while (recvclient(buf, sizeof(buf)) >= 0) ;
272 pr("Execute : %s\n", failed ? "aborted" : "terminated");
282 char buf[MAXTELSIZE + 1]; /* UTF-8 */
284 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
288 pr ("Could not open motd.\n");
289 logerror("Could not open motd (%s).\n", motdfil);
293 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
294 logerror("bad header on login message (motdfil)");
298 if (tgm.tel_length >= (long)sizeof(buf)) {
299 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
303 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
304 logerror("bad length %ld on login message", tgm.tel_length);
308 buf[tgm.tel_length] = 0;
317 player->state = PS_SHUTDOWN;
322 praddr(struct player *p)
324 return prbuf("%s@%s", p->userid,
325 *p->hostname ? p->hostname : p->hostaddr);