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 static int status(void);
64 struct player *player;
67 player_main(struct player *p)
73 p->state = PS_PLAYING;
75 time(&player->lasttime);
80 natp = getnatp(player->cnum);
81 if (player->god && !match_user(authfil, player)) {
82 logerror("NON-AUTHed Login attempted by %s", praddr(player));
83 pr("You're not a deity!\n");
86 if (!gamehours(player->curup)) {
87 pr("Empire hours restriction in force\n");
88 if ((natp->nat_stat & STAT_GOD) == 0)
91 daychange(player->curup);
92 if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
93 pr("Time exceeded today\n");
96 if ((*natp->nat_hostaddr &&
98 strcmp(natp->nat_hostaddr, player->hostaddr)) ||
100 *player->userid && strcmp(natp->nat_userid, player->userid))) {
101 if (natp->nat_stat != VIS) {
102 pr("Last connection from: %s", ctime(&natp->nat_last_login));
103 pr(" to: %s", natp->nat_last_login <
104 natp->nat_last_logout ? ctime(&natp->
105 nat_last_logout) : "?");
107 *natp->nat_userid ? natp->nat_userid : "nobody",
108 *natp->nat_hostname ? natp->nat_hostname
109 : *natp->nat_hostaddr ? natp->nat_hostaddr : "nowhere");
113 strcpy(natp->nat_userid, player->userid);
115 strcpy(natp->nat_userid, "nobody");
117 if (*player->hostname)
118 strcpy(natp->nat_hostname, player->hostname);
120 strcpy(natp->nat_hostname, "nowhere");
122 if (*player->hostaddr)
123 strcpy(natp->nat_hostaddr, player->hostaddr);
125 time(&natp->nat_last_login);
126 natp->nat_connected = 1;
128 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
129 if (natp->nat_tgms == 1)
130 pr("You have a new telegram waiting ...\n");
132 pr("You have %s new telegrams waiting ...\n",
133 numstr(buf, natp->nat_tgms));
138 if (command() == 0 && !player->aborted)
143 /* #*# I put the following line in to prevent server crash -KHS */
144 natp = getnatp(player->cnum);
146 * randomly round up to the nearest minute,
147 * charging at least 15 seconds.
149 time(&natp->nat_last_logout);
150 secs = max(natp->nat_last_logout - player->lasttime, 15);
151 natp->nat_minused += secs / 60;
153 if (chance(secs / 60.0))
154 natp->nat_minused += 1;
155 natp->nat_connected = 0;
164 char *redir; /* UTF-8 */
165 char scanspace[1024];
167 if (getcommand(player->combuf) < 0)
169 if (parse(player->combuf, player->argp, &player->condarg,
170 scanspace, &redir) < 0) {
171 pr("See \"info Syntax\"?\n");
173 /* XXX don't use alarm; use a scavenger thread */
174 /* DONT USE IT!!!! alarm and sleep may and dont work
176 /* alarm((unsigned int)60*60); 1 hour */
177 if (player->condarg != NULL)
178 for (x = 0; x < strlen(player->condarg); x++)
179 if (isupper(*(player->condarg + x)))
180 *(player->condarg + x) =
181 tolower(*(player->condarg + x));
182 if (dispatch(player->combuf, redir) < 0)
183 pr("Try \"list of commands\" or \"info\"\n");
196 if (player->state == PS_SHUTDOWN)
198 natp = getnatp(player->cnum);
199 if (io_error(player->iop) || io_eof(player->iop)) {
203 player->visitor = (natp->nat_stat & (STAT_NORM | STAT_GOD)) == 0;
204 if (player->dolcost != 0.0) {
205 if (player->dolcost > 100.0)
206 pr("That just cost you $%.2f\n", player->dolcost);
207 else if (player->dolcost < -100.0)
208 pr("You just made $%.2f\n", -player->dolcost);
209 if (natp->nat_money < player->dolcost && !player->broke) {
211 player->nstat &= ~MONEY;
212 pr("You are now broke; industries are on strike.\n");
213 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
215 player->nstat |= MONEY;
216 pr("You are no longer broke!\n");
218 natp->nat_money -= roundavg(player->dolcost);
219 player->dolcost = 0.0;
221 if (natp->nat_money < 0.0 && !player->broke) {
223 player->nstat &= ~MONEY;
224 pr("You are now broke; industries are on strike.\n");
226 if (player->broke && natp->nat_money > 0) {
228 player->nstat |= MONEY;
229 pr("You are no longer broke!\n");
232 getsect(natp->nat_xcap, natp->nat_ycap, §);
233 if ((sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT ||
234 sect.sct_type == SCT_SANCT) && sect.sct_own == player->cnum)
235 player->nstat |= CAP;
237 player->nstat &= ~CAP;
238 /* Ok, has the country owner reset his capital yet after it was sacked? */
239 if (natp->nat_flags & NF_SACKED)
240 player->nstat &= ~CAP; /* No capital yet */
241 player->ncomstat = player->nstat;
242 time(&player->curup);
243 minute = (player->curup - player->lasttime) / 60;
245 player->minleft -= minute;
246 if (player->minleft <= 0) {
248 * countdown timer "player->minleft" has expired.
249 * either day change, or hours restriction
251 daychange(player->curup);
252 if (!gamehours(player->curup)) {
253 pr("Empire hours restriction in force\n");
254 if ((natp->nat_stat & STAT_GOD) == 0) {
259 player->minleft = getminleft(player->curup, m_m_p_d);
261 player->lasttime += minute * 60;
262 natp->nat_minused += minute;
264 if ((player->nstat & NORM) && natp->nat_minused > m_m_p_d) {
265 pr("Max minutes per day limit exceeded.\n");
266 player->ncomstat = VIS;
268 if (player->btused) {
269 natp->nat_btu -= player->btused;
272 if (natp->nat_tgms > 0) {
273 if (!(natp->nat_flags & NF_INFORM)) {
274 if (natp->nat_tgms == 1)
275 pr("You have a new telegram waiting ...\n");
277 pr("You have %s new telegrams waiting ...\n",
278 numstr(buf, natp->nat_tgms));
282 if (natp->nat_ann > 0) {
283 if (natp->nat_ann == 1)
284 pr("You have a new announcement waiting ...\n");
286 pr("You have %s new announcements waiting ...\n",
287 numstr(buf, natp->nat_ann));
290 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
291 pr("You lost your capital... better designate one\n");
293 if (gamedown() && !player->god) {
301 * actually a command; redirection and piping ignored.
302 * XXX This whole mess should be redone; execute block should
303 * start with "exec start", and should end with "exec end".
304 * We'll wait until 1.2 I guess.
312 char *redir; /* UTF-8 */
313 char scanspace[1024];
318 p = getstarg(player->argp[1], "File? ", buf);
320 if (p == NULL || *p == '\0')
323 /* FIXME should use raw argument here, to support UTF-8 file names */
324 prexec(player->argp[1]);
326 while (!failed && status()) {
327 if (recvclient(buf, sizeof(buf)) < 0)
329 if (parse(buf, player->argp, &player->condarg,
330 scanspace, &redir) < 0) {
335 pr("\nExecute : %s\n", buf);
336 if (dispatch(buf, redir) < 0)
340 while (recvclient(buf, sizeof(buf)) >= 0) ;
343 pr("Execute : %s\n", failed ? "aborted" : "terminated");
352 char buf[MAXTELSIZE + 1]; /* UTF-8 */
354 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
358 pr ("Could not open motd.\n");
359 logerror("Could not open motd (%s).\n", motdfil);
363 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
364 logerror("bad header on login message (motdfil)");
368 if (tgm.tel_length >= (long)sizeof(buf)) {
369 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
373 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
374 logerror("bad length %ld on login message", tgm.tel_length);
378 buf[tgm.tel_length] = 0;
385 match_user(char *file, struct player *p)
392 if ((fp = fopen(file, "r")) == NULL) {
393 /*logerror("Cannot find file %s", file); */
397 while (!feof(fp) && !match) {
398 if (fgets(host, sizeof(host) - 1, fp) == NULL)
402 if (fgets(user, sizeof(user) - 1, fp) == NULL)
404 host[strlen(host) - 1] = '\0';
405 user[strlen(user) - 1] = '\0';
406 if (strstr(p->userid, user) &&
407 (strstr(p->hostaddr, host) ||
408 strstr(p->hostname, host)))
418 player->state = PS_SHUTDOWN;
423 praddr(struct player *p)
425 return prbuf("%s@%s", p->userid,
426 *p->hostname ? p->hostname : p->hostaddr);