forked from Atlantis-PBEM/Atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events-battle.cpp
487 lines (422 loc) · 14.8 KB
/
events-battle.cpp
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// START A3HEADER
//
// This source file is part of the Atlantis PBM game program.
// Copyright (C) 2020 Valdis Zobēla
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program, in the file license.txt. If not, write
// to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// See the Atlantis Project web page for details:
// http://www.prankster.com/project
//
// END A3HEADER
#include "gameio.h"
#include "events.h"
#include <memory>
#include <string>
#include <stdexcept>
template<typename ... Args> std::string string_format( const std::string& format, Args ... args )
{
int size = std::snprintf(nullptr, 0, format.c_str(), args ... ) + 1;
if (size <= 0) {
throw std::runtime_error( "Error during formatting." );
}
std::unique_ptr<char[]> buf(new char[ size ]);
snprintf(buf.get(), size, format.c_str(), args ...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
BattleFact::BattleFact() {
this->attacker = BattleSide();
this->defender = BattleSide();
this->location = EventLocation();
}
BattleFact::~BattleFact() {
}
const int N_VARIANTS = 4;
const char* ADJECTIVE[N_VARIANTS] = {
"Some",
"Many",
"Several",
"Dozen"
};
const char* REPORTING[N_VARIANTS] = {
"traders",
"pilgrims",
"travelers",
"adventurers"
};
const char* CHANNEL[N_VARIANTS] = {
"they have heard about",
"refugees are worried about",
"locals are rumoring about",
"have heard a tell about"
};
const char* SMALL_BATTLE[N_VARIANTS] = {
"encounter",
"fight",
"skrimish",
"battle"
};
const char* BATTLE[N_VARIANTS] = {
"clash",
"fight",
"assault",
"battle"
};
const char* ONE_SIDE[N_VARIANTS] = {
"a faction",
"a rebels",
"a villans",
"an opposition"
};
const char* TWO_SIDES[N_VARIANTS] = {
"hostile forces",
"enemies",
"two armies",
"combatants"
};
const char* HUNTERS[N_VARIANTS] = {
"an andventurers",
"a hunters",
"a witchers",
"a rangers"
};
const char* SIZES[N_VARIANTS] = {
"couple",
"few",
"several",
"many"
};
const char* ACTION_SUCCESS[N_VARIANTS] = {
"slain",
"murdered",
"put to the sword",
"expelled"
};
const char* ACTION_ATTEMPT[N_VARIANTS] = {
"attacked",
"ambushed",
"tried to cast out",
"tried to expel"
};
const char* NOUN[N_VARIANTS] = {
"terror",
"fear",
"horror",
"dread"
};
std::string relativeSize(int size) {
if (size < 3) return SIZES[0];
if (size < 12) return SIZES[1];
if (size < 24) return SIZES[2];
return SIZES[3];
}
std::string townType(int type) {
switch (type)
{
case TOWN_VILLAGE: return "village";
case TOWN_TOWN: return "town";
case TOWN_CITY: return "city";
default: return "unknown";
}
}
void BattleFact::GetEvents(std::list<Event> &events) {
// Some traders are telling that they have heard about
// Some traders are telling that refugees are worried about
// Some traders are telling that locals are rumoring about
// Some traders are telling that have heard a tell about
std::string text = string_format("%s %s are telling that %s ",
ADJECTIVE[getrandom(N_VARIANTS)],
REPORTING[getrandom(N_VARIANTS)],
CHANNEL[getrandom(N_VARIANTS)]
);
if (this->defender.factionNum == 1) {
// city capture
if (this->outcome == BATTLE_WON) {
// plains of Cefelat where in the Toadfield city guards were slain by a faction
text += string_format("%ss of %s where in the %s %s guards were %s by %s.",
this->location.getTerrain().c_str(),
this->location.province.c_str(),
this->location.settlement.c_str(),
townType(this->location.settlementType).c_str(),
ACTION_SUCCESS[getrandom(N_VARIANTS)],
ONE_SIDE[getrandom(N_VARIANTS)]
);
}
else {
// plains of Cefelat where in the Toadfield a villans attacked guards but were unsuccessful.
text += string_format("%ss of %s where in the %s %s %s %s guards but were unsuccessful.",
this->location.getTerrain().c_str(),
this->location.province.c_str(),
this->location.settlement.c_str(),
townType(this->location.settlementType).c_str(),
ONE_SIDE[getrandom(N_VARIANTS)],
ACTION_ATTEMPT[getrandom(N_VARIANTS)]
);
}
events.push_back({ EventCategory::EVENT_CITY_CAPTURE, this->location.settlementType + 1, text });
return;
}
if (this->defender.factionNum == 2) {
// monster hunt
if (this->outcome == BATTLE_WON) {
// a witchers who have slain Demons freeing the plains of Cefelat from their terror.
text += string_format("%s who have %s %s freeing the %ss of %s from their %s.",
HUNTERS[getrandom(N_VARIANTS)],
ACTION_SUCCESS[getrandom(N_VARIANTS)],
this->defender.unitName.c_str(),
this->location.getTerrain().c_str(),
this->location.province.c_str(),
NOUN[getrandom(N_VARIANTS)]
);
}
else {
// a witchers who tried to slain Demons in the plains of Cefelat but were all slain by their prey.
text += string_format("%s who tried to %s %s in the %ss of %s but were all %s by their prey.",
HUNTERS[getrandom(N_VARIANTS)],
ACTION_SUCCESS[getrandom(N_VARIANTS)],
this->defender.unitName.c_str(),
this->location.getTerrain().c_str(),
this->location.province.c_str(),
ACTION_SUCCESS[getrandom(N_VARIANTS)]
);
}
events.push_back({ EventCategory::EVENT_MONSTER_HUNT, 1, text });
return;
}
if (this->attacker.factionNum == 2) {
// monster aggression
if (this->outcome == BATTLE_WON) {
// Demons in the plains of Cefelat continue to cause %s on local inhabitants.
text += string_format("%s in the %ss of %s continue to cause %s on local inhabitants.",
this->attacker.unitName.c_str(),
this->location.getTerrain().c_str(),
this->location.province.c_str(),
NOUN[getrandom(N_VARIANTS)]
);
}
else {
// Demons tried to cause fear in the plains of Cefelat bet were slain by a witchers.
text += string_format("%s tried to cause %s in the %ss of %s bet were %s by %s.",
this->attacker.unitName.c_str(),
NOUN[getrandom(N_VARIANTS)],
this->location.getTerrain().c_str(),
this->location.province.c_str(),
ACTION_SUCCESS[getrandom(N_VARIANTS)],
HUNTERS[getrandom(N_VARIANTS)]
);
}
events.push_back({ EventCategory::EVENT_MONSTER_AGGRESSION, 1, text });
return;
}
// PvP
int total = this->attacker.total + this->defender.total;
int totalLost = this->attacker.lost + this->defender.lost;
int totalMages = this->attacker.mages + this->defender.mages;
int totalMonsters = this->attacker.monsters + this->defender.monsters;
int totalUndead = this->attacker.undead + this->defender.undead;
int totalFMI = this->attacker.fmi + this->defender.fmi;
int minLost = std::min(this->attacker.lost, this->defender.lost);
int maxLost = std::max(this->attacker.lost, this->defender.lost);
bool isSlaughter = total > 10 && (minLost == 0 || (maxLost / minLost) > 10);
int score = 0;
if (total <= 100) {
// encounter
// location known
std::string result;
if (this->outcome == BATTLE_DRAW) {
result = "and neither side won";
}
else if (isSlaughter) {
result = "ended in a slaughter";
}
else {
result = totalLost > total / 2
? "and some men died"
: "and many soldiers will never fight again";
}
// a small encounter between hostile forces in the woods of Sansaor where some men died.
text += string_format("a small %s between %s in the %ss of %s where %s.",
SMALL_BATTLE[getrandom(N_VARIANTS)],
TWO_SIDES[getrandom(N_VARIANTS)],
this->location.getTerrain().c_str(),
this->location.province.c_str(),
result.c_str()
);
score = 1;
}
else if (total <= 500) {
// local conflict
// location known
// number of looses known
int unceretanity = totalLost / 3; // 33%
int lost = totalLost + getrandom(unceretanity) - (unceretanity / 2);
std::string result;
if (this->outcome == BATTLE_DRAW) {
result = "and neither side won";
}
else if (isSlaughter) {
result = "ended in a slaughter";
}
else {
result = "and many soldiers will never fight again";
}
// a battle between two armies in the woods of Sansaor with .
text += string_format("a %s between %s in the %ss of %s with %i killed from both sides %s.",
SMALL_BATTLE[getrandom(N_VARIANTS)],
TWO_SIDES[getrandom(N_VARIANTS)],
this->location.getTerrain().c_str(),
this->location.province.c_str(),
lost,
result.c_str()
);
score = 2;
}
else if (total <= 2500) {
// regional conflict
// location known
// number of looses known
// mages, monsters, fmi
std::string specials;
if (totalFMI + totalMages + totalMonsters + totalUndead) {
specials = " with use of ";
bool second = false;
if (totalMages) {
specials += "magic";
second = true;
}
if (totalFMI) {
if (second) specials += ", ";
specials += "mechanisms";
second = true;
}
if (totalUndead) {
if (second) specials += ", ";
specials += "undead";
second = true;
}
if (totalMonsters) {
if (second) specials += ", ";
specials += "monsters";
second = true;
}
}
int unceretanity = totalLost / 5; // 20%
int lost = totalLost + getrandom(unceretanity) - (unceretanity / 2);
std::string result;
if (this->outcome == BATTLE_DRAW) {
result = "and neither side won";
}
else if (isSlaughter) {
result = "ended in a slaughter";
}
else {
result = "and many soldiers will never fight again";
}
std::string sides;
int roll = getrandom(10);
if (roll >= 8) {
// 20 %
sides = string_format("%s and %s",
this->attacker.factionName.c_str(),
this->defender.factionName.c_str()
);
}
else if (roll >= 6) {
// 20%
sides = string_format("%s and %s",
(getrandom(2) >= 1 ? this->attacker.factionName.c_str() : this->defender.factionName.c_str()),
ONE_SIDE[getrandom(N_VARIANTS)]
);
}
else {
// 60%
sides = TWO_SIDES[getrandom(N_VARIANTS)];
}
// a battle with use of magic between two armies in the woods of Sansaor with .
text += string_format("a %s%s between %s in the %ss of %s with %i killed from both sides %s.",
BATTLE[getrandom(N_VARIANTS)],
specials.c_str(),
sides.c_str(),
this->location.getTerrain().c_str(),
this->location.province.c_str(),
lost,
result.c_str()
);
score = 3;
}
else {
// continental conflict / epic conflict
// location known
// number of looses known
// mages, monsters, fmi
std::string specials;
if (totalFMI + totalMages + totalMonsters + totalUndead) {
specials = " with use of ";
bool second = false;
if (totalMages) {
specials += relativeSize(totalMages) + " mages";
second = true;
}
if (totalFMI) {
if (second) specials += ", ";
specials += relativeSize(totalFMI) + " mechanisms";
second = true;
}
if (totalUndead) {
if (second) specials += ", ";
specials += relativeSize(totalUndead) + " undead";
second = true;
}
if (totalMonsters) {
if (second) specials += ", ";
specials += relativeSize(totalMonsters) + " monsters";
second = true;
}
}
int unceretanity = totalLost / 10; // 10%
int lost = totalLost + getrandom(unceretanity) - (unceretanity / 2);
std::string result;
if (this->outcome == BATTLE_DRAW) {
result = "and neither side won";
}
else if (isSlaughter) {
result = "ended in a slaughter";
}
else {
result = "and many soldiers will never fight again";
}
std::string sides = string_format("%s and %s",
this->attacker.factionName.c_str(),
this->defender.factionName.c_str()
);
// a battle with use of magic between two armies in the woods of Sansaor with .
text += string_format("a epic %s%s between %s in the %ss of %s with %i killed from both sides %s.",
BATTLE[getrandom(N_VARIANTS)],
specials.c_str(),
sides.c_str(),
this->location.getTerrain().c_str(),
this->location.province.c_str(),
lost,
result.c_str()
);
score = 5;
}
if (totalMages > 0) score *= 2;
if (totalMonsters > 0) score += 1;
if (totalUndead > 0) score += 1;
if (totalFMI > 0) score += 1;
events.push_back({ EventCategory::EVENT_BATTLE, score, text });
}