2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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:
37 #include "prototypes.h"
48 #include "empthread.h"
63 static int status(void);
65 struct player *player;
68 player_main(struct player *p)
74 p->state = PS_PLAYING;
76 time(&player->lasttime);
81 natp = getnatp(player->cnum);
82 if (!gamehours(player->curup)) {
83 pr("Empire hours restriction in force\n");
84 if (natp->nat_stat != STAT_GOD)
87 daychange(player->curup);
88 if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
89 pr("Time exceeded today\n");
92 if (natp->nat_stat != STAT_VIS
93 && natp->nat_last_login
94 && (strcmp(natp->nat_hostaddr, player->hostaddr)
95 || strcmp(natp->nat_userid, player->userid))) {
96 pr("Last connection from: %s", ctime(&natp->nat_last_login));
98 natp->nat_last_login <= natp->nat_last_logout
99 ? ctime(&natp->nat_last_logout) : "?");
102 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
104 strcpy(natp->nat_userid, player->userid);
105 strcpy(natp->nat_hostname, player->hostname);
106 strcpy(natp->nat_hostaddr, player->hostaddr);
108 time(&natp->nat_last_login);
110 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
111 if (natp->nat_tgms == 1)
112 pr("You have a new telegram waiting ...\n");
114 pr("You have %s new telegrams waiting ...\n",
115 numstr(buf, natp->nat_tgms));
120 if (command() == 0 && !player->aborted)
125 /* #*# I put the following line in to prevent server crash -KHS */
126 natp = getnatp(player->cnum);
128 * randomly round up to the nearest minute,
129 * charging at least 15 seconds.
131 time(&natp->nat_last_logout);
132 secs = MAX(natp->nat_last_logout - player->lasttime, 15);
133 natp->nat_minused += secs / 60;
135 if (chance(secs / 60.0))
136 natp->nat_minused += 1;
145 char *redir; /* UTF-8 */
146 char scanspace[1024];
148 if (getcommand(player->combuf) < 0)
150 if (parse(player->combuf, player->argp, &player->condarg,
151 scanspace, &redir) < 0) {
152 pr("See \"info Syntax\"?\n");
154 /* XXX don't use alarm; use a scavenger thread */
155 /* DONT USE IT!!!! alarm and sleep may and dont work
157 /* alarm((unsigned int)60*60); 1 hour */
158 if (player->condarg != NULL)
159 for (x = 0; x < strlen(player->condarg); x++)
160 if (isupper(*(player->condarg + x)))
161 *(player->condarg + x) =
162 tolower(*(player->condarg + x));
163 if (dispatch(player->combuf, redir) < 0)
164 pr("Try \"list of commands\" or \"info\"\n");
177 if (player->state == PS_SHUTDOWN)
179 natp = getnatp(player->cnum);
180 if (io_error(player->iop) || io_eof(player->iop)) {
184 player->visitor = natp->nat_stat < STAT_SANCT;
185 if (player->dolcost != 0.0) {
186 if (player->dolcost > 100.0)
187 pr("That just cost you $%.2f\n", player->dolcost);
188 else if (player->dolcost < -100.0)
189 pr("You just made $%.2f\n", -player->dolcost);
190 if (natp->nat_money < player->dolcost && !player->broke) {
192 player->nstat &= ~MONEY;
193 pr("You are now broke; industries are on strike.\n");
194 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
196 player->nstat |= MONEY;
197 pr("You are no longer broke!\n");
199 natp->nat_money -= roundavg(player->dolcost);
200 player->dolcost = 0.0;
202 if (natp->nat_money < 0.0 && !player->broke) {
204 player->nstat &= ~MONEY;
205 pr("You are now broke; industries are on strike.\n");
207 if (player->broke && natp->nat_money > 0) {
209 player->nstat |= MONEY;
210 pr("You are no longer broke!\n");
213 getsect(natp->nat_xcap, natp->nat_ycap, §);
215 player->nstat &= ~CAP;
217 player->nstat |= CAP;
218 player->ncomstat = player->nstat;
220 player->ncomstat |= CAP | MONEY;
221 time(&player->curup);
222 minute = (player->curup - player->lasttime) / 60;
224 player->minleft -= minute;
225 if (player->minleft <= 0) {
227 * countdown timer "player->minleft" has expired.
228 * either day change, or hours restriction
230 daychange(player->curup);
231 if (!gamehours(player->curup)) {
232 pr("Empire hours restriction in force\n");
233 if (natp->nat_stat != STAT_GOD) {
238 player->minleft = getminleft(player->curup, m_m_p_d);
240 player->lasttime += minute * 60;
241 natp->nat_minused += minute;
243 if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
244 pr("Max minutes per day limit exceeded.\n");
245 player->ncomstat = VIS;
247 if (player->btused) {
248 natp->nat_btu -= player->btused;
251 if (natp->nat_tgms > 0) {
252 if (!(natp->nat_flags & NF_INFORM)) {
253 if (natp->nat_tgms == 1)
254 pr("You have a new telegram waiting ...\n");
256 pr("You have %s new telegrams waiting ...\n",
257 numstr(buf, natp->nat_tgms));
261 if (natp->nat_ann > 0) {
262 if (natp->nat_ann == 1)
263 pr("You have a new announcement waiting ...\n");
265 pr("You have %s new announcements waiting ...\n",
266 numstr(buf, natp->nat_ann));
269 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
270 pr("You lost your capital... better designate one\n");
272 if (gamedown() && !player->god) {
280 * actually a command; redirection and piping ignored.
281 * XXX This whole mess should be redone; execute block should
282 * start with "exec start", and should end with "exec end".
283 * We'll wait until 1.2 I guess.
291 char *redir; /* UTF-8 */
292 char scanspace[1024];
297 p = getstarg(player->argp[1], "File? ", buf);
299 if (p == NULL || *p == '\0')
302 /* FIXME should use raw argument here, to support UTF-8 file names */
303 prexec(player->argp[1]);
305 while (!failed && status()) {
306 if (recvclient(buf, sizeof(buf)) < 0)
308 if (parse(buf, player->argp, &player->condarg,
309 scanspace, &redir) < 0) {
314 pr("\nExecute : %s\n", buf);
315 if (dispatch(buf, redir) < 0)
319 while (recvclient(buf, sizeof(buf)) >= 0) ;
322 pr("Execute : %s\n", failed ? "aborted" : "terminated");
331 char buf[MAXTELSIZE + 1]; /* UTF-8 */
333 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
337 pr ("Could not open motd.\n");
338 logerror("Could not open motd (%s).\n", motdfil);
342 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
343 logerror("bad header on login message (motdfil)");
347 if (tgm.tel_length >= (long)sizeof(buf)) {
348 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
352 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
353 logerror("bad length %ld on login message", tgm.tel_length);
357 buf[tgm.tel_length] = 0;
366 player->state = PS_SHUTDOWN;
371 praddr(struct player *p)
373 return prbuf("%s@%s", p->userid,
374 *p->hostname ? p->hostname : p->hostaddr);