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"
46 #include "empthread.h"
61 static int status(void);
63 struct player *player;
66 player_main(struct player *p)
72 p->state = PS_PLAYING;
74 time(&player->lasttime);
79 natp = getnatp(player->cnum);
80 if (player->god && !match_user(authfil, player)) {
81 logerror("NON-AUTHed Login attempted by %s", praddr(player));
82 pr("You're not a deity!\n");
85 if (!gamehours(player->curup)) {
86 pr("Empire hours restriction in force\n");
87 if ((natp->nat_stat & STAT_GOD) == 0)
90 daychange(player->curup);
91 if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
92 pr("Time exceeded today\n");
95 if (natp->nat_stat != VIS
96 && natp->nat_last_login
97 && (strcmp(natp->nat_hostaddr, player->hostaddr)
98 || strcmp(natp->nat_userid, player->userid))) {
99 pr("Last connection from: %s", ctime(&natp->nat_last_login));
101 natp->nat_last_login <= natp->nat_last_logout
102 ? ctime(&natp->nat_last_logout) : "?");
105 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
107 strcpy(natp->nat_userid, player->userid);
108 strcpy(natp->nat_hostname, player->hostname);
109 strcpy(natp->nat_hostaddr, player->hostaddr);
111 time(&natp->nat_last_login);
113 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
114 if (natp->nat_tgms == 1)
115 pr("You have a new telegram waiting ...\n");
117 pr("You have %s new telegrams waiting ...\n",
118 numstr(buf, natp->nat_tgms));
123 if (command() == 0 && !player->aborted)
128 /* #*# I put the following line in to prevent server crash -KHS */
129 natp = getnatp(player->cnum);
131 * randomly round up to the nearest minute,
132 * charging at least 15 seconds.
134 time(&natp->nat_last_logout);
135 secs = max(natp->nat_last_logout - player->lasttime, 15);
136 natp->nat_minused += secs / 60;
138 if (chance(secs / 60.0))
139 natp->nat_minused += 1;
148 char *redir; /* UTF-8 */
149 char scanspace[1024];
151 if (getcommand(player->combuf) < 0)
153 if (parse(player->combuf, player->argp, &player->condarg,
154 scanspace, &redir) < 0) {
155 pr("See \"info Syntax\"?\n");
157 /* XXX don't use alarm; use a scavenger thread */
158 /* DONT USE IT!!!! alarm and sleep may and dont work
160 /* alarm((unsigned int)60*60); 1 hour */
161 if (player->condarg != NULL)
162 for (x = 0; x < strlen(player->condarg); x++)
163 if (isupper(*(player->condarg + x)))
164 *(player->condarg + x) =
165 tolower(*(player->condarg + x));
166 if (dispatch(player->combuf, redir) < 0)
167 pr("Try \"list of commands\" or \"info\"\n");
180 if (player->state == PS_SHUTDOWN)
182 natp = getnatp(player->cnum);
183 if (io_error(player->iop) || io_eof(player->iop)) {
187 player->visitor = (natp->nat_stat & (STAT_NORM | STAT_GOD)) == 0;
188 if (player->dolcost != 0.0) {
189 if (player->dolcost > 100.0)
190 pr("That just cost you $%.2f\n", player->dolcost);
191 else if (player->dolcost < -100.0)
192 pr("You just made $%.2f\n", -player->dolcost);
193 if (natp->nat_money < player->dolcost && !player->broke) {
195 player->nstat &= ~MONEY;
196 pr("You are now broke; industries are on strike.\n");
197 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
199 player->nstat |= MONEY;
200 pr("You are no longer broke!\n");
202 natp->nat_money -= roundavg(player->dolcost);
203 player->dolcost = 0.0;
205 if (natp->nat_money < 0.0 && !player->broke) {
207 player->nstat &= ~MONEY;
208 pr("You are now broke; industries are on strike.\n");
210 if (player->broke && natp->nat_money > 0) {
212 player->nstat |= MONEY;
213 pr("You are no longer broke!\n");
216 getsect(natp->nat_xcap, natp->nat_ycap, §);
217 if ((sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT ||
218 sect.sct_type == SCT_SANCT) && sect.sct_own == player->cnum)
219 player->nstat |= CAP;
221 player->nstat &= ~CAP;
222 /* Ok, has the country owner reset his capital yet after it was sacked? */
223 if (natp->nat_flags & NF_SACKED)
224 player->nstat &= ~CAP; /* No capital yet */
225 player->ncomstat = player->nstat;
226 time(&player->curup);
227 minute = (player->curup - player->lasttime) / 60;
229 player->minleft -= minute;
230 if (player->minleft <= 0) {
232 * countdown timer "player->minleft" has expired.
233 * either day change, or hours restriction
235 daychange(player->curup);
236 if (!gamehours(player->curup)) {
237 pr("Empire hours restriction in force\n");
238 if ((natp->nat_stat & STAT_GOD) == 0) {
243 player->minleft = getminleft(player->curup, m_m_p_d);
245 player->lasttime += minute * 60;
246 natp->nat_minused += minute;
248 if ((player->nstat & NORM) && natp->nat_minused > m_m_p_d) {
249 pr("Max minutes per day limit exceeded.\n");
250 player->ncomstat = VIS;
252 if (player->btused) {
253 natp->nat_btu -= player->btused;
256 if (natp->nat_tgms > 0) {
257 if (!(natp->nat_flags & NF_INFORM)) {
258 if (natp->nat_tgms == 1)
259 pr("You have a new telegram waiting ...\n");
261 pr("You have %s new telegrams waiting ...\n",
262 numstr(buf, natp->nat_tgms));
266 if (natp->nat_ann > 0) {
267 if (natp->nat_ann == 1)
268 pr("You have a new announcement waiting ...\n");
270 pr("You have %s new announcements waiting ...\n",
271 numstr(buf, natp->nat_ann));
274 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
275 pr("You lost your capital... better designate one\n");
277 if (gamedown() && !player->god) {
285 * actually a command; redirection and piping ignored.
286 * XXX This whole mess should be redone; execute block should
287 * start with "exec start", and should end with "exec end".
288 * We'll wait until 1.2 I guess.
296 char *redir; /* UTF-8 */
297 char scanspace[1024];
302 p = getstarg(player->argp[1], "File? ", buf);
304 if (p == NULL || *p == '\0')
307 /* FIXME should use raw argument here, to support UTF-8 file names */
308 prexec(player->argp[1]);
310 while (!failed && status()) {
311 if (recvclient(buf, sizeof(buf)) < 0)
313 if (parse(buf, player->argp, &player->condarg,
314 scanspace, &redir) < 0) {
319 pr("\nExecute : %s\n", buf);
320 if (dispatch(buf, redir) < 0)
324 while (recvclient(buf, sizeof(buf)) >= 0) ;
327 pr("Execute : %s\n", failed ? "aborted" : "terminated");
336 char buf[MAXTELSIZE + 1]; /* UTF-8 */
338 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
342 pr ("Could not open motd.\n");
343 logerror("Could not open motd (%s).\n", motdfil);
347 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
348 logerror("bad header on login message (motdfil)");
352 if (tgm.tel_length >= (long)sizeof(buf)) {
353 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
357 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
358 logerror("bad length %ld on login message", tgm.tel_length);
362 buf[tgm.tel_length] = 0;
369 match_user(char *file, struct player *p)
376 if ((fp = fopen(file, "r")) == NULL) {
377 /*logerror("Cannot find file %s", file); */
381 while (!feof(fp) && !match) {
382 if (fgets(host, sizeof(host), fp) == NULL)
386 if (fgets(user, sizeof(user), fp) == NULL)
388 host[strlen(host) - 1] = '\0';
389 user[strlen(user) - 1] = '\0';
390 if (strstr(p->userid, user) &&
391 (strstr(p->hostaddr, host) ||
392 strstr(p->hostname, host)))
402 player->state = PS_SHUTDOWN;
407 praddr(struct player *p)
409 return prbuf("%s@%s", p->userid,
410 *p->hostname ? p->hostname : p->hostaddr);