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)
66 p->state = PS_PLAYING;
68 time(&player->lasttime);
71 if (init_nats() < 0) {
72 pr("Server confused, try again later\n");
75 natp = getnatp(player->cnum);
76 if (!gamehours(player->curup)) {
77 pr("Empire hours restriction in force\n");
78 if (natp->nat_stat != STAT_GOD)
81 daychange(player->curup);
82 if ((player->timeleft = gettimeleft(player->curup, m_m_p_d)) <= 0) {
83 pr("Time exceeded today\n");
86 if (natp->nat_stat != STAT_VIS
87 && natp->nat_last_login
88 && (strcmp(natp->nat_hostaddr, player->hostaddr)
89 || strcmp(natp->nat_userid, player->userid))) {
90 pr("Last connection from: %s", ctime(&natp->nat_last_login));
92 natp->nat_last_login <= natp->nat_last_logout
93 ? ctime(&natp->nat_last_logout) : "?");
96 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
98 strcpy(natp->nat_userid, player->userid);
99 strcpy(natp->nat_hostname, player->hostname);
100 strcpy(natp->nat_hostaddr, player->hostaddr);
102 time(&natp->nat_last_login);
105 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
106 if (natp->nat_tgms == 1)
107 pr("You have a new telegram waiting ...\n");
109 pr("You have %s new telegrams waiting ...\n",
110 numstr(buf, natp->nat_tgms));
116 player->aborted = player->eof;
119 /* #*# I put the following line in to prevent server crash -KHS */
120 natp = getnatp(player->cnum);
122 * randomly round up to the nearest minute,
123 * charging at least 15 seconds.
125 time(&natp->nat_last_logout);
126 secs = MAX(natp->nat_last_logout - player->lasttime, 15);
127 natp->nat_timeused += secs;
136 char *redir; /* UTF-8 */
137 char scanspace[1024];
139 if (getcommand(player->combuf) < 0)
141 if (parse(player->combuf, scanspace, player->argp, player->comtail,
142 &player->condarg, &redir) < 0) {
143 pr("See \"info Syntax\"?\n");
145 if (dispatch(player->combuf, redir) < 0)
146 pr("Try \"list of commands\" or \"info\"\n");
155 int old_nstat, second;
158 if (player->eof || player->state == PS_SHUTDOWN)
160 natp = getnatp(player->cnum);
161 if (player->dolcost > 100.0)
162 pr("That just cost you $%.2f\n", player->dolcost);
163 else if (player->dolcost < -100.0)
164 pr("You just made $%.2f\n", -player->dolcost);
165 if (player->dolcost != 0.0) {
167 * Hackish work around for a race condition in the nightly
168 * build's regression tests: sometimes the update starts right
169 * after the force command yields, sometimes a bit later. If
170 * it is late, we use one random number here, for the bye,
171 * and throwing off the random sequence.
173 natp->nat_money -= roundavg(player->dolcost);
174 player->dolcost = 0.0;
177 old_nstat = player->nstat;
178 player_set_nstat(player, natp);
179 if ((old_nstat & MONEY) && !(player->nstat & MONEY))
180 pr("You are now broke; industries are on strike.\n");
181 if (!(old_nstat & MONEY) && (player->nstat & MONEY))
182 pr("You are no longer broke!\n");
184 time(&player->curup);
185 second = player->curup - player->lasttime;
187 player->timeleft -= second;
188 if (player->timeleft <= 0) {
190 * countdown timer "player->timeleft" has expired.
191 * either day change, or hours restriction
193 daychange(player->curup);
194 if (!gamehours(player->curup)) {
195 pr("Empire hours restriction in force\n");
196 if (natp->nat_stat != STAT_GOD) {
201 player->timeleft = gettimeleft(player->curup, m_m_p_d);
203 player->lasttime += second;
204 natp->nat_timeused += second;
206 if (natp->nat_stat == STAT_ACTIVE &&
207 natp->nat_timeused > m_m_p_d * 60) {
208 pr("Max minutes per day limit exceeded.\n");
209 player->nstat = (player->nstat & ~NORM) | VIS;
211 if (player->btused) {
212 natp->nat_btu -= player->btused;
215 if (natp->nat_tgms > 0) {
216 if (!(natp->nat_flags & NF_INFORM)) {
217 if (natp->nat_tgms == 1)
218 pr("You have a new telegram waiting ...\n");
220 pr("You have %s new telegrams waiting ...\n",
221 numstr(buf, natp->nat_tgms));
225 if (natp->nat_ann > 0) {
226 if (natp->nat_ann == 1)
227 pr("You have a new announcement waiting ...\n");
229 pr("You have %s new announcements waiting ...\n",
230 numstr(buf, natp->nat_ann));
233 if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
234 pr("You lost your capital... better designate one\n");
236 if (gamedown() && !player->god) {
244 * XXX This whole mess should be redone; execute block should
245 * start with "exec start", and should end with "exec end".
246 * We'll wait until 1.2 I guess.
254 char *redir; /* UTF-8 */
255 char scanspace[1024];
259 if (player->comtail[1])
260 p = player->comtail[1];
262 p = getstring("File? ", buf);
263 if (p == NULL || *p == '\0')
267 while (!failed && status()) {
268 player->nstat &= ~EXEC;
269 if (recvclient(buf, sizeof(buf)) < 0)
271 if (parse(buf, scanspace, player->argp, player->comtail,
272 &player->condarg, &redir) < 0) {
276 pr("\nExecute : %s\n", buf);
278 pr("Execute : redirection not supported\n");
280 } else if (dispatch(buf, NULL) < 0)
284 while (recvclient(buf, sizeof(buf)) >= 0) ;
287 pr("Execute : %s\n", failed ? "aborted" : "terminated");
297 char buf[MAXTELSIZE + 1]; /* UTF-8 */
299 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
303 pr ("Could not open motd.\n");
304 logerror("Could not open motd (%s).\n", motdfil);
308 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
309 logerror("bad header on login message (motdfil)");
313 if (tgm.tel_length >= (long)sizeof(buf)) {
314 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
318 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
319 logerror("bad length %ld on login message", tgm.tel_length);
323 buf[tgm.tel_length] = 0;
332 player->state = PS_SHUTDOWN;
337 praddr(struct player *p)
339 return prbuf("%s@%s", p->userid,
340 *p->hostname ? p->hostname : p->hostaddr);