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"
63 static int command(void);
64 static int status(void);
66 struct player *player;
69 player_main(struct player *p)
75 p->state = PS_PLAYING;
77 time(&player->lasttime);
81 if (init_nats() < 0) {
82 pr("Server confused, try again later\n");
85 natp = getnatp(player->cnum);
86 if (!gamehours(player->curup)) {
87 pr("Empire hours restriction in force\n");
88 if (natp->nat_stat != STAT_GOD)
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 != 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 if (dispatch(player->combuf, redir) < 0)
159 pr("Try \"list of commands\" or \"info\"\n");
172 if (player->state == PS_SHUTDOWN)
174 natp = getnatp(player->cnum);
175 if (io_error(player->iop) || io_eof(player->iop)) {
179 player->visitor = natp->nat_stat < STAT_SANCT;
180 if (player->dolcost != 0.0) {
181 if (player->dolcost > 100.0)
182 pr("That just cost you $%.2f\n", player->dolcost);
183 else if (player->dolcost < -100.0)
184 pr("You just made $%.2f\n", -player->dolcost);
185 if (natp->nat_money < player->dolcost && !player->broke) {
187 player->nstat &= ~MONEY;
188 pr("You are now broke; industries are on strike.\n");
189 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
191 player->nstat |= MONEY;
192 pr("You are no longer broke!\n");
194 natp->nat_money -= roundavg(player->dolcost);
195 player->dolcost = 0.0;
197 if (natp->nat_money < 0.0 && !player->broke) {
199 player->nstat &= ~MONEY;
200 pr("You are now broke; industries are on strike.\n");
202 if (player->broke && natp->nat_money > 0) {
204 player->nstat |= MONEY;
205 pr("You are no longer broke!\n");
208 getsect(natp->nat_xcap, natp->nat_ycap, §);
210 player->nstat &= ~CAP;
212 player->nstat |= CAP;
213 player->ncomstat = player->nstat;
215 player->ncomstat |= CAP | MONEY;
216 time(&player->curup);
217 minute = (player->curup - player->lasttime) / 60;
219 player->minleft -= minute;
220 if (player->minleft <= 0) {
222 * countdown timer "player->minleft" has expired.
223 * either day change, or hours restriction
225 daychange(player->curup);
226 if (!gamehours(player->curup)) {
227 pr("Empire hours restriction in force\n");
228 if (natp->nat_stat != STAT_GOD) {
233 player->minleft = getminleft(player->curup, m_m_p_d);
235 player->lasttime += minute * 60;
236 natp->nat_minused += minute;
238 if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
239 pr("Max minutes per day limit exceeded.\n");
240 player->ncomstat = VIS;
242 if (player->btused) {
243 natp->nat_btu -= player->btused;
246 if (natp->nat_tgms > 0) {
247 if (!(natp->nat_flags & NF_INFORM)) {
248 if (natp->nat_tgms == 1)
249 pr("You have a new telegram waiting ...\n");
251 pr("You have %s new telegrams waiting ...\n",
252 numstr(buf, natp->nat_tgms));
256 if (natp->nat_ann > 0) {
257 if (natp->nat_ann == 1)
258 pr("You have a new announcement waiting ...\n");
260 pr("You have %s new announcements waiting ...\n",
261 numstr(buf, natp->nat_ann));
264 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
265 pr("You lost your capital... better designate one\n");
267 if (gamedown() && !player->god) {
275 * actually a command; redirection and piping ignored.
276 * XXX This whole mess should be redone; execute block should
277 * start with "exec start", and should end with "exec end".
278 * We'll wait until 1.2 I guess.
286 char *redir; /* UTF-8 */
287 char scanspace[1024];
292 p = getstarg(player->argp[1], "File? ", buf);
294 if (p == NULL || *p == '\0')
297 /* FIXME should use raw argument here, to support UTF-8 file names */
298 prexec(player->argp[1]);
300 while (!failed && status()) {
301 if (recvclient(buf, sizeof(buf)) < 0)
303 if (parse(buf, player->argp, &player->condarg,
304 scanspace, &redir) < 0) {
309 pr("\nExecute : %s\n", buf);
310 if (dispatch(buf, redir) < 0)
314 while (recvclient(buf, sizeof(buf)) >= 0) ;
317 pr("Execute : %s\n", failed ? "aborted" : "terminated");
326 char buf[MAXTELSIZE + 1]; /* UTF-8 */
328 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
332 pr ("Could not open motd.\n");
333 logerror("Could not open motd (%s).\n", motdfil);
337 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
338 logerror("bad header on login message (motdfil)");
342 if (tgm.tel_length >= (long)sizeof(buf)) {
343 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
347 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
348 logerror("bad length %ld on login message", tgm.tel_length);
352 buf[tgm.tel_length] = 0;
361 player->state = PS_SHUTDOWN;
366 praddr(struct player *p)
368 return prbuf("%s@%s", p->userid,
369 *p->hostname ? p->hostname : p->hostaddr);