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);
84 !match_user(authfil, player)) {
85 logerror("NON-AUTHed Login attempted by %s",
87 pr("You're not a deity!\n");
90 if (!gamehours(player->curup, hour)) {
91 pr("Empire hours restriction in force\n");
92 if ((natp->nat_stat & STAT_GOD) == 0)
95 daychange(player->curup);
96 if ((player->minleft = getminleft(player->curup, hour, &m_m_p_d)) <= 0){
97 pr("Time exceeded today\n");
100 if ((*natp->nat_hostaddr &&
102 strcmp(natp->nat_hostaddr, player->hostaddr)) ||
103 (*natp->nat_userid &&
105 strcmp(natp->nat_userid, player->userid))) {
106 if (natp->nat_stat != VIS) {
107 pr("Last connection from: %s", ctime(&natp->nat_last_login));
108 pr(" to: %s", natp->nat_last_login <
109 natp->nat_last_logout ? ctime(&natp->nat_last_logout):"?");
111 *natp->nat_userid?natp->nat_userid : (s_char *)"nobody",
112 *natp->nat_hostname?natp->nat_hostname:
113 *natp->nat_hostaddr?natp->nat_hostaddr:(s_char *)"nowhere");
117 strcpy(natp->nat_userid, player->userid);
119 strcpy(natp->nat_userid, "nobody");
121 if (*player->hostname)
122 strcpy(natp->nat_hostname, player->hostname);
124 strcpy(natp->nat_hostname, "nowhere");
126 if (*player->hostaddr)
127 strcpy(natp->nat_hostaddr, player->hostaddr);
129 time(&natp->nat_last_login);
130 natp->nat_connected = 1;
132 if (natp->nat_flags & NF_INFORM &&
133 natp->nat_tgms > 0) {
134 if (natp->nat_tgms == 1)
135 pr("You have a new telegram waiting ...\n");
137 pr("You have %s new telegrams waiting ...\n",
138 numstr(buf, natp->nat_tgms));
143 if (command() == 0 && !player->aborted)
147 /* #*# I put the following line in to prevent server crash -KHS */
148 natp = getnatp(player->cnum);
150 * randomly round up to the nearest minute,
151 * charging at least 15 seconds.
153 time(&natp->nat_last_logout);
154 secs = max(natp->nat_last_logout - player->lasttime, 15);
155 natp->nat_minused += secs / 60;
157 if (chance(secs / 60.0))
158 natp->nat_minused += 1;
159 natp->nat_connected = 0;
167 register unsigned int x;
170 s_char scanspace[1024];
172 if (getcommand(player->combuf) < 0)
174 if (parse(player->combuf, player->argp, &player->condarg,
175 scanspace, &redir) < 0) {
176 pr("See \"info Syntax\"?\n");
178 /* XXX don't use alarm; use a scavenger thread */
179 /* DONT USE IT!!!! alarm and sleep may and dont work
181 /* alarm((unsigned int)60*60); 1 hour */
182 if (player->condarg != (s_char *)0)
183 for(x=0;x<strlen(player->condarg);x++)
184 if (isupper(*(player->condarg+x)))
185 *(player->condarg+x) = tolower(*(player->condarg+x));
186 if (dispatch(player->combuf, redir) < 0)
187 pr("Try \"list of commands\" or \"info\"\n");
201 if (player->state == PS_SHUTDOWN)
203 natp = getnatp(player->cnum);
204 if (io_error(player->iop) || io_eof(player->iop)) {
208 player->visitor = (natp->nat_stat & (STAT_NORM|STAT_GOD)) == 0;
209 if (player->dolcost != 0.0) {
210 if (player->dolcost > 100.0)
211 pr("That just cost you $%.2f\n", player->dolcost);
212 else if (player->dolcost < -100.0)
213 pr("You just made $%.2f\n", -player->dolcost);
214 if (natp->nat_money < player->dolcost && !player->broke) {
216 player->nstat &= ~MONEY;
217 pr("You are now broke; industries are on strike.\n");
218 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
220 player->nstat |= MONEY;
221 pr("You are no longer broke!\n");
223 natp->nat_money -= roundavg(player->dolcost);
224 player->dolcost = 0.0;
226 if (natp->nat_money < 0.0 && !player->broke) {
228 player->nstat &= ~MONEY;
229 pr("You are now broke; industries are on strike.\n");
231 if (player->broke && natp->nat_money > 0) {
233 player->nstat |= MONEY;
234 pr("You are no longer broke!\n");
237 getsect(natp->nat_xcap, natp->nat_ycap, §);
238 if ((sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT ||
239 sect.sct_type == SCT_SANCT) &&
240 sect.sct_own == player->cnum)
241 player->nstat |= CAP;
243 player->nstat &= ~CAP;
244 /* Ok, has the country owner reset his capital yet after it was sacked? */
245 if (natp->nat_flags & NF_SACKED)
246 player->nstat &= ~CAP; /* No capital yet */
247 player->ncomstat = player->nstat;
248 (void) time(&player->curup);
249 minute = (player->curup - player->lasttime) / 60;
251 player->minleft -= minute;
252 if (player->minleft <= 0) {
254 * countdown timer "player->minleft" has expired.
255 * either day change, or hours restriction
257 daychange(player->curup);
258 if (!gamehours(player->curup, hour)) {
259 pr("Empire hours restriction in force\n");
260 if ((natp->nat_stat & STAT_GOD) == 0) {
265 player->minleft = getminleft(player->curup, hour, &m_m_p_d);
267 player->lasttime += minute * 60;
268 natp->nat_minused += minute;
270 if ((player->nstat & NORM) && natp->nat_minused > m_m_p_d) {
271 pr("Max minutes per day limit exceeded.\n");
272 player->ncomstat = VIS;
274 if (player->btused) {
275 natp->nat_btu -= player->btused;
278 if (natp->nat_tgms > 0) {
279 if (!(natp->nat_flags & NF_INFORM)) {
280 if (natp->nat_tgms == 1)
281 pr("You have a new telegram waiting ...\n");
283 pr("You have %s new telegrams waiting ...\n",
284 numstr(buf, natp->nat_tgms));
288 if (natp->nat_ann > 0) {
289 if (natp->nat_ann == 1)
290 pr("You have a new announcement waiting ...\n");
292 pr("You have %s new announcements waiting ...\n",
293 numstr(buf, natp->nat_ann));
296 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
297 pr("You lost your capital... better designate one\n");
299 if (gamedown() && !player->god) {
307 * actually a command; redirection and piping ignored.
308 * XXX This whole mess should be redone; execute block should
309 * start with "exec start", and should end with "exec end".
310 * We'll wait until 1.2 I guess.
319 s_char scanspace[1024];
324 p = getstarg (player->argp[1], "File? ", buf);
326 if (p == (s_char *) 0 || p == '\0')
329 prexec(player->argp[1]);
330 while (!abort && status()) {
331 if (recvclient(buf, sizeof(buf)) < 0)
333 if (parse(buf, player->argp, &player->condarg,
334 scanspace, &redir) < 0) {
339 pr("\nExecute : %s\n", buf);
340 if (dispatch(buf, redir) < 0)
344 while (recvclient(buf, sizeof(buf)) >= 0)
348 pr("Execute : %s\n", abort ? "aborted" : "terminated");
355 extern s_char *upfil;
358 s_char buf[MAXTELSIZE];
361 if ((upf = open(upfil, O_RDONLY, 0)) < 0)
363 if ((upf = open(upfil, O_RDONLY|O_BINARY, 0)) < 0)
366 if (read(upf, (s_char *) &tgm, sizeof(tgm)) != sizeof(tgm)) {
367 logerror("bad header on login message (upfil)");
371 if (read(upf, buf, tgm.tel_length) != tgm.tel_length) {
372 logerror("bad length %d on login message", tgm.tel_length);
376 if (tgm.tel_length >= (long)sizeof(buf))
377 tgm.tel_length = sizeof(buf)-1;
378 buf[tgm.tel_length] = 0;
384 int match_user(char *file, struct player *player)
391 if ((fp = fopen(file, "r")) == NULL) {
392 /*logerror("Cannot find file %s", file);*/
396 while (!feof(fp) && !match) {
397 if (fgets(host, sizeof(host)-1, fp) == NULL)
399 if (host[0] == '#') continue;
400 if (fgets(user, sizeof(user)-1, fp) == NULL)
402 host[strlen(host)-1] = '\0';
403 user[strlen(user)-1] = '\0';
404 if (strstr(player->userid, user) &&
405 (strstr(player->hostaddr, host) ||
406 strstr(player->hostname, host)))
416 player->state = PS_SHUTDOWN;
421 praddr(struct player *player)
423 return prbuf("%s@%s", player->userid,
424 *player->hostname?player->hostname:player->hostaddr);