2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * player.c: Main command loop for a player
30 * Known contributors to this file:
36 #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);
78 if (init_nats() < 0) {
79 pr("Server confused, try again later\n");
82 natp = getnatp(player->cnum);
83 if (!gamehours(player->curup)) {
84 pr("Empire hours restriction in force\n");
85 if (natp->nat_stat != STAT_GOD)
88 daychange(player->curup);
89 if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
90 pr("Time exceeded today\n");
93 if (natp->nat_stat != STAT_VIS
94 && natp->nat_last_login
95 && (strcmp(natp->nat_hostaddr, player->hostaddr)
96 || strcmp(natp->nat_userid, player->userid))) {
97 pr("Last connection from: %s", ctime(&natp->nat_last_login));
99 natp->nat_last_login <= natp->nat_last_logout
100 ? ctime(&natp->nat_last_logout) : "?");
103 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
105 strcpy(natp->nat_userid, player->userid);
106 strcpy(natp->nat_hostname, player->hostname);
107 strcpy(natp->nat_hostaddr, player->hostaddr);
109 time(&natp->nat_last_login);
111 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
112 if (natp->nat_tgms == 1)
113 pr("You have a new telegram waiting ...\n");
115 pr("You have %s new telegrams waiting ...\n",
116 numstr(buf, natp->nat_tgms));
121 if (command() == 0 && !player->aborted)
126 /* #*# I put the following line in to prevent server crash -KHS */
127 natp = getnatp(player->cnum);
129 * randomly round up to the nearest minute,
130 * charging at least 15 seconds.
132 time(&natp->nat_last_logout);
133 secs = MAX(natp->nat_last_logout - player->lasttime, 15);
134 natp->nat_minused += secs / 60;
136 if (chance(secs / 60.0))
137 natp->nat_minused += 1;
146 char *redir; /* UTF-8 */
147 char scanspace[1024];
149 if (getcommand(player->combuf) < 0)
151 if (parse(player->combuf, player->argp, &player->condarg,
152 scanspace, &redir) < 0) {
153 pr("See \"info Syntax\"?\n");
155 /* XXX don't use alarm; use a scavenger thread */
156 /* DONT USE IT!!!! alarm and sleep may and dont work
158 /* alarm((unsigned int)60*60); 1 hour */
159 if (player->condarg != NULL)
160 for (x = 0; x < strlen(player->condarg); x++)
161 if (isupper(*(player->condarg + x)))
162 *(player->condarg + x) =
163 tolower(*(player->condarg + x));
164 if (dispatch(player->combuf, redir) < 0)
165 pr("Try \"list of commands\" or \"info\"\n");
178 if (player->state == PS_SHUTDOWN)
180 natp = getnatp(player->cnum);
181 if (io_error(player->iop) || io_eof(player->iop)) {
185 player->visitor = natp->nat_stat < STAT_SANCT;
186 if (player->dolcost != 0.0) {
187 if (player->dolcost > 100.0)
188 pr("That just cost you $%.2f\n", player->dolcost);
189 else if (player->dolcost < -100.0)
190 pr("You just made $%.2f\n", -player->dolcost);
191 if (natp->nat_money < player->dolcost && !player->broke) {
193 player->nstat &= ~MONEY;
194 pr("You are now broke; industries are on strike.\n");
195 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
197 player->nstat |= MONEY;
198 pr("You are no longer broke!\n");
200 natp->nat_money -= roundavg(player->dolcost);
201 player->dolcost = 0.0;
203 if (natp->nat_money < 0.0 && !player->broke) {
205 player->nstat &= ~MONEY;
206 pr("You are now broke; industries are on strike.\n");
208 if (player->broke && natp->nat_money > 0) {
210 player->nstat |= MONEY;
211 pr("You are no longer broke!\n");
214 getsect(natp->nat_xcap, natp->nat_ycap, §);
216 player->nstat &= ~CAP;
218 player->nstat |= CAP;
219 player->ncomstat = player->nstat;
221 player->ncomstat |= CAP | MONEY;
222 time(&player->curup);
223 minute = (player->curup - player->lasttime) / 60;
225 player->minleft -= minute;
226 if (player->minleft <= 0) {
228 * countdown timer "player->minleft" has expired.
229 * either day change, or hours restriction
231 daychange(player->curup);
232 if (!gamehours(player->curup)) {
233 pr("Empire hours restriction in force\n");
234 if (natp->nat_stat != STAT_GOD) {
239 player->minleft = getminleft(player->curup, m_m_p_d);
241 player->lasttime += minute * 60;
242 natp->nat_minused += minute;
244 if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
245 pr("Max minutes per day limit exceeded.\n");
246 player->ncomstat = VIS;
248 if (player->btused) {
249 natp->nat_btu -= player->btused;
252 if (natp->nat_tgms > 0) {
253 if (!(natp->nat_flags & NF_INFORM)) {
254 if (natp->nat_tgms == 1)
255 pr("You have a new telegram waiting ...\n");
257 pr("You have %s new telegrams waiting ...\n",
258 numstr(buf, natp->nat_tgms));
262 if (natp->nat_ann > 0) {
263 if (natp->nat_ann == 1)
264 pr("You have a new announcement waiting ...\n");
266 pr("You have %s new announcements waiting ...\n",
267 numstr(buf, natp->nat_ann));
270 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
271 pr("You lost your capital... better designate one\n");
273 if (gamedown() && !player->god) {
281 * actually a command; redirection and piping ignored.
282 * XXX This whole mess should be redone; execute block should
283 * start with "exec start", and should end with "exec end".
284 * We'll wait until 1.2 I guess.
292 char *redir; /* UTF-8 */
293 char scanspace[1024];
298 p = getstarg(player->argp[1], "File? ", buf);
300 if (p == NULL || *p == '\0')
303 /* FIXME should use raw argument here, to support UTF-8 file names */
304 prexec(player->argp[1]);
306 while (!failed && status()) {
307 if (recvclient(buf, sizeof(buf)) < 0)
309 if (parse(buf, player->argp, &player->condarg,
310 scanspace, &redir) < 0) {
315 pr("\nExecute : %s\n", buf);
316 if (dispatch(buf, redir) < 0)
320 while (recvclient(buf, sizeof(buf)) >= 0) ;
323 pr("Execute : %s\n", failed ? "aborted" : "terminated");
332 char buf[MAXTELSIZE + 1]; /* UTF-8 */
334 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
338 pr ("Could not open motd.\n");
339 logerror("Could not open motd (%s).\n", motdfil);
343 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
344 logerror("bad header on login message (motdfil)");
348 if (tgm.tel_length >= (long)sizeof(buf)) {
349 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
353 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
354 logerror("bad length %ld on login message", tgm.tel_length);
358 buf[tgm.tel_length] = 0;
367 player->state = PS_SHUTDOWN;
372 praddr(struct player *p)
374 return prbuf("%s@%s", p->userid,
375 *p->hostname ? p->hostname : p->hostaddr);