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:
37 #include "prototypes.h"
48 #include "empthread.h"
63 static int status(void);
65 struct player *player;
68 player_main(struct player *p)
74 p->state = PS_PLAYING;
76 time(&player->lasttime);
79 if (init_nats() < 0) {
80 pr("Server confused, try again later\n");
83 natp = getnatp(player->cnum);
84 if (!gamehours(player->curup)) {
85 pr("Empire hours restriction in force\n");
86 if (natp->nat_stat != STAT_GOD)
89 daychange(player->curup);
90 if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
91 pr("Time exceeded today\n");
94 if (natp->nat_stat != STAT_VIS
95 && natp->nat_last_login
96 && (strcmp(natp->nat_hostaddr, player->hostaddr)
97 || strcmp(natp->nat_userid, player->userid))) {
98 pr("Last connection from: %s", ctime(&natp->nat_last_login));
100 natp->nat_last_login <= natp->nat_last_logout
101 ? ctime(&natp->nat_last_logout) : "?");
104 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
106 strcpy(natp->nat_userid, player->userid);
107 strcpy(natp->nat_hostname, player->hostname);
108 strcpy(natp->nat_hostaddr, player->hostaddr);
110 time(&natp->nat_last_login);
112 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
113 if (natp->nat_tgms == 1)
114 pr("You have a new telegram waiting ...\n");
116 pr("You have %s new telegrams waiting ...\n",
117 numstr(buf, natp->nat_tgms));
122 if (command() == 0 && !player->aborted)
127 /* #*# I put the following line in to prevent server crash -KHS */
128 natp = getnatp(player->cnum);
130 * randomly round up to the nearest minute,
131 * charging at least 15 seconds.
133 time(&natp->nat_last_logout);
134 secs = MAX(natp->nat_last_logout - player->lasttime, 15);
135 natp->nat_minused += secs / 60;
137 if (chance(secs / 60.0))
138 natp->nat_minused += 1;
147 char *redir; /* UTF-8 */
148 char scanspace[1024];
150 if (getcommand(player->combuf) < 0)
152 if (parse(player->combuf, player->argp, &player->condarg,
153 scanspace, &redir) < 0) {
154 pr("See \"info Syntax\"?\n");
156 /* XXX don't use alarm; use a scavenger thread */
157 /* DONT USE IT!!!! alarm and sleep may and dont work
159 /* alarm((unsigned int)60*60); 1 hour */
160 if (player->condarg != NULL)
161 for (x = 0; x < strlen(player->condarg); x++)
162 if (isupper(*(player->condarg + x)))
163 *(player->condarg + x) =
164 tolower(*(player->condarg + x));
165 if (dispatch(player->combuf, redir) < 0)
166 pr("Try \"list of commands\" or \"info\"\n");
179 if (player->state == PS_SHUTDOWN)
181 natp = getnatp(player->cnum);
182 if (io_error(player->iop) || io_eof(player->iop)) {
186 player->visitor = natp->nat_stat < STAT_SANCT;
187 if (player->dolcost != 0.0) {
188 if (player->dolcost > 100.0)
189 pr("That just cost you $%.2f\n", player->dolcost);
190 else if (player->dolcost < -100.0)
191 pr("You just made $%.2f\n", -player->dolcost);
192 if (natp->nat_money < player->dolcost && !player->broke) {
194 player->nstat &= ~MONEY;
195 pr("You are now broke; industries are on strike.\n");
196 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
198 player->nstat |= MONEY;
199 pr("You are no longer broke!\n");
201 natp->nat_money -= roundavg(player->dolcost);
202 player->dolcost = 0.0;
204 if (natp->nat_money < 0.0 && !player->broke) {
206 player->nstat &= ~MONEY;
207 pr("You are now broke; industries are on strike.\n");
209 if (player->broke && natp->nat_money > 0) {
211 player->nstat |= MONEY;
212 pr("You are no longer broke!\n");
215 getsect(natp->nat_xcap, natp->nat_ycap, §);
217 player->nstat &= ~CAP;
219 player->nstat |= CAP;
220 player->ncomstat = player->nstat;
222 player->ncomstat |= CAP | MONEY;
223 time(&player->curup);
224 minute = (player->curup - player->lasttime) / 60;
226 player->minleft -= minute;
227 if (player->minleft <= 0) {
229 * countdown timer "player->minleft" has expired.
230 * either day change, or hours restriction
232 daychange(player->curup);
233 if (!gamehours(player->curup)) {
234 pr("Empire hours restriction in force\n");
235 if (natp->nat_stat != STAT_GOD) {
240 player->minleft = getminleft(player->curup, m_m_p_d);
242 player->lasttime += minute * 60;
243 natp->nat_minused += minute;
245 if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
246 pr("Max minutes per day limit exceeded.\n");
247 player->ncomstat = VIS;
249 if (player->btused) {
250 natp->nat_btu -= player->btused;
253 if (natp->nat_tgms > 0) {
254 if (!(natp->nat_flags & NF_INFORM)) {
255 if (natp->nat_tgms == 1)
256 pr("You have a new telegram waiting ...\n");
258 pr("You have %s new telegrams waiting ...\n",
259 numstr(buf, natp->nat_tgms));
263 if (natp->nat_ann > 0) {
264 if (natp->nat_ann == 1)
265 pr("You have a new announcement waiting ...\n");
267 pr("You have %s new announcements waiting ...\n",
268 numstr(buf, natp->nat_ann));
271 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
272 pr("You lost your capital... better designate one\n");
274 if (gamedown() && !player->god) {
282 * actually a command; redirection and piping ignored.
283 * XXX This whole mess should be redone; execute block should
284 * start with "exec start", and should end with "exec end".
285 * We'll wait until 1.2 I guess.
293 char *redir; /* UTF-8 */
294 char scanspace[1024];
299 p = getstarg(player->argp[1], "File? ", buf);
301 if (p == NULL || *p == '\0')
304 /* FIXME should use raw argument here, to support UTF-8 file names */
305 prexec(player->argp[1]);
307 while (!failed && status()) {
308 if (recvclient(buf, sizeof(buf)) < 0)
310 if (parse(buf, player->argp, &player->condarg,
311 scanspace, &redir) < 0) {
316 pr("\nExecute : %s\n", buf);
317 if (dispatch(buf, redir) < 0)
321 while (recvclient(buf, sizeof(buf)) >= 0) ;
324 pr("Execute : %s\n", failed ? "aborted" : "terminated");
333 char buf[MAXTELSIZE + 1]; /* UTF-8 */
335 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
339 pr ("Could not open motd.\n");
340 logerror("Could not open motd (%s).\n", motdfil);
344 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
345 logerror("bad header on login message (motdfil)");
349 if (tgm.tel_length >= (long)sizeof(buf)) {
350 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
354 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
355 logerror("bad length %ld on login message", tgm.tel_length);
359 buf[tgm.tel_length] = 0;
368 player->state = PS_SHUTDOWN;
373 praddr(struct player *p)
375 return prbuf("%s@%s", p->userid,
376 *p->hostname ? p->hostname : p->hostaddr);