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->minleft = getminleft(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_minused += secs / 60;
129 if (chance(secs / 60.0))
130 natp->nat_minused += 1;
139 char *redir; /* UTF-8 */
140 char scanspace[1024];
142 if (getcommand(player->combuf) < 0)
144 if (parse(player->combuf, scanspace, player->argp, player->comtail,
145 &player->condarg, &redir) < 0) {
146 pr("See \"info Syntax\"?\n");
148 if (dispatch(player->combuf, redir) < 0)
149 pr("Try \"list of commands\" or \"info\"\n");
158 int old_nstat, minute;
161 if (player->eof || player->state == PS_SHUTDOWN)
163 natp = getnatp(player->cnum);
164 if (player->dolcost > 100.0)
165 pr("That just cost you $%.2f\n", player->dolcost);
166 else if (player->dolcost < -100.0)
167 pr("You just made $%.2f\n", -player->dolcost);
168 if (player->dolcost != 0.0) {
170 * Hackish work around for a race condition in the nightly
171 * build's regression tests: sometimes the update starts right
172 * after the force command yields, sometimes a bit later. If
173 * it is late, we use one random number here, for the bye,
174 * and throwing off the random sequence.
176 natp->nat_money -= roundavg(player->dolcost);
177 player->dolcost = 0.0;
180 old_nstat = player->nstat;
181 player_set_nstat(player, natp);
182 if ((old_nstat & MONEY) && !(player->nstat & MONEY))
183 pr("You are now broke; industries are on strike.\n");
184 if (!(old_nstat & MONEY) && (player->nstat & MONEY))
185 pr("You are no longer broke!\n");
187 time(&player->curup);
188 minute = (player->curup - player->lasttime) / 60;
190 player->minleft -= minute;
191 if (player->minleft <= 0) {
193 * countdown timer "player->minleft" has expired.
194 * either day change, or hours restriction
196 daychange(player->curup);
197 if (!gamehours(player->curup)) {
198 pr("Empire hours restriction in force\n");
199 if (natp->nat_stat != STAT_GOD) {
204 player->minleft = getminleft(player->curup, m_m_p_d);
206 player->lasttime += minute * 60;
207 natp->nat_minused += minute;
209 if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
210 pr("Max minutes per day limit exceeded.\n");
211 player->nstat = (player->nstat & ~NORM) | VIS;
213 if (player->btused) {
214 natp->nat_btu -= player->btused;
217 if (natp->nat_tgms > 0) {
218 if (!(natp->nat_flags & NF_INFORM)) {
219 if (natp->nat_tgms == 1)
220 pr("You have a new telegram waiting ...\n");
222 pr("You have %s new telegrams waiting ...\n",
223 numstr(buf, natp->nat_tgms));
227 if (natp->nat_ann > 0) {
228 if (natp->nat_ann == 1)
229 pr("You have a new announcement waiting ...\n");
231 pr("You have %s new announcements waiting ...\n",
232 numstr(buf, natp->nat_ann));
235 if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
236 pr("You lost your capital... better designate one\n");
238 if (gamedown() && !player->god) {
246 * XXX This whole mess should be redone; execute block should
247 * start with "exec start", and should end with "exec end".
248 * We'll wait until 1.2 I guess.
256 char *redir; /* UTF-8 */
257 char scanspace[1024];
261 if (player->comtail[1])
262 p = player->comtail[1];
264 p = getstring("File? ", buf);
265 if (p == NULL || *p == '\0')
269 while (!failed && status()) {
270 player->nstat &= ~EXEC;
271 if (recvclient(buf, sizeof(buf)) < 0)
273 if (parse(buf, scanspace, player->argp, player->comtail,
274 &player->condarg, &redir) < 0) {
278 pr("\nExecute : %s\n", buf);
280 pr("Execute : redirection not supported\n");
282 } else if (dispatch(buf, NULL) < 0)
286 while (recvclient(buf, sizeof(buf)) >= 0) ;
289 pr("Execute : %s\n", failed ? "aborted" : "terminated");
299 char buf[MAXTELSIZE + 1]; /* UTF-8 */
301 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
305 pr ("Could not open motd.\n");
306 logerror("Could not open motd (%s).\n", motdfil);
310 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
311 logerror("bad header on login message (motdfil)");
315 if (tgm.tel_length >= (long)sizeof(buf)) {
316 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
320 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
321 logerror("bad length %ld on login message", tgm.tel_length);
325 buf[tgm.tel_length] = 0;
334 player->state = PS_SHUTDOWN;
339 praddr(struct player *p)
341 return prbuf("%s@%s", p->userid,
342 *p->hostname ? p->hostname : p->hostaddr);