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 &&
81 natp->nat_timeused > m_m_p_d * 60) {
82 pr("Time exceeded today\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);
101 time(&natp->nat_last_login);
104 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
105 if (natp->nat_tgms == 1)
106 pr("You have a new telegram waiting ...\n");
108 pr("You have %s new telegrams waiting ...\n",
109 numstr(buf, natp->nat_tgms));
115 player->aborted = player->eof;
118 /* #*# I put the following line in to prevent server crash -KHS */
119 natp = getnatp(player->cnum);
120 time(&natp->nat_last_logout);
122 update_timeused(natp->nat_last_logout);
123 enforce_minimum_session_time();
131 char *redir; /* UTF-8 */
132 char scanspace[1024];
134 if (getcommand(player->combuf) < 0)
136 if (parse(player->combuf, scanspace, player->argp, player->comtail,
137 &player->condarg, &redir) < 0) {
138 pr("See \"info Syntax\"?\n");
140 if (dispatch(player->combuf, redir) < 0)
141 pr("Try \"list of commands\" or \"info\"\n");
153 if (player->eof || player->state == PS_SHUTDOWN)
155 natp = getnatp(player->cnum);
156 if (player->dolcost > 100.0)
157 pr("That just cost you $%.2f\n", player->dolcost);
158 else if (player->dolcost < -100.0)
159 pr("You just made $%.2f\n", -player->dolcost);
160 if (player->dolcost != 0.0) {
162 * Hackish work around for a race condition in the nightly
163 * build's regression tests: sometimes the update starts right
164 * after the force command yields, sometimes a bit later. If
165 * it is late, we use one random number here, for the bye,
166 * and throwing off the random sequence.
168 natp->nat_money -= roundavg(player->dolcost);
169 player->dolcost = 0.0;
172 old_nstat = player->nstat;
173 player_set_nstat(player, natp);
174 if ((old_nstat & MONEY) && !(player->nstat & MONEY))
175 pr("You are now broke; industries are on strike.\n");
176 if (!(old_nstat & MONEY) && (player->nstat & MONEY))
177 pr("You are no longer broke!\n");
179 time(&player->curup);
180 update_timeused(player->curup);
181 if (natp->nat_stat == STAT_ACTIVE &&
182 natp->nat_timeused > m_m_p_d * 60) {
183 pr("Max minutes per day limit exceeded.\n");
184 player->nstat = (player->nstat & ~NORM) | VIS;
186 if (player->btused) {
187 natp->nat_btu -= player->btused;
190 if (natp->nat_tgms > 0) {
191 if (!(natp->nat_flags & NF_INFORM)) {
192 if (natp->nat_tgms == 1)
193 pr("You have a new telegram waiting ...\n");
195 pr("You have %s new telegrams waiting ...\n",
196 numstr(buf, natp->nat_tgms));
200 if (natp->nat_ann > 0) {
201 if (natp->nat_ann == 1)
202 pr("You have a new announcement waiting ...\n");
204 pr("You have %s new announcements waiting ...\n",
205 numstr(buf, natp->nat_ann));
208 if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
209 pr("You lost your capital... better designate one\n");
211 if (gamedown() && !player->god) {
219 * XXX This whole mess should be redone; execute block should
220 * start with "exec start", and should end with "exec end".
221 * We'll wait until 1.2 I guess.
229 char *redir; /* UTF-8 */
230 char scanspace[1024];
234 if (player->comtail[1])
235 p = player->comtail[1];
237 p = getstring("File? ", buf);
238 if (p == NULL || *p == '\0')
242 while (!failed && status()) {
243 player->nstat &= ~EXEC;
244 if (recvclient(buf, sizeof(buf)) < 0)
246 if (parse(buf, scanspace, player->argp, player->comtail,
247 &player->condarg, &redir) < 0) {
251 pr("\nExecute : %s\n", buf);
253 pr("Execute : redirection not supported\n");
255 } else if (dispatch(buf, NULL) < 0)
259 while (recvclient(buf, sizeof(buf)) >= 0) ;
262 pr("Execute : %s\n", failed ? "aborted" : "terminated");
272 char buf[MAXTELSIZE + 1]; /* UTF-8 */
274 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
278 pr ("Could not open motd.\n");
279 logerror("Could not open motd (%s).\n", motdfil);
283 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
284 logerror("bad header on login message (motdfil)");
288 if (tgm.tel_length >= (long)sizeof(buf)) {
289 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
293 if (fread(buf, tgm.tel_length, 1, motd_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);