]> git.pond.sub.org Git - empserver/blob - src/lib/player/empdis.c
Declare all configuration parameters in optlist.h. Remove some
[empserver] / src / lib / player / empdis.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "prototypes.h"
36 #include <stdio.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "nat.h"
40 #include "tel.h"
41 #include "proto.h"
42 #include "com.h"
43 #include "deity.h"
44 #include "keyword.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 s_char player_commands[KEEP_COMMANDS][1024 + 8];
60 int player_commands_index = 0;
61
62 int
63 getcommand(s_char *combufp)
64 {
65     struct natstr *natp;
66     s_char buf[1024];
67
68 /* Note this now assumes a 1024 byte buffer is being passed in */
69     natp = getnatp(player->cnum);
70     if (++player_commands_index >= KEEP_COMMANDS)
71         player_commands_index = 0;
72     sprintf(player_commands[player_commands_index], "%3d %3d [prompt]",
73             player_commands_index, player->cnum);
74     do {
75         prprompt(natp->nat_minused, natp->nat_btu);
76         buf[0] = 0;
77         if (recvclient(buf, 1024) < 0) {
78             return -1;
79         }
80     } while (buf[0] == 0);
81     if (++player_commands_index >= KEEP_COMMANDS)
82         player_commands_index = 0;
83     sprintf(player_commands[player_commands_index], "%3d %3d %s",
84             player_commands_index, player->cnum, buf);
85     strcpy(combufp, buf);
86     return (strlen(combufp));
87 }
88
89 void
90 init_player_commands(void)
91 {
92     int i;
93
94     for (i = 0; i < KEEP_COMMANDS; ++i)
95         *player_commands[i] = 0;
96 }
97
98 void
99 log_last_commands(void)
100 {
101     int i;
102
103     logerror("Most recent player commands:");
104     for (i = player_commands_index; i >= 0; --i)
105         if (*player_commands[i])
106             logerror("%s", player_commands[i] + 4);
107     for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
108         if (*player_commands[i])
109             logerror("%s", player_commands[i] + 4);
110 }
111
112 int
113 explain(void)
114 {
115     extern struct cmndstr player_coms[];
116     register s_char *format;
117     register int i;
118
119     pr("\t\tCurrent EMPIRE Command List\n");
120     pr("\t\t------- ------ ------- ----\n");
121     pr("Initial number is cost in B.T.U. units.\n");
122     pr("Next 2 chars (if present) are:\n");
123     pr("$ - must be non-broke\tc -- must have capital\n");
124     pr("Args in [brackets] are optional.\n");
125     if (player->nstat > 4) {
126         pr("All-caps args in <angle brackets>");
127         pr(" have the following meanings:\n");
128         pr("  <NUM> :: a number in unspecified units\n");
129         pr("  <COMM> :: a commodity such as `food', `guns', etc\n");
130         pr("  <VAR> :: a commodity such as `food', `guns', etc\n");
131         pr("  <TYPE> :: an item type such as `ship', `plane', etc\n");
132     }
133     for (i = 0; (format = player_coms[i].c_form) != 0; i++) {
134         if ((player_coms[i].c_permit & player->ncomstat) ==
135             player_coms[i].c_permit) {
136             pr("%2d ", player_coms[i].c_cost);
137             if ((player_coms[i].c_permit & MONEY) == MONEY)
138                 pr("$");
139             else
140                 pr(" ");
141             if ((player_coms[i].c_permit & CAP) == CAP)
142                 pr("c");
143             else
144                 pr(" ");
145             pr(" %s\n", format);
146         }
147     }
148     pr("For further info on command syntax see \"info Syntax\".\n");
149     return RET_OK;
150 }
151
152 /*
153  * returns true if down
154  */
155 int
156 gamedown(void)
157 {
158     int downf;
159     struct telstr tgm;
160     s_char buf[1024];
161
162     if (player->god)
163         return 0;
164 #if !defined(_WIN32)
165     if ((downf = open(downfil, O_RDONLY, 0)) < 0)
166 #else
167     if ((downf = open(downfil, O_RDONLY | O_BINARY, 0)) < 0)
168 #endif
169         return 0;
170     if (read(downf, (s_char *)&tgm, sizeof(tgm)) != sizeof(tgm)) {
171         logerror("bad header on login message (downfil)");
172         close(downf);
173         return 1;
174     }
175     if (read(downf, buf, tgm.tel_length) != tgm.tel_length) {
176         logerror("bad length %d on login message", tgm.tel_length);
177         close(downf);
178         return 1;
179     }
180     if (tgm.tel_length >= (long)sizeof(buf))
181         tgm.tel_length = sizeof(buf) - 1;
182     buf[tgm.tel_length] = 0;
183     pr(buf);
184     pr("\nThe game is down\n");
185     (void)close(downf);
186     return 1;
187 }
188
189 void
190 daychange(time_t now)
191 {
192     struct natstr *natp;
193     struct tm *tm;
194
195     natp = getnatp(player->cnum);
196     tm = localtime(&now);
197     if ((tm->tm_yday % 128) != natp->nat_dayno) {
198         natp->nat_dayno = tm->tm_yday % 128;
199         natp->nat_minused = 0;
200     }
201 }
202
203 int
204 getminleft(time_t now, int *hour, int *mpd)
205 {
206     s_char *bp;
207     struct tm *tm;
208     int nminleft;
209     int curtime;
210     struct natstr *natp;
211     int n;
212
213     tm = localtime(&now);
214     curtime = tm->tm_min + tm->tm_hour * 60;
215     if (NULL != (bp = kw_find("minutes")))
216         kw_parse(CF_VALUE, bp, mpd);
217     natp = getnatp(player->cnum);
218     nminleft = *mpd - natp->nat_minused;
219     if (NULL != (bp = kw_find("hours"))) {
220         /*
221          * assume hours has already been set; just verify
222          * that it is present
223          */
224         n = hour[1] - curtime;
225         if (n < nminleft)
226             nminleft = n;
227     }
228     n = 60 * 24 - (tm->tm_min + tm->tm_hour * 60);
229     if (n < nminleft)
230         nminleft = n;
231     return nminleft;
232 }