]> git.pond.sub.org Git - empserver/blob - src/lib/player/empdis.c
Include config.h.
[empserver] / src / lib / player / empdis.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  empdis.c: Empire dispatcher stuff
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 2000
33  */
34
35 #include <config.h>
36
37 #include "prototypes.h"
38 #include <stdio.h>
39 #include "misc.h"
40 #include "player.h"
41 #include "nat.h"
42 #include "tel.h"
43 #include "proto.h"
44 #include "com.h"
45 #include "file.h"
46 #include "empio.h"
47 #include "subs.h"
48 #include "common.h"
49 #include "optlist.h"
50
51 #include <fcntl.h>
52 #include <time.h>
53 #if !defined(_WIN32)
54 #include <unistd.h>
55 #endif
56 #include <signal.h>
57
58 #define KEEP_COMMANDS 50
59
60 /* ring buffer of most recent command prompts and commands, user text */
61 static char player_commands[KEEP_COMMANDS][1024 + 8];
62
63 /* the slot holding the most recent command in player_commands[] */
64 static int player_commands_index = 0;
65
66 /*
67  * Get a command from the current player into COMBUFP[1024], in UTF-8.
68  * This may block for input, yielding the processor.  Flush buffered
69  * output when blocking, to make sure player sees the prompt.
70  * Return command's byte length on success, -1 on error.
71  */
72 int
73 getcommand(char *combufp)
74 {
75     struct natstr *natp = getnatp(player->cnum);
76     char buf[1024];             /* user text */
77
78     if (++player_commands_index >= KEEP_COMMANDS)
79         player_commands_index = 0;
80     sprintf(player_commands[player_commands_index], "%3d %3d [prompt]",
81             player_commands_index, player->cnum);
82
83     do {
84         prprompt(natp->nat_minused, natp->nat_btu);
85         buf[0] = 0;
86         if (recvclient(buf, 1024) < 0) {
87             return -1;
88         }
89     } while (buf[0] == 0);
90
91     if (++player_commands_index >= KEEP_COMMANDS)
92         player_commands_index = 0;
93     sprintf(player_commands[player_commands_index], "%3d %3d %s",
94             player_commands_index, player->cnum, buf);
95
96     if (player->flags & PF_UTF8)
97         return copy_utf8_no_funny(combufp, buf);
98     return copy_ascii_no_funny(combufp, buf);
99 }
100
101 void
102 init_player_commands(void)
103 {
104     int i;
105
106     for (i = 0; i < KEEP_COMMANDS; ++i)
107         *player_commands[i] = 0;
108 }
109
110 void
111 log_last_commands(void)
112 {
113     int i;
114
115     logerror("Most recent player commands:");
116     for (i = player_commands_index; i >= 0; --i)
117         if (*player_commands[i])
118             logerror("%s", player_commands[i] + 4);
119     for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
120         if (*player_commands[i])
121             logerror("%s", player_commands[i] + 4);
122 }
123
124 int
125 explain(void)
126 {
127     register s_char *format;
128     register int i;
129
130     pr("\t\tCurrent EMPIRE Command List\n");
131     pr("\t\t------- ------ ------- ----\n");
132     pr("Initial number is cost in B.T.U. units.\n");
133     pr("Next 2 chars (if present) are:\n");
134     pr("$ - must be non-broke\tc -- must have capital\n");
135     pr("Args in [brackets] are optional.\n");
136     if (player->nstat > 4) {
137         pr("All-caps args in <angle brackets>");
138         pr(" have the following meanings:\n");
139         pr("  <NUM> :: a number in unspecified units\n");
140         pr("  <COMM> :: a commodity such as `food', `guns', etc\n");
141         pr("  <VAR> :: a commodity such as `food', `guns', etc\n");
142         pr("  <TYPE> :: an item type such as `ship', `plane', etc\n");
143     }
144     for (i = 0; (format = player_coms[i].c_form) != 0; i++) {
145         if ((player_coms[i].c_permit & player->ncomstat) ==
146             player_coms[i].c_permit) {
147             pr("%2d ", player_coms[i].c_cost);
148             if ((player_coms[i].c_permit & MONEY) == MONEY)
149                 pr("$");
150             else
151                 pr(" ");
152             if ((player_coms[i].c_permit & CAP) == CAP)
153                 pr("c");
154             else
155                 pr(" ");
156             pr(" %s\n", format);
157         }
158     }
159     pr("For further info on command syntax see \"info Syntax\".\n");
160     return RET_OK;
161 }
162
163 /*
164  * returns true if down
165  */
166 int
167 gamedown(void)
168 {
169     FILE *down_fp;
170     struct telstr tgm;
171     char buf[MAXTELSIZE + 1];   /* UTF-8 */
172
173     if (player->god)
174         return 0;
175     if ((down_fp = fopen(downfil, "rb")) == NULL)
176         return 0;
177     if (fread(&tgm, sizeof(tgm), 1, down_fp) != 1) {
178         logerror("bad header on login message (downfil)");
179         fclose(down_fp);
180         return 1;
181     }
182     if (tgm.tel_length >= (long)sizeof(buf)) {
183         logerror("text length (%ld) is too long for login message (downfil)", tgm.tel_length);
184         fclose(down_fp);
185         return 1;
186     }
187     if (fread(buf, tgm.tel_length, 1, down_fp) != 1) {
188         logerror("bad length %ld on login message", tgm.tel_length);
189         fclose(down_fp);
190         return 1;
191     }
192     buf[tgm.tel_length] = 0;
193     uprnf(buf);
194     pr("\nThe game is down\n");
195     fclose(down_fp);
196     return 1;
197 }
198
199 void
200 daychange(time_t now)
201 {
202     struct natstr *natp;
203     struct tm *tm;
204
205     natp = getnatp(player->cnum);
206     tm = localtime(&now);
207     if ((tm->tm_yday % 128) != natp->nat_dayno) {
208         natp->nat_dayno = tm->tm_yday % 128;
209         natp->nat_minused = 0;
210     }
211 }
212
213 int
214 getminleft(time_t now, int mpd)
215 {
216     struct tm *tm;
217     int nminleft;
218     struct natstr *natp;
219     int n;
220
221     tm = localtime(&now);
222     natp = getnatp(player->cnum);
223     nminleft = mpd - natp->nat_minused;
224     n = 60 * 24 - (tm->tm_min + tm->tm_hour * 60);
225     if (n < nminleft)
226         nminleft = n;
227     return nminleft;
228 }