-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
221 lines (196 loc) · 6.87 KB
/
commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* \file commands.c
* \brief Agave Tequilana - Command Handling
*/
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include "commands.h"
#include "message.h"
#include "util.h"
static const char MSG_TERMINATOR = 26;
static const char MSG_OFFSET = 13;
/** @private */
struct CommandInfo {
struct State* pState;
const struct Config* pConfig;
};
static void ProcessBuildCommand(Uns16 pRace, const char* pArgs, const char* pWholeLine, const struct CommandInfo*const pInfo)
{
char* rem;
long parsed = strtol(pArgs, &rem, 10);
if (rem[strspn(rem, " \t")] == '\0' && parsed > 0 && parsed <= PLANET_NR) {
const Uns16 planetId = (Uns16) parsed;
if (pRace == State_PlanetOwner(pInfo->pState, planetId)) {
State_SetBuildRequest(pInfo->pState, planetId, True);
} else {
Info("\t(-) rejected command from %d: '%s' (not owned)", (int) pRace, pWholeLine);
Message_CactusFailed_NotOwned(pRace, planetId);
}
} else {
Info("\t(-) rejected command from %d: '%s' (syntax error)", (int) pRace, pWholeLine);
Message_CommandSyntaxError(pRace, pWholeLine);
}
}
static void ProcessVoteCommand(Uns16 pRace, const char* pArgs, const char* pWholeLine, const struct CommandInfo*const pInfo)
{
if (strcasecmp(pArgs, "yes") == 0 || strcasecmp(pArgs, "true") == 0) {
State_SetVote(pInfo->pState, pRace, True);
} else if (strcasecmp(pArgs, "no") == 0 || strcasecmp(pArgs, "false") == 0) {
State_SetVote(pInfo->pState, pRace, False);
} else {
Info("\t(-) rejected command from %d: '%s' (syntax error)", (int) pRace, pWholeLine);
Message_CommandSyntaxError(pRace, pWholeLine);
}
}
static Boolean CheckCommand(Uns16 pRace, const char* pCommand, const char* pArgs, const char* pWholeLine, void* pData)
{
const struct CommandInfo*const pInfo = pData;
if (strcasecmp(pCommand, "defhw") == 0 || strcasecmp(pCommand, "build") == 0) {
// Plant a cactus
ProcessBuildCommand(pRace, pArgs, pWholeLine, pInfo);
return True;
} else if (strcasecmp(pCommand, "vote") == 0) {
// Vote for end
ProcessVoteCommand(pRace, pArgs, pWholeLine, pInfo);
return True;
} else {
return False;
}
}
void ProcessCommands(struct State* pState, const struct Config* pConfig)
{
struct CommandInfo info;
info.pState = pState;
info.pConfig = pConfig;
Info(" Checking commands...");
CommandFileReader(0 /* all players */,
CheckCommand,
0 /* CommandComplain_Func */,
"cactus",
0 /* pPrivateFile */,
&info);
}
/*
* Classic/Legacy Message Processing
*/
static void CheckMessageLine(Uns16 pRace, const char* line, void* pData)
{
const char* arg;
if ((arg = StrStartsWith(line, "defhw ")) != 0 ||
(arg = StrStartsWith(line, "build ")) != 0 ||
(arg = StrStartsWith(line, "OBJECT: Planet ")) != 0)
{
ProcessBuildCommand(pRace, arg, line, pData);
} else if ((arg = StrStartsWith(line, "vote ")) != 0) {
ProcessVoteCommand(pRace, arg, line, pData);
} else {
// Ignore.
}
}
static const char* ReadMessageLine(char** data, size_t* size)
{
const char* result = *data;
// Skip over line
while (*size != 0 && **data != MSG_TERMINATOR) {
**data -= MSG_OFFSET;
++*data;
--*size;
}
// If we have data remaining, we're looking at a MSG_TERMINATOR.
// Replace by null byte and return.
if (*size != 0) {
**data = '\0';
++*data;
--*size;
return result;
} else {
// Partial line: cannot return because there's no space for the null terminator
// (or we ended the message already).
return 0;
}
}
/* Sort-of generic message file reader.
Reads mess.tmp/messpnt.tmp, and forwards message-to-self to func(), line-by-line */
static void MessageFileReader(void func(Uns16, const char*, void*), void* pData)
{
// Open files
FILE* pointerFile = OpenInputFile("messpnt.tmp", GAME_DIR_ONLY | NO_MISSING_ERROR);
FILE* dataFile = OpenInputFile("mess.tmp", GAME_DIR_ONLY | NO_MISSING_ERROR);
// Process files
if (pointerFile != 0 && dataFile != 0) {
// Read count
Uns16 numMessages;
if (!DOSRead16(&numMessages, 1, pointerFile)) {
numMessages = 0;
}
// Read messages
for (Uns16 i = 0; i < numMessages; ++i) {
// Single header
enum { Receiver, AddressLo, AddressHi, Length };
Uns16 header[4];
if (!DOSRead16(header, 4, pointerFile)) {
Warning("Unable to read header of message %d", (int) i);
break;
}
// Message receiver
Uns16 recv = header[Receiver];
char recvChar = (recv > 0 && recv < 10 ? '0' + recv
: recv == 10 ? 'a'
: recv == 11 ? 'b'
: '\0');
if (recvChar == '\0') {
Warning("Invalid receiver for message %d", (int) i);
break;
}
// Message position
Uns32 pos = (Uns32)header[AddressLo] + (65536*header[AddressHi]);
if (pos == 0) {
Warning("Invalid position for message %d", (int) i);
break;
}
if (fseek(dataFile, pos-1, SEEK_SET) != 0) {
Warning("Unable to set position for message %d", (int) i);
break;
}
// Message content
char data[1000];
size_t size = MIN(sizeof(data)-1, header[Length]);
if (fread(data, 1, size, dataFile) != size) {
Warning("Unable to read message %d", (int) i);
break;
}
// Terminate it, so ReadMessageLine can extract lines
data[size++] = MSG_TERMINATOR;
// Read it
char* p = data;
const char* firstLine = ReadMessageLine(&p, &size);
if (firstLine[0] == '('
&& firstLine[1] == '-'
&& firstLine[2] == 'r'
&& tolower((unsigned char) firstLine[3]) == recvChar)
{
// It's a message-to-self; parse it.
const char* line;
while ((line = ReadMessageLine(&p, &size)) != 0) {
func(recv, line, pData);
}
}
}
}
// Close files
if (pointerFile != 0) {
fclose(pointerFile);
}
if (dataFile != 0) {
fclose(dataFile);
}
}
void ProcessMessages(struct State* pState, const struct Config* pConfig)
{
struct CommandInfo info;
info.pState = pState;
info.pConfig = pConfig;
Info(" Checking legacy commands...");
MessageFileReader(CheckMessageLine, &info);
}