2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * player.c: Main command loop for a player
30 * Known contributors to this file:
35 #include "prototypes.h"
47 #include "empthread.h"
62 struct player *player;
65 player_main(struct player *p)
71 p->state = PS_PLAYING;
73 time(&player->lasttime);
78 natp = getnatp(player->cnum);
79 if (player->god && !match_user(authfil, player)) {
80 logerror("NON-AUTHed Login attempted by %s", praddr(player));
81 pr("You're not a deity!\n");
84 if (!gamehours(player->curup)) {
85 pr("Empire hours restriction in force\n");
86 if ((natp->nat_stat & STAT_GOD) == 0)
89 daychange(player->curup);
90 if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
91 pr("Time exceeded today\n");
94 if ((*natp->nat_hostaddr &&
96 strcmp(natp->nat_hostaddr, player->hostaddr)) ||
98 *player->userid && strcmp(natp->nat_userid, player->userid))) {
99 if (natp->nat_stat != VIS) {
100 pr("Last connection from: %s", ctime(&natp->nat_last_login));
101 pr(" to: %s", natp->nat_last_login <
102 natp->nat_last_logout ? ctime(&natp->
103 nat_last_logout) : "?");
105 *natp->nat_userid ? natp->nat_userid : "nobody",
106 *natp->nat_hostname ? natp->nat_hostname
107 : *natp->nat_hostaddr ? natp->nat_hostaddr : "nowhere");
111 strcpy(natp->nat_userid, player->userid);
113 strcpy(natp->nat_userid, "nobody");
115 if (*player->hostname)
116 strcpy(natp->nat_hostname, player->hostname);
118 strcpy(natp->nat_hostname, "nowhere");
120 if (*player->hostaddr)
121 strcpy(natp->nat_hostaddr, player->hostaddr);
123 time(&natp->nat_last_login);
124 natp->nat_connected = 1;
126 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
127 if (natp->nat_tgms == 1)
128 pr("You have a new telegram waiting ...\n");
130 pr("You have %s new telegrams waiting ...\n",
131 numstr(buf, natp->nat_tgms));
136 if (command() == 0 && !player->aborted)
140 /* #*# I put the following line in to prevent server crash -KHS */
141 natp = getnatp(player->cnum);
143 * randomly round up to the nearest minute,
144 * charging at least 15 seconds.
146 time(&natp->nat_last_logout);
147 secs = max(natp->nat_last_logout - player->lasttime, 15);
148 natp->nat_minused += secs / 60;
150 if (chance(secs / 60.0))
151 natp->nat_minused += 1;
152 natp->nat_connected = 0;
162 char scanspace[1024];
164 if (getcommand(player->combuf) < 0)
166 if (parse(player->combuf, player->argp, &player->condarg,
167 scanspace, &redir) < 0) {
168 pr("See \"info Syntax\"?\n");
170 /* XXX don't use alarm; use a scavenger thread */
171 /* DONT USE IT!!!! alarm and sleep may and dont work
173 /* alarm((unsigned int)60*60); 1 hour */
174 if (player->condarg != NULL)
175 for (x = 0; x < strlen(player->condarg); x++)
176 if (isupper(*(player->condarg + x)))
177 *(player->condarg + x) =
178 tolower(*(player->condarg + x));
179 if (dispatch(player->combuf, redir) < 0)
180 pr("Try \"list of commands\" or \"info\"\n");
193 if (player->state == PS_SHUTDOWN)
195 natp = getnatp(player->cnum);
196 if (io_error(player->iop) || io_eof(player->iop)) {
200 player->visitor = (natp->nat_stat & (STAT_NORM | STAT_GOD)) == 0;
201 if (player->dolcost != 0.0) {
202 if (player->dolcost > 100.0)
203 pr("That just cost you $%.2f\n", player->dolcost);
204 else if (player->dolcost < -100.0)
205 pr("You just made $%.2f\n", -player->dolcost);
206 if (natp->nat_money < player->dolcost && !player->broke) {
208 player->nstat &= ~MONEY;
209 pr("You are now broke; industries are on strike.\n");
210 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
212 player->nstat |= MONEY;
213 pr("You are no longer broke!\n");
215 natp->nat_money -= roundavg(player->dolcost);
216 player->dolcost = 0.0;
218 if (natp->nat_money < 0.0 && !player->broke) {
220 player->nstat &= ~MONEY;
221 pr("You are now broke; industries are on strike.\n");
223 if (player->broke && natp->nat_money > 0) {
225 player->nstat |= MONEY;
226 pr("You are no longer broke!\n");
229 getsect(natp->nat_xcap, natp->nat_ycap, §);
230 if ((sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT ||
231 sect.sct_type == SCT_SANCT) && sect.sct_own == player->cnum)
232 player->nstat |= CAP;
234 player->nstat &= ~CAP;
235 /* Ok, has the country owner reset his capital yet after it was sacked? */
236 if (natp->nat_flags & NF_SACKED)
237 player->nstat &= ~CAP; /* No capital yet */
238 player->ncomstat = player->nstat;
239 time(&player->curup);
240 minute = (player->curup - player->lasttime) / 60;
242 player->minleft -= minute;
243 if (player->minleft <= 0) {
245 * countdown timer "player->minleft" has expired.
246 * either day change, or hours restriction
248 daychange(player->curup);
249 if (!gamehours(player->curup)) {
250 pr("Empire hours restriction in force\n");
251 if ((natp->nat_stat & STAT_GOD) == 0) {
256 player->minleft = getminleft(player->curup, m_m_p_d);
258 player->lasttime += minute * 60;
259 natp->nat_minused += minute;
261 if ((player->nstat & NORM) && natp->nat_minused > m_m_p_d) {
262 pr("Max minutes per day limit exceeded.\n");
263 player->ncomstat = VIS;
265 if (player->btused) {
266 natp->nat_btu -= player->btused;
269 if (natp->nat_tgms > 0) {
270 if (!(natp->nat_flags & NF_INFORM)) {
271 if (natp->nat_tgms == 1)
272 pr("You have a new telegram waiting ...\n");
274 pr("You have %s new telegrams waiting ...\n",
275 numstr(buf, natp->nat_tgms));
279 if (natp->nat_ann > 0) {
280 if (natp->nat_ann == 1)
281 pr("You have a new announcement waiting ...\n");
283 pr("You have %s new announcements waiting ...\n",
284 numstr(buf, natp->nat_ann));
287 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
288 pr("You lost your capital... better designate one\n");
290 if (gamedown() && !player->god) {
298 * actually a command; redirection and piping ignored.
299 * XXX This whole mess should be redone; execute block should
300 * start with "exec start", and should end with "exec end".
301 * We'll wait until 1.2 I guess.
310 char scanspace[1024];
315 p = getstarg(player->argp[1], "File? ", buf);
317 if (p == NULL || *p == '\0')
320 prexec(player->argp[1]);
321 while (!failed && status()) {
322 if (recvclient(buf, sizeof(buf)) < 0)
324 if (parse(buf, player->argp, &player->condarg,
325 scanspace, &redir) < 0) {
330 pr("\nExecute : %s\n", buf);
331 if (dispatch(buf, redir) < 0)
335 while (recvclient(buf, sizeof(buf)) >= 0) ;
338 pr("Execute : %s\n", failed ? "aborted" : "terminated");
347 char buf[MAXTELSIZE + 1]; /* message text */
349 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
353 pr ("Could not open motd.\n");
354 logerror("Could not open motd (%s).\n", motdfil);
358 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
359 logerror("bad header on login message (motdfil)");
363 if (tgm.tel_length >= (long)sizeof(buf)) {
364 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
368 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
369 logerror("bad length %ld on login message", tgm.tel_length);
373 buf[tgm.tel_length] = 0;
380 match_user(char *file, struct player *p)
387 if ((fp = fopen(file, "r")) == NULL) {
388 /*logerror("Cannot find file %s", file); */
392 while (!feof(fp) && !match) {
393 if (fgets(host, sizeof(host) - 1, fp) == NULL)
397 if (fgets(user, sizeof(user) - 1, fp) == NULL)
399 host[strlen(host) - 1] = '\0';
400 user[strlen(user) - 1] = '\0';
401 if (strstr(p->userid, user) &&
402 (strstr(p->hostaddr, host) ||
403 strstr(p->hostname, host)))
413 player->state = PS_SHUTDOWN;
418 praddr(struct player *p)
420 return prbuf("%s@%s", p->userid,
421 *p->hostname ? p->hostname : p->hostaddr);