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