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 != 0.0) {
170 if (player->dolcost > 100.0)
171 pr("That just cost you $%.2f\n", player->dolcost);
172 else if (player->dolcost < -100.0)
173 pr("You just made $%.2f\n", -player->dolcost);
174 if (natp->nat_money < player->dolcost && !player->broke) {
176 player->nstat &= ~MONEY;
177 pr("You are now broke; industries are on strike.\n");
178 } else if (player->broke && natp->nat_money - player->dolcost > 0) {
180 player->nstat |= MONEY;
181 pr("You are no longer broke!\n");
183 natp->nat_money -= roundavg(player->dolcost);
184 player->dolcost = 0.0;
186 if (natp->nat_money < 0.0 && !player->broke) {
188 player->nstat &= ~MONEY;
189 pr("You are now broke; industries are on strike.\n");
191 if (player->broke && natp->nat_money > 0) {
193 player->nstat |= MONEY;
194 pr("You are no longer broke!\n");
197 getsect(natp->nat_xcap, natp->nat_ycap, §);
199 player->nstat &= ~CAP;
201 player->nstat |= CAP;
202 player->ncomstat = player->nstat;
204 player->ncomstat |= CAP | MONEY;
205 time(&player->curup);
206 minute = (player->curup - player->lasttime) / 60;
208 player->minleft -= minute;
209 if (player->minleft <= 0) {
211 * countdown timer "player->minleft" has expired.
212 * either day change, or hours restriction
214 daychange(player->curup);
215 if (!gamehours(player->curup)) {
216 pr("Empire hours restriction in force\n");
217 if (natp->nat_stat != STAT_GOD) {
222 player->minleft = getminleft(player->curup, m_m_p_d);
224 player->lasttime += minute * 60;
225 natp->nat_minused += minute;
227 if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
228 pr("Max minutes per day limit exceeded.\n");
229 player->ncomstat = VIS;
231 if (player->btused) {
232 natp->nat_btu -= player->btused;
235 if (natp->nat_tgms > 0) {
236 if (!(natp->nat_flags & NF_INFORM)) {
237 if (natp->nat_tgms == 1)
238 pr("You have a new telegram waiting ...\n");
240 pr("You have %s new telegrams waiting ...\n",
241 numstr(buf, natp->nat_tgms));
245 if (natp->nat_ann > 0) {
246 if (natp->nat_ann == 1)
247 pr("You have a new announcement waiting ...\n");
249 pr("You have %s new announcements waiting ...\n",
250 numstr(buf, natp->nat_ann));
253 if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
254 pr("You lost your capital... better designate one\n");
256 if (gamedown() && !player->god) {
264 * actually a command; redirection and piping ignored.
265 * XXX This whole mess should be redone; execute block should
266 * start with "exec start", and should end with "exec end".
267 * We'll wait until 1.2 I guess.
275 char *redir; /* UTF-8 */
276 char scanspace[1024];
281 if (player->comtail[1])
282 p = player->comtail[1];
284 p = getstring("File? ", buf);
285 if (p == NULL || *p == '\0')
289 while (!failed && status()) {
290 if (recvclient(buf, sizeof(buf)) < 0)
292 if (parse(buf, scanspace, player->argp, player->comtail,
293 &player->condarg, &redir) < 0) {
298 pr("\nExecute : %s\n", buf);
299 if (dispatch(buf, redir) < 0)
303 while (recvclient(buf, sizeof(buf)) >= 0) ;
306 pr("Execute : %s\n", failed ? "aborted" : "terminated");
316 char buf[MAXTELSIZE + 1]; /* UTF-8 */
318 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
322 pr ("Could not open motd.\n");
323 logerror("Could not open motd (%s).\n", motdfil);
327 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
328 logerror("bad header on login message (motdfil)");
332 if (tgm.tel_length >= (long)sizeof(buf)) {
333 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
337 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
338 logerror("bad length %ld on login message", tgm.tel_length);
342 buf[tgm.tel_length] = 0;
351 player->state = PS_SHUTDOWN;
356 praddr(struct player *p)
358 return prbuf("%s@%s", p->userid,
359 *p->hostname ? p->hostname : p->hostaddr);