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_stat != VIS
97 && natp->nat_last_login
98 && (strcmp(natp->nat_hostaddr, player->hostaddr)
99 || strcmp(natp->nat_userid, player->userid))) {
100 pr("Last connection from: %s", ctime(&natp->nat_last_login));
102 natp->nat_last_login <= natp->nat_last_logout
103 ? ctime(&natp->nat_last_logout) : "?");
106 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
108 strcpy(natp->nat_userid, player->userid);
109 strcpy(natp->nat_hostname, player->hostname);
110 strcpy(natp->nat_hostaddr, player->hostaddr);
112 time(&natp->nat_last_login);
114 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
115 if (natp->nat_tgms == 1)
116 pr("You have a new telegram waiting ...\n");
118 pr("You have %s new telegrams waiting ...\n",
119 numstr(buf, natp->nat_tgms));
124 if (command() == 0 && !player->aborted)
129 /* #*# I put the following line in to prevent server crash -KHS */
130 natp = getnatp(player->cnum);
132 * randomly round up to the nearest minute,
133 * charging at least 15 seconds.
135 time(&natp->nat_last_logout);
136 secs = max(natp->nat_last_logout - player->lasttime, 15);
137 natp->nat_minused += secs / 60;
139 if (chance(secs / 60.0))
140 natp->nat_minused += 1;
149 char *redir; /* UTF-8 */
150 char scanspace[1024];
152 if (getcommand(player->combuf) < 0)
154 if (parse(player->combuf, player->argp, &player->condarg,
155 scanspace, &redir) < 0) {
156 pr("See \"info Syntax\"?\n");
158 /* XXX don't use alarm; use a scavenger thread */
159 /* DONT USE IT!!!! alarm and sleep may and dont work
161 /* alarm((unsigned int)60*60); 1 hour */
162 if (player->condarg != NULL)
163 for (x = 0; x < strlen(player->condarg); x++)
164 if (isupper(*(player->condarg + x)))
165 *(player->condarg + x) =
166 tolower(*(player->condarg + x));
167 if (dispatch(player->combuf, redir) < 0)
168 pr("Try \"list of commands\" or \"info\"\n");
181 if (player->state == PS_SHUTDOWN)
183 natp = getnatp(player->cnum);
184 if (io_error(player->iop) || io_eof(player->iop)) {
188 player->visitor = (natp->nat_stat & (STAT_NORM | STAT_GOD)) == 0;
189 if (player->dolcost != 0.0) {
190 if (player->dolcost > 100.0)
191 pr("That just cost you $%.2f\n", player->dolcost);
192 else if (player->dolcost < -100.0)
193 pr("You just made $%.2f\n", -player->dolcost);
194 if (natp->nat_money < player->dolcost && !player->broke) {
196 player->nstat &= ~MONEY;
197 pr("You are now broke; industries are on strike.\n");
198 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
200 player->nstat |= MONEY;
201 pr("You are no longer broke!\n");
203 natp->nat_money -= roundavg(player->dolcost);
204 player->dolcost = 0.0;
206 if (natp->nat_money < 0.0 && !player->broke) {
208 player->nstat &= ~MONEY;
209 pr("You are now broke; industries are on strike.\n");
211 if (player->broke && natp->nat_money > 0) {
213 player->nstat |= MONEY;
214 pr("You are no longer broke!\n");
217 getsect(natp->nat_xcap, natp->nat_ycap, §);
218 if ((sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT ||
219 sect.sct_type == SCT_SANCT) && sect.sct_own == player->cnum)
220 player->nstat |= CAP;
222 player->nstat &= ~CAP;
223 /* Ok, has the country owner reset his capital yet after it was sacked? */
224 if (natp->nat_flags & NF_SACKED)
225 player->nstat &= ~CAP; /* No capital yet */
226 player->ncomstat = player->nstat;
227 time(&player->curup);
228 minute = (player->curup - player->lasttime) / 60;
230 player->minleft -= minute;
231 if (player->minleft <= 0) {
233 * countdown timer "player->minleft" has expired.
234 * either day change, or hours restriction
236 daychange(player->curup);
237 if (!gamehours(player->curup)) {
238 pr("Empire hours restriction in force\n");
239 if ((natp->nat_stat & STAT_GOD) == 0) {
244 player->minleft = getminleft(player->curup, m_m_p_d);
246 player->lasttime += minute * 60;
247 natp->nat_minused += minute;
249 if ((player->nstat & NORM) && natp->nat_minused > m_m_p_d) {
250 pr("Max minutes per day limit exceeded.\n");
251 player->ncomstat = VIS;
253 if (player->btused) {
254 natp->nat_btu -= player->btused;
257 if (natp->nat_tgms > 0) {
258 if (!(natp->nat_flags & NF_INFORM)) {
259 if (natp->nat_tgms == 1)
260 pr("You have a new telegram waiting ...\n");
262 pr("You have %s new telegrams waiting ...\n",
263 numstr(buf, natp->nat_tgms));
267 if (natp->nat_ann > 0) {
268 if (natp->nat_ann == 1)
269 pr("You have a new announcement waiting ...\n");
271 pr("You have %s new announcements waiting ...\n",
272 numstr(buf, natp->nat_ann));
275 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
276 pr("You lost your capital... better designate one\n");
278 if (gamedown() && !player->god) {
286 * actually a command; redirection and piping ignored.
287 * XXX This whole mess should be redone; execute block should
288 * start with "exec start", and should end with "exec end".
289 * We'll wait until 1.2 I guess.
297 char *redir; /* UTF-8 */
298 char scanspace[1024];
303 p = getstarg(player->argp[1], "File? ", buf);
305 if (p == NULL || *p == '\0')
308 /* FIXME should use raw argument here, to support UTF-8 file names */
309 prexec(player->argp[1]);
311 while (!failed && status()) {
312 if (recvclient(buf, sizeof(buf)) < 0)
314 if (parse(buf, player->argp, &player->condarg,
315 scanspace, &redir) < 0) {
320 pr("\nExecute : %s\n", buf);
321 if (dispatch(buf, redir) < 0)
325 while (recvclient(buf, sizeof(buf)) >= 0) ;
328 pr("Execute : %s\n", failed ? "aborted" : "terminated");
337 char buf[MAXTELSIZE + 1]; /* UTF-8 */
339 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
343 pr ("Could not open motd.\n");
344 logerror("Could not open motd (%s).\n", motdfil);
348 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
349 logerror("bad header on login message (motdfil)");
353 if (tgm.tel_length >= (long)sizeof(buf)) {
354 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
358 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
359 logerror("bad length %ld on login message", tgm.tel_length);
363 buf[tgm.tel_length] = 0;
370 match_user(char *file, struct player *p)
377 if ((fp = fopen(file, "r")) == NULL) {
378 /*logerror("Cannot find file %s", file); */
382 while (!feof(fp) && !match) {
383 if (fgets(host, sizeof(host), fp) == NULL)
387 if (fgets(user, sizeof(user), fp) == NULL)
389 host[strlen(host) - 1] = '\0';
390 user[strlen(user) - 1] = '\0';
391 if (strstr(p->userid, user) &&
392 (strstr(p->hostaddr, host) ||
393 strstr(p->hostname, host)))
403 player->state = PS_SHUTDOWN;
408 praddr(struct player *p)
410 return prbuf("%s@%s", p->userid,
411 *p->hostname ? p->hostname : p->hostaddr);