2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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"
49 #include "empthread.h"
62 struct player *player;
66 player_main(struct player *p)
68 extern s_char *authfil;
74 p->state = PS_PLAYING;
76 time(&player->lasttime);
77 (void)time(&player->curup);
78 showvers(CLIENTPROTO);
82 natp = getnatp(player->cnum);
83 if (player->god && !match_user(authfil, player)) {
84 logerror("NON-AUTHed Login attempted by %s", praddr(player));
85 pr("You're not a deity!\n");
88 if (!gamehours(player->curup, hour)) {
89 pr("Empire hours restriction in force\n");
90 if ((natp->nat_stat & STAT_GOD) == 0)
93 daychange(player->curup);
94 if ((player->minleft = getminleft(player->curup, hour, &m_m_p_d)) <= 0) {
95 pr("Time exceeded today\n");
98 if ((*natp->nat_hostaddr &&
100 strcmp(natp->nat_hostaddr, player->hostaddr)) ||
101 (*natp->nat_userid &&
102 *player->userid && strcmp(natp->nat_userid, player->userid))) {
103 if (natp->nat_stat != VIS) {
104 pr("Last connection from: %s", ctime(&natp->nat_last_login));
105 pr(" to: %s", natp->nat_last_login <
106 natp->nat_last_logout ? ctime(&natp->
107 nat_last_logout) : "?");
109 *natp->nat_userid ? natp->nat_userid : (s_char *)"nobody",
110 *natp->nat_hostname ? natp->nat_hostname : *natp->
111 nat_hostaddr ? natp->nat_hostaddr : (s_char *)"nowhere");
115 strcpy(natp->nat_userid, player->userid);
117 strcpy(natp->nat_userid, "nobody");
119 if (*player->hostname)
120 strcpy(natp->nat_hostname, player->hostname);
122 strcpy(natp->nat_hostname, "nowhere");
124 if (*player->hostaddr)
125 strcpy(natp->nat_hostaddr, player->hostaddr);
127 time(&natp->nat_last_login);
128 natp->nat_connected = 1;
130 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
131 if (natp->nat_tgms == 1)
132 pr("You have a new telegram waiting ...\n");
134 pr("You have %s new telegrams waiting ...\n",
135 numstr(buf, natp->nat_tgms));
140 if (command() == 0 && !player->aborted)
144 /* #*# I put the following line in to prevent server crash -KHS */
145 natp = getnatp(player->cnum);
147 * randomly round up to the nearest minute,
148 * charging at least 15 seconds.
150 time(&natp->nat_last_logout);
151 secs = max(natp->nat_last_logout - player->lasttime, 15);
152 natp->nat_minused += secs / 60;
154 if (chance(secs / 60.0))
155 natp->nat_minused += 1;
156 natp->nat_connected = 0;
164 register unsigned int x;
167 s_char scanspace[1024];
169 if (getcommand(player->combuf) < 0)
171 if (parse(player->combuf, player->argp, &player->condarg,
172 scanspace, &redir) < 0) {
173 pr("See \"info Syntax\"?\n");
175 /* XXX don't use alarm; use a scavenger thread */
176 /* DONT USE IT!!!! alarm and sleep may and dont work
178 /* alarm((unsigned int)60*60); 1 hour */
179 if (player->condarg != (s_char *)0)
180 for (x = 0; x < strlen(player->condarg); x++)
181 if (isupper(*(player->condarg + x)))
182 *(player->condarg + x) =
183 tolower(*(player->condarg + x));
184 if (dispatch(player->combuf, redir) < 0)
185 pr("Try \"list of commands\" or \"info\"\n");
199 if (player->state == PS_SHUTDOWN)
201 natp = getnatp(player->cnum);
202 if (io_error(player->iop) || io_eof(player->iop)) {
206 player->visitor = (natp->nat_stat & (STAT_NORM | STAT_GOD)) == 0;
207 if (player->dolcost != 0.0) {
208 if (player->dolcost > 100.0)
209 pr("That just cost you $%.2f\n", player->dolcost);
210 else if (player->dolcost < -100.0)
211 pr("You just made $%.2f\n", -player->dolcost);
212 if (natp->nat_money < player->dolcost && !player->broke) {
214 player->nstat &= ~MONEY;
215 pr("You are now broke; industries are on strike.\n");
216 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
218 player->nstat |= MONEY;
219 pr("You are no longer broke!\n");
221 natp->nat_money -= roundavg(player->dolcost);
222 player->dolcost = 0.0;
224 if (natp->nat_money < 0.0 && !player->broke) {
226 player->nstat &= ~MONEY;
227 pr("You are now broke; industries are on strike.\n");
229 if (player->broke && natp->nat_money > 0) {
231 player->nstat |= MONEY;
232 pr("You are no longer broke!\n");
235 getsect(natp->nat_xcap, natp->nat_ycap, §);
236 if ((sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT ||
237 sect.sct_type == SCT_SANCT) && sect.sct_own == player->cnum)
238 player->nstat |= CAP;
240 player->nstat &= ~CAP;
241 /* Ok, has the country owner reset his capital yet after it was sacked? */
242 if (natp->nat_flags & NF_SACKED)
243 player->nstat &= ~CAP; /* No capital yet */
244 player->ncomstat = player->nstat;
245 (void)time(&player->curup);
246 minute = (player->curup - player->lasttime) / 60;
248 player->minleft -= minute;
249 if (player->minleft <= 0) {
251 * countdown timer "player->minleft" has expired.
252 * either day change, or hours restriction
254 daychange(player->curup);
255 if (!gamehours(player->curup, hour)) {
256 pr("Empire hours restriction in force\n");
257 if ((natp->nat_stat & STAT_GOD) == 0) {
262 player->minleft = getminleft(player->curup, hour, &m_m_p_d);
264 player->lasttime += minute * 60;
265 natp->nat_minused += minute;
267 if ((player->nstat & NORM) && natp->nat_minused > m_m_p_d) {
268 pr("Max minutes per day limit exceeded.\n");
269 player->ncomstat = VIS;
271 if (player->btused) {
272 natp->nat_btu -= player->btused;
275 if (natp->nat_tgms > 0) {
276 if (!(natp->nat_flags & NF_INFORM)) {
277 if (natp->nat_tgms == 1)
278 pr("You have a new telegram waiting ...\n");
280 pr("You have %s new telegrams waiting ...\n",
281 numstr(buf, natp->nat_tgms));
285 if (natp->nat_ann > 0) {
286 if (natp->nat_ann == 1)
287 pr("You have a new announcement waiting ...\n");
289 pr("You have %s new announcements waiting ...\n",
290 numstr(buf, natp->nat_ann));
293 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
294 pr("You lost your capital... better designate one\n");
296 if (gamedown() && !player->god) {
304 * actually a command; redirection and piping ignored.
305 * XXX This whole mess should be redone; execute block should
306 * start with "exec start", and should end with "exec end".
307 * We'll wait until 1.2 I guess.
316 s_char scanspace[1024];
321 p = getstarg(player->argp[1], "File? ", buf);
323 if (p == (s_char *)0 || p == '\0')
326 prexec(player->argp[1]);
327 while (!abort && status()) {
328 if (recvclient(buf, sizeof(buf)) < 0)
330 if (parse(buf, player->argp, &player->condarg,
331 scanspace, &redir) < 0) {
336 pr("\nExecute : %s\n", buf);
337 if (dispatch(buf, redir) < 0)
341 while (recvclient(buf, sizeof(buf)) >= 0) ;
344 pr("Execute : %s\n", abort ? "aborted" : "terminated");
351 extern s_char *upfil;
354 s_char buf[MAXTELSIZE];
357 if ((upf = open(upfil, O_RDONLY, 0)) < 0)
359 if ((upf = open(upfil, O_RDONLY | O_BINARY, 0)) < 0)
362 if (read(upf, (s_char *)&tgm, sizeof(tgm)) != sizeof(tgm)) {
363 logerror("bad header on login message (upfil)");
367 if (read(upf, buf, tgm.tel_length) != tgm.tel_length) {
368 logerror("bad length %d on login message", tgm.tel_length);
372 if (tgm.tel_length >= (long)sizeof(buf))
373 tgm.tel_length = sizeof(buf) - 1;
374 buf[tgm.tel_length] = 0;
381 match_user(char *file, struct player *player)
388 if ((fp = fopen(file, "r")) == NULL) {
389 /*logerror("Cannot find file %s", file); */
393 while (!feof(fp) && !match) {
394 if (fgets(host, sizeof(host) - 1, fp) == NULL)
398 if (fgets(user, sizeof(user) - 1, fp) == NULL)
400 host[strlen(host) - 1] = '\0';
401 user[strlen(user) - 1] = '\0';
402 if (strstr(player->userid, user) &&
403 (strstr(player->hostaddr, host) ||
404 strstr(player->hostname, host)))
414 player->state = PS_SHUTDOWN;
419 praddr(struct player *player)
421 return prbuf("%s@%s", player->userid,
422 *player->hostname ? player->hostname : player->hostaddr);