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"
52 static int command(void);
53 static int status(void);
55 struct player *player;
58 player_main(struct player *p)
64 p->state = PS_PLAYING;
66 time(&player->lasttime);
69 if (init_nats() < 0) {
70 pr("Server confused, try again later\n");
73 natp = getnatp(player->cnum);
74 if (!gamehours(player->curup)) {
75 pr("Empire hours restriction in force\n");
76 if (natp->nat_stat != STAT_GOD)
79 daychange(player->curup);
80 if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
81 pr("Time exceeded today\n");
84 if (natp->nat_stat != STAT_VIS
85 && natp->nat_last_login
86 && (strcmp(natp->nat_hostaddr, player->hostaddr)
87 || strcmp(natp->nat_userid, player->userid))) {
88 pr("Last connection from: %s", ctime(&natp->nat_last_login));
90 natp->nat_last_login <= natp->nat_last_logout
91 ? ctime(&natp->nat_last_logout) : "?");
94 *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
96 strcpy(natp->nat_userid, player->userid);
97 strcpy(natp->nat_hostname, player->hostname);
98 strcpy(natp->nat_hostaddr, player->hostaddr);
100 time(&natp->nat_last_login);
103 if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
104 if (natp->nat_tgms == 1)
105 pr("You have a new telegram waiting ...\n");
107 pr("You have %s new telegrams waiting ...\n",
108 numstr(buf, natp->nat_tgms));
113 if (command() == 0 && !player->aborted)
118 /* #*# I put the following line in to prevent server crash -KHS */
119 natp = getnatp(player->cnum);
121 * randomly round up to the nearest minute,
122 * charging at least 15 seconds.
124 time(&natp->nat_last_logout);
125 secs = MAX(natp->nat_last_logout - player->lasttime, 15);
126 natp->nat_minused += secs / 60;
128 if (chance(secs / 60.0))
129 natp->nat_minused += 1;
138 char *redir; /* UTF-8 */
139 char scanspace[1024];
141 if (getcommand(player->combuf) < 0)
143 if (parse(player->combuf, scanspace, player->argp, player->comtail,
144 &player->condarg, &redir) < 0) {
145 pr("See \"info Syntax\"?\n");
147 if (dispatch(player->combuf, redir) < 0)
148 pr("Try \"list of commands\" or \"info\"\n");
157 int old_nstat, minute;
160 if (player->state == PS_SHUTDOWN)
162 natp = getnatp(player->cnum);
163 if (io_error(player->iop) || io_eof(player->iop)) {
167 if (player->dolcost > 100.0)
168 pr("That just cost you $%.2f\n", player->dolcost);
169 else if (player->dolcost < -100.0)
170 pr("You just made $%.2f\n", -player->dolcost);
171 natp->nat_money -= roundavg(player->dolcost);
172 player->dolcost = 0.0;
174 old_nstat = player->nstat;
175 player_set_nstat(player, natp);
176 if ((old_nstat & MONEY) && !(player->nstat & MONEY))
177 pr("You are now broke; industries are on strike.\n");
178 if (!(old_nstat & MONEY) && (player->nstat & MONEY))
179 pr("You are no longer broke!\n");
180 player->ncomstat = player->nstat;
182 player->ncomstat |= CAP | MONEY;
184 time(&player->curup);
185 minute = (player->curup - player->lasttime) / 60;
187 player->minleft -= minute;
188 if (player->minleft <= 0) {
190 * countdown timer "player->minleft" has expired.
191 * either day change, or hours restriction
193 daychange(player->curup);
194 if (!gamehours(player->curup)) {
195 pr("Empire hours restriction in force\n");
196 if (natp->nat_stat != STAT_GOD) {
201 player->minleft = getminleft(player->curup, m_m_p_d);
203 player->lasttime += minute * 60;
204 natp->nat_minused += minute;
206 if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
207 pr("Max minutes per day limit exceeded.\n");
208 player->ncomstat = VIS;
210 if (player->btused) {
211 natp->nat_btu -= player->btused;
214 if (natp->nat_tgms > 0) {
215 if (!(natp->nat_flags & NF_INFORM)) {
216 if (natp->nat_tgms == 1)
217 pr("You have a new telegram waiting ...\n");
219 pr("You have %s new telegrams waiting ...\n",
220 numstr(buf, natp->nat_tgms));
224 if (natp->nat_ann > 0) {
225 if (natp->nat_ann == 1)
226 pr("You have a new announcement waiting ...\n");
228 pr("You have %s new announcements waiting ...\n",
229 numstr(buf, natp->nat_ann));
232 if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
233 pr("You lost your capital... better designate one\n");
235 if (gamedown() && !player->god) {
243 * actually a command; redirection and piping ignored.
244 * XXX This whole mess should be redone; execute block should
245 * start with "exec start", and should end with "exec end".
246 * We'll wait until 1.2 I guess.
254 char *redir; /* UTF-8 */
255 char scanspace[1024];
260 if (player->comtail[1])
261 p = player->comtail[1];
263 p = getstring("File? ", buf);
264 if (p == NULL || *p == '\0')
268 while (!failed && status()) {
269 if (recvclient(buf, sizeof(buf)) < 0)
271 if (parse(buf, scanspace, player->argp, player->comtail,
272 &player->condarg, &redir) < 0) {
277 pr("\nExecute : %s\n", buf);
278 if (dispatch(buf, redir) < 0)
282 while (recvclient(buf, sizeof(buf)) >= 0) ;
285 pr("Execute : %s\n", failed ? "aborted" : "terminated");
295 char buf[MAXTELSIZE + 1]; /* UTF-8 */
297 if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
301 pr ("Could not open motd.\n");
302 logerror("Could not open motd (%s).\n", motdfil);
306 if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
307 logerror("bad header on login message (motdfil)");
311 if (tgm.tel_length >= (long)sizeof(buf)) {
312 logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
316 if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
317 logerror("bad length %ld on login message", tgm.tel_length);
321 buf[tgm.tel_length] = 0;
330 player->state = PS_SHUTDOWN;
335 praddr(struct player *p)
337 return prbuf("%s@%s", p->userid,
338 *p->hostname ? p->hostname : p->hostaddr);