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:
44 #include "empthread.h"
53 #include "prototypes.h"
58 static int command(void);
59 static int status(void);
61 struct player *player;
64 player_main(struct player *p)
70 p->state = PS_PLAYING;
72 time(&player->lasttime);
75 if (init_nats() < 0) {
76 pr("Server confused, try again later\n");
79 natp = getnatp(player->cnum);
80 if (!gamehours(player->curup)) {
81 pr("Empire hours restriction in force\n");
82 if (natp->nat_stat != STAT_GOD)
85 daychange(player->curup);
86 if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
87 pr("Time exceeded today\n");
90 if (natp->nat_stat != STAT_VIS
91 && natp->nat_last_login
92 && (strcmp(natp->nat_hostaddr, player->hostaddr)
93 || strcmp(natp->nat_userid, player->userid))) {
94 pr("Last connection from: %s", ctime(&natp->nat_last_login));
96 natp->nat_last_login <= natp->nat_last_logout
97 ? ctime(&natp->nat_last_logout) : "?");
100 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
102 strcpy(natp->nat_userid, player->userid);
103 strcpy(natp->nat_hostname, player->hostname);
104 strcpy(natp->nat_hostaddr, player->hostaddr);
106 time(&natp->nat_last_login);
109 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
110 if (natp->nat_tgms == 1)
111 pr("You have a new telegram waiting ...\n");
113 pr("You have %s new telegrams waiting ...\n",
114 numstr(buf, natp->nat_tgms));
119 if (command() == 0 && !player->aborted)
124 /* #*# I put the following line in to prevent server crash -KHS */
125 natp = getnatp(player->cnum);
127 * randomly round up to the nearest minute,
128 * charging at least 15 seconds.
130 time(&natp->nat_last_logout);
131 secs = MAX(natp->nat_last_logout - player->lasttime, 15);
132 natp->nat_minused += secs / 60;
134 if (chance(secs / 60.0))
135 natp->nat_minused += 1;
144 char *redir; /* UTF-8 */
145 char scanspace[1024];
147 if (getcommand(player->combuf) < 0)
149 if (parse(player->combuf, player->argp, &player->condarg,
150 scanspace, &redir) < 0) {
151 pr("See \"info Syntax\"?\n");
153 if (dispatch(player->combuf, redir) < 0)
154 pr("Try \"list of commands\" or \"info\"\n");
167 if (player->state == PS_SHUTDOWN)
169 natp = getnatp(player->cnum);
170 if (io_error(player->iop) || io_eof(player->iop)) {
174 player->visitor = natp->nat_stat < STAT_SANCT;
175 if (player->dolcost != 0.0) {
176 if (player->dolcost > 100.0)
177 pr("That just cost you $%.2f\n", player->dolcost);
178 else if (player->dolcost < -100.0)
179 pr("You just made $%.2f\n", -player->dolcost);
180 if (natp->nat_money < player->dolcost && !player->broke) {
182 player->nstat &= ~MONEY;
183 pr("You are now broke; industries are on strike.\n");
184 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
186 player->nstat |= MONEY;
187 pr("You are no longer broke!\n");
189 natp->nat_money -= roundavg(player->dolcost);
190 player->dolcost = 0.0;
192 if (natp->nat_money < 0.0 && !player->broke) {
194 player->nstat &= ~MONEY;
195 pr("You are now broke; industries are on strike.\n");
197 if (player->broke && natp->nat_money > 0) {
199 player->nstat |= MONEY;
200 pr("You are no longer broke!\n");
203 getsect(natp->nat_xcap, natp->nat_ycap, §);
205 player->nstat &= ~CAP;
207 player->nstat |= CAP;
208 player->ncomstat = player->nstat;
210 player->ncomstat |= CAP | MONEY;
211 time(&player->curup);
212 minute = (player->curup - player->lasttime) / 60;
214 player->minleft -= minute;
215 if (player->minleft <= 0) {
217 * countdown timer "player->minleft" has expired.
218 * either day change, or hours restriction
220 daychange(player->curup);
221 if (!gamehours(player->curup)) {
222 pr("Empire hours restriction in force\n");
223 if (natp->nat_stat != STAT_GOD) {
228 player->minleft = getminleft(player->curup, m_m_p_d);
230 player->lasttime += minute * 60;
231 natp->nat_minused += minute;
233 if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
234 pr("Max minutes per day limit exceeded.\n");
235 player->ncomstat = VIS;
237 if (player->btused) {
238 natp->nat_btu -= player->btused;
241 if (natp->nat_tgms > 0) {
242 if (!(natp->nat_flags & NF_INFORM)) {
243 if (natp->nat_tgms == 1)
244 pr("You have a new telegram waiting ...\n");
246 pr("You have %s new telegrams waiting ...\n",
247 numstr(buf, natp->nat_tgms));
251 if (natp->nat_ann > 0) {
252 if (natp->nat_ann == 1)
253 pr("You have a new announcement waiting ...\n");
255 pr("You have %s new announcements waiting ...\n",
256 numstr(buf, natp->nat_ann));
259 if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
260 pr("You lost your capital... better designate one\n");
262 if (gamedown() && !player->god) {
270 * actually a command; redirection and piping ignored.
271 * XXX This whole mess should be redone; execute block should
272 * start with "exec start", and should end with "exec end".
273 * We'll wait until 1.2 I guess.
281 char *redir; /* UTF-8 */
282 char scanspace[1024];
287 p = getstarg(player->argp[1], "File? ", buf);
289 if (p == NULL || *p == '\0')
292 /* FIXME should use raw argument here, to support UTF-8 file names */
293 prexec(player->argp[1]);
295 while (!failed && status()) {
296 if (recvclient(buf, sizeof(buf)) < 0)
298 if (parse(buf, player->argp, &player->condarg,
299 scanspace, &redir) < 0) {
304 pr("\nExecute : %s\n", buf);
305 if (dispatch(buf, redir) < 0)
309 while (recvclient(buf, sizeof(buf)) >= 0) ;
312 pr("Execute : %s\n", failed ? "aborted" : "terminated");
321 char buf[MAXTELSIZE + 1]; /* UTF-8 */
323 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
327 pr ("Could not open motd.\n");
328 logerror("Could not open motd (%s).\n", motdfil);
332 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
333 logerror("bad header on login message (motdfil)");
337 if (tgm.tel_length >= (long)sizeof(buf)) {
338 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
342 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
343 logerror("bad length %ld on login message", tgm.tel_length);
347 buf[tgm.tel_length] = 0;
356 player->state = PS_SHUTDOWN;
361 praddr(struct player *p)
363 return prbuf("%s@%s", p->userid,
364 *p->hostname ? p->hostname : p->hostaddr);