2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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:
40 #include "empthread.h"
48 #include "prototypes.h"
53 static int command(void);
54 static int status(void);
56 struct player *player;
59 player_main(struct player *p)
65 p->state = PS_PLAYING;
67 time(&player->lasttime);
70 if (init_nats() < 0) {
71 pr("Server confused, try again later\n");
74 natp = getnatp(player->cnum);
75 if (!gamehours(player->curup)) {
76 pr("Empire hours restriction in force\n");
77 if (natp->nat_stat != STAT_GOD)
80 daychange(player->curup);
81 if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
82 pr("Time exceeded today\n");
85 if (natp->nat_stat != STAT_VIS
86 && natp->nat_last_login
87 && (strcmp(natp->nat_hostaddr, player->hostaddr)
88 || strcmp(natp->nat_userid, player->userid))) {
89 pr("Last connection from: %s", ctime(&natp->nat_last_login));
91 natp->nat_last_login <= natp->nat_last_logout
92 ? ctime(&natp->nat_last_logout) : "?");
95 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
97 strcpy(natp->nat_userid, player->userid);
98 strcpy(natp->nat_hostname, player->hostname);
99 strcpy(natp->nat_hostaddr, player->hostaddr);
101 time(&natp->nat_last_login);
104 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
105 if (natp->nat_tgms == 1)
106 pr("You have a new telegram waiting ...\n");
108 pr("You have %s new telegrams waiting ...\n",
109 numstr(buf, natp->nat_tgms));
114 if (command() == 0 && !player->aborted)
119 /* #*# I put the following line in to prevent server crash -KHS */
120 natp = getnatp(player->cnum);
122 * randomly round up to the nearest minute,
123 * charging at least 15 seconds.
125 time(&natp->nat_last_logout);
126 secs = MAX(natp->nat_last_logout - player->lasttime, 15);
127 natp->nat_minused += secs / 60;
129 if (chance(secs / 60.0))
130 natp->nat_minused += 1;
139 char *redir; /* UTF-8 */
140 char scanspace[1024];
142 if (getcommand(player->combuf) < 0)
144 if (parse(player->combuf, scanspace, player->argp, player->comtail,
145 &player->condarg, &redir) < 0) {
146 pr("See \"info Syntax\"?\n");
148 if (dispatch(player->combuf, redir) < 0)
149 pr("Try \"list of commands\" or \"info\"\n");
162 if (player->state == PS_SHUTDOWN)
164 natp = getnatp(player->cnum);
165 if (io_error(player->iop) || io_eof(player->iop)) {
169 if (player->dolcost > 100.0)
170 pr("That just cost you $%.2f\n", player->dolcost);
171 else if (player->dolcost < -100.0)
172 pr("You just made $%.2f\n", -player->dolcost);
173 natp->nat_money -= roundavg(player->dolcost);
174 player->dolcost = 0.0;
175 if (natp->nat_money < 0 && !player->broke) {
177 player->nstat &= ~MONEY;
178 pr("You are now broke; industries are on strike.\n");
179 } else if (player->broke && natp->nat_money >= 0) {
181 player->nstat |= MONEY;
182 pr("You are no longer broke!\n");
184 getsect(natp->nat_xcap, natp->nat_ycap, §);
186 player->nstat &= ~CAP;
188 player->nstat |= CAP;
189 player->ncomstat = player->nstat;
191 player->ncomstat |= CAP | MONEY;
192 time(&player->curup);
193 minute = (player->curup - player->lasttime) / 60;
195 player->minleft -= minute;
196 if (player->minleft <= 0) {
198 * countdown timer "player->minleft" has expired.
199 * either day change, or hours restriction
201 daychange(player->curup);
202 if (!gamehours(player->curup)) {
203 pr("Empire hours restriction in force\n");
204 if (natp->nat_stat != STAT_GOD) {
209 player->minleft = getminleft(player->curup, m_m_p_d);
211 player->lasttime += minute * 60;
212 natp->nat_minused += minute;
214 if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
215 pr("Max minutes per day limit exceeded.\n");
216 player->ncomstat = VIS;
218 if (player->btused) {
219 natp->nat_btu -= player->btused;
222 if (natp->nat_tgms > 0) {
223 if (!(natp->nat_flags & NF_INFORM)) {
224 if (natp->nat_tgms == 1)
225 pr("You have a new telegram waiting ...\n");
227 pr("You have %s new telegrams waiting ...\n",
228 numstr(buf, natp->nat_tgms));
232 if (natp->nat_ann > 0) {
233 if (natp->nat_ann == 1)
234 pr("You have a new announcement waiting ...\n");
236 pr("You have %s new announcements waiting ...\n",
237 numstr(buf, natp->nat_ann));
240 if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
241 pr("You lost your capital... better designate one\n");
243 if (gamedown() && !player->god) {
251 * actually a command; redirection and piping ignored.
252 * XXX This whole mess should be redone; execute block should
253 * start with "exec start", and should end with "exec end".
254 * We'll wait until 1.2 I guess.
262 char *redir; /* UTF-8 */
263 char scanspace[1024];
268 if (player->comtail[1])
269 p = player->comtail[1];
271 p = getstring("File? ", buf);
272 if (p == NULL || *p == '\0')
276 while (!failed && status()) {
277 if (recvclient(buf, sizeof(buf)) < 0)
279 if (parse(buf, scanspace, player->argp, player->comtail,
280 &player->condarg, &redir) < 0) {
285 pr("\nExecute : %s\n", buf);
286 if (dispatch(buf, redir) < 0)
290 while (recvclient(buf, sizeof(buf)) >= 0) ;
293 pr("Execute : %s\n", failed ? "aborted" : "terminated");
303 char buf[MAXTELSIZE + 1]; /* UTF-8 */
305 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
309 pr ("Could not open motd.\n");
310 logerror("Could not open motd (%s).\n", motdfil);
314 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
315 logerror("bad header on login message (motdfil)");
319 if (tgm.tel_length >= (long)sizeof(buf)) {
320 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
324 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
325 logerror("bad length %ld on login message", tgm.tel_length);
329 buf[tgm.tel_length] = 0;
338 player->state = PS_SHUTDOWN;
343 praddr(struct player *p)
345 return prbuf("%s@%s", p->userid,
346 *p->hostname ? p->hostname : p->hostaddr);