/
message.c
429 lines (390 loc) · 13.8 KB
/
message.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* \file message.c
* \brief Starbase Reloaded - Messages
*/
#include <assert.h>
#include <string.h>
#include "message.h"
void Message_Init(struct Message* m)
{
m->Length = 0;
m->Lines = 0;
}
void Message_AddChar(struct Message* m, char ch)
{
if (m->Length < MAX_MESSAGE_LENGTH-1) {
if (ch == '\n') {
// Newline transmitted as \r (13) in VGAP
++m->Lines;
m->Content[m->Length] = 13;
++m->Length;
} else if ((unsigned char) ch > 255-13) {
// These characters cannot be handled by ROT-13 encryption; discard.
// They can possibly appear in user-provided names.
} else {
// Normal case
m->Content[m->Length] = ch;
++m->Length;
}
}
}
void Message_Add(struct Message* m, const char* str)
{
char ch;
while ((ch = *str++) != '\0') {
Message_AddChar(m, ch);
}
}
void Message_Format(struct Message* m, const char* tpl, const Uns32* args, size_t numArgs)
{
char ch;
while ((ch = *tpl++) != '\0') {
if (ch == '%') {
// Parameter index
size_t index = 0;
while (*tpl >= '0' && *tpl <= '9') {
index = 10*index + (*tpl++ - '0');
}
// Format it
char fmt = *tpl++;
char tmp[100];
switch (fmt) {
case '%':
Message_Add(m, "%");
break;
case 'I':
// 4-digit Id
sprintf(tmp, "%04ld", (long) (index < numArgs ? args[index] : 0));
Message_Add(m, tmp);
break;
case 'd':
// Normal decimal number
sprintf(tmp, "%ld", (long) (index < numArgs ? args[index] : 0));
Message_Add(m, tmp);
break;
case 'A':
// Adjective
if (index < numArgs) {
RaceNameAdjective(args[index], tmp);
Message_Add(m, tmp);
}
break;
case 'P':
// Planet name
if (index < numArgs) {
PlanetName(args[index], tmp);
Message_Add(m, tmp);
}
break;
case 'S':
// Ship name
if (index < numArgs) {
ShipName(args[index], tmp);
Message_Add(m, tmp);
}
break;
}
} else {
Message_AddChar(m, ch);
}
}
}
void Message_Send(struct Message* m, RaceType_Def to)
{
assert(m->Length < sizeof(m->Content));
m->Content[m->Length] = '\0';
WriteAUXHOSTMessage(to, m->Content);
}
void Message_SendTemplate(RaceType_Def to, const char* tpl, const Uns32* args, size_t numArgs)
{
struct Message m;
Message_Init(&m);
Message_Format(&m, tpl, args, numArgs);
Message_Send(&m, to);
}
void Message_Credits_InsufficentTechToReceive(RaceType_Def owner, Uns16 planetId, Uns16 required)
{
Uns32 args[] = { planetId, required };
Message_SendTemplate(owner,
"(-p%0I)<<< Space Dock Message >>>\n"
"\n"
"FROM: %0P\n"
" Starbase %0d\n"
"\n"
"We do not have sufficient tech to\n"
"receive credit transfers.\n"
"\n"
"Required total: %1d\n",
args, 2);
}
void Message_Credits_InsufficentTechToSend(RaceType_Def owner, Uns16 planetId, Uns16 required)
{
Uns32 args[] = { planetId, required };
Message_SendTemplate(owner,
"(-p%0I)<<< Space Dock Message >>>\n"
"\n"
"FROM: %0P\n"
" Starbase %0d\n"
"\n"
"We do not have sufficient tech to\n"
"send credit transfers.\n"
"\n"
"Required total: %1d\n",
args, 2);
}
void Message_Credits_Transferred(RaceType_Def owner, Uns16 fromPlanet, Uns16 toPlanet, Uns16 amount)
{
Uns32 args[] = { fromPlanet, toPlanet, amount };
Message_SendTemplate(owner,
"(-z%0I)<<< Space Dock Message >>>\n"
"\n"
"FROM: %0P\n"
" Starbase %0d\n"
"\n"
"We have transferred %2d mc\n"
"to the starbase at planet %1d,\n"
"%1P\n",
args, 3);
}
void Message_MinefieldLaid(RaceType_Def owner, Uns16 planetId, Uns16 mineId, Uns16 mineX, Uns16 mineY, Uns32 unitsLaid, Uns32 unitsNow, Uns16 radius, Boolean isWeb)
{
// 0 1 2 3 4 5 6
Uns32 args[] = { planetId, mineId, mineX, mineY, unitsLaid, unitsNow, radius };
const char* prefix = "(-l%1I)<<< Space Dock Message >>>\n"
"\n"
"FROM: %0P\n"
" Starbase %0d\n"
"\n";
const char* action;
if (isWeb) {
action =
"We have converted our\n"
"torpedoes into web mines\n";
} else {
action =
"We have converted our\n"
"torpedoes into deep space mines\n";
};
const char* result =
"and laid them in a field centered\n"
"at ( %2d , %3d )\n"
" %4d mines were laid\n"
"Mine field ID# %1d now contains\n"
" %5d mine units and is\n"
" %6d light years in radius\n";
struct Message m;
Message_Init(&m);
Message_Format(&m, prefix, args, 7);
Message_Format(&m, action, args, 7);
Message_Format(&m, result, args, 7);
Message_Send(&m, owner);
}
void Message_MinefieldSwept(RaceType_Def owner, Uns16 planetId, Uns16 mineId, Uns16 mineX, Uns16 mineY, RaceType_Def oldOwner, Uns16 oldRadius, Uns32 unitsSwept, Uns32 unitsNow, Boolean isWeb, Boolean usingFighters)
{
// 0 1 2 3 4 5 6 7
Uns32 args[] = { planetId, mineId, mineX, mineY, oldOwner, 2*oldRadius, unitsSwept, unitsNow };
// Describe situation
const char* situation;
if (isWeb) {
situation =
"(-p%0I)<<< Starbase Message >>>\n"
"\n"
"From: %0P\n"
" Starbase %0d\n\n"
"Detected enemy mine field #%1d\n"
"at ( %2d, %3d )\n"
"They are %4A WEB mines!\n"
"Minefield is %5d ly across.\n";
} else {
situation =
"(-p%0I)<<< Starbase Message >>>\n"
"\n"
"From: %0P\n"
" Starbase %0d\n\n"
"Detected enemy mine field #%1d\n"
"at ( %2d, %3d )\n"
"They are %4A mines!\n"
"Minefield is %5d ly across.\n";
}
// Describe action taken
const char* action;
if (usingFighters) {
action =
"Using fighters to sweep mines.\n"
"%6d mines destroyed.\n"
"%7d units remain.\n";
} else {
action =
"Using beam weapons to sweep mines.\n"
"%6d mines destroyed.\n"
"%7d units remain.\n";
}
struct Message m;
Message_Init(&m);
Message_Format(&m, situation, args, 8);
Message_Format(&m, action, args, 8);
Message_Send(&m, owner);
}
void Message_MinefieldScooped(RaceType_Def owner, Uns16 planetId, Uns16 mineId, Uns16 mineX, Uns16 mineY, Uns16 oldRadius, Uns16 torpsMade, Boolean isWeb)
{
// 0 1 2 3 4 5
Uns32 args[] = { planetId, mineId, mineX, mineY, 2*oldRadius, torpsMade };
// Describe situation
const char* situation;
if (isWeb) {
situation =
"(-p%0I)<<< Starbase Message >>>\n"
"\n"
"From: %0P\n"
" Starbase %0d\n\n"
"Detected our WEB mine field #%1d\n"
"at ( %2d, %3d )\n"
"Minefield is %4d ly across.\n";
} else {
situation =
"(-p%0I)<<< Starbase Message >>>\n"
"\n"
"From: %0P\n"
" Starbase %0d\n\n"
"Detected our mine field #%1d\n"
"at ( %2d, %3d )\n"
"Minefield is %4d ly across.\n";
}
// Describe action taken
const char* action =
"Gathering mines.\n"
"We made %5d torpedoes.\n";
struct Message m;
Message_Init(&m);
Message_Format(&m, situation, args, 6);
Message_Format(&m, action, args, 6);
Message_Send(&m, owner);
}
void Message_Transport_LoadNotPermitted(RaceType_Def to, Uns16 shipId)
{
Uns32 args[] = { shipId };
Message_SendTemplate(to,
"(-s%0I)<<< Fleet Message >>>\n"
"\n"
"FROM: %0S\n"
" Ship %0d\n"
"\n"
"We were ordered to load starship parts.\n"
"Unfortunately, our ship is not able\n"
"to carry starship parts.\n",
args, 1);
}
void Message_Transport_LoadNoParts(RaceType_Def to, Uns16 shipId, Uns16 planetId)
{
Uns32 args[] = { shipId, planetId };
Message_SendTemplate(to,
"(-s%0I)<<< Fleet Message >>>\n"
"\n"
"FROM: %0S\n"
" Ship %0d\n"
"\n"
"We were ordered to load starship parts.\n"
"Unfortunately, the starbase #%1d\n"
"at %1P\n"
"did not have any matching parts\n"
"in storage.\n",
args, 2);
}
void Message_Transport_LoadConflictingParts(RaceType_Def to, Uns16 shipId)
{
Uns32 args[] = { shipId };
Message_SendTemplate(to,
"(-s%0I)<<< Fleet Message >>>\n"
"\n"
"FROM: %0S\n"
" Ship %0d\n"
"\n"
"We were ordered to load starship parts.\n"
"Unfortunately, we already parts of\n"
"a different type aboard and cannot\n"
"load another type.\n",
args, 1);
}
void Message_Transport_LoadNoSpace(RaceType_Def to, Uns16 shipId)
{
Uns32 args[] = { shipId };
Message_SendTemplate(to,
"(-s%0I)<<< Fleet Message >>>\n"
"\n"
"FROM: %0S\n"
" Ship %0d\n"
"\n"
"We were ordered to load starship parts.\n"
"Unfortunately, we didn't have any space\n"
"in our cargo room to take the parts.\n",
args, 1);
}
void Message_Transport_LoadSuccess(RaceType_Def to, Uns16 shipId, Uns16 planetId, Uns16 numComponents)
{
Uns32 args[] = { shipId, planetId, numComponents };
Message_SendTemplate(to,
"(-s%0I)<<< Fleet Message >>>\n"
"\n"
"FROM: %0S\n"
" Ship %0d\n"
"\n"
"We have successfully loaded %2d parts\n"
"from starbase #%1d\n"
"at %1P\n",
args, 3);
}
void Message_Transport_UnloadNoParts(RaceType_Def to, Uns16 shipId)
{
Uns32 args[] = { shipId };
Message_SendTemplate(to,
"(-s%0I)<<< Fleet Message >>>\n"
"\n"
"FROM: %0S\n"
" Ship %0d\n"
"\n"
"We were ordered to unload starship\n"
"parts, but did not have anything\n"
"to unload.",
args, 1);
}
void Message_Transport_UnloadSuccess(RaceType_Def to, Uns16 shipId, Uns16 planetId, Uns32 numComponents)
{
Uns32 args[] = { shipId, planetId, numComponents };
Message_SendTemplate(to,
"(-s%0I)<<< Fleet Message >>>\n"
"\n"
"FROM: %0S\n"
" Ship %0d\n"
"\n"
"We have unloaded %2d parts\n"
"to starbase #%1d\n"
"at %1P",
args, 3);
}
void Message_Transport_TrimmedComponents(RaceType_Def to, Uns16 shipId, Uns16 droppedComponents, Uns16 droppedMass)
{
Uns32 args[] = { shipId, droppedComponents, droppedMass };
Message_SendTemplate(to,
"(-s%0I)<<< Fleet Message >>>\n"
"\n"
"FROM: %0S\n"
" Ship %0d\n"
"\n"
"We are overloaded!\n"
"We had to jettison %1d parts\n"
"weighing %2d kt total!\n",
args, 3);
}
void Message_Transport_TrimmedCargo(RaceType_Def to, Uns16 shipId, Uns16 droppedMass)
{
Uns32 args[] = { shipId, droppedMass };
Message_SendTemplate(to,
"(-s%0I)<<< Fleet Message >>>\n"
"\n"
"FROM: %0S\n"
" Ship %0d\n"
"\n"
"We are overloaded!\n"
"We had to jettison %1d kt cargo!\n",
args, 2);
}