-
Notifications
You must be signed in to change notification settings - Fork 0
/
diplomacy.c
374 lines (349 loc) · 9.9 KB
/
diplomacy.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "diplomacy.h"
#include "world.h"
char *const dipstatus_name[] = {
[NEUTRAL] = "neutral",
[ALLIANCE] = "alliance",
[WAR] = "war"
};
dipstatus_t *create_diplomacylist()
{
/* single instance */
if (world->diplomacylist != NULL)
return world->diplomacylist;
world->diplomacylist = malloc(sizeof(dipstatus_t));
if (world->diplomacylist == NULL) exit(EXIT_FAILURE);
return world->diplomacylist;
}
inline static void fill_diplomacy_details(dipstatus_t *diplomacy, character_t *character1,
character_t *character2, const unsigned int status)
{
if (!diplomacy || !character1 || !character2) return;
if (character1->id < character2->id) {
diplomacy->character1 = character1;
diplomacy->character2 = character2;
}
else {
diplomacy->character1 = character2;
diplomacy->character2 = character1;
}
diplomacy->status = status;
diplomacy->pending_offer = NULL;
diplomacy->prev = NULL;
diplomacy->next = NULL;
}
inline static dipstatus_t *get_dipstatus_if_exist(character_t *character1, character_t *character2) {
if (character1 == NULL || character2 == NULL) return NULL;
character_t *first, *second;
if (character1->id < character2->id) {
first = character1;
second = character2;
}
else {
first = character2;
second = character1;
}
dipstatus_t *current = world->diplomacylist;
while (current != NULL) {
if (current->character1 == first && current->character2 == second) {
break;
}
current = current->next;
}
return current;
}
inline static dipstatus_t *add_dipstatus(character_t *character1, character_t *character2,
const unsigned int status)
{
if (character1 == NULL || character2 == NULL) return NULL;
character_t *first, *second;
if (character1->id < character2->id) {
first = character1;
second = character2;
}
else {
first = character2;
second = character1;
}
dipstatus_t *current = world->diplomacylist;
dipstatus_t *prev = NULL;
/* if diplomacylist does not exist, create it */
if (!current) {
current = create_diplomacylist();
fill_diplomacy_details(current, first, second, status);
world->diplomacylist = current;
return current;
}
else {
while (current && (current->character1->id < first->id || (current->character1 == first && current->character2->id < second->id))) {
prev = current;
current = current->next;
}
dipstatus_t *new = malloc(sizeof(dipstatus_t));
if (!new) exit(EXIT_FAILURE);
fill_diplomacy_details(new, character1, character2, status);
if (prev) {
prev->next = new;
new->prev = prev;
}
else {
world->diplomacylist = new;
}
if (current) {
current->prev = new;
new->next = current;
}
return new;
}
}
dipstatus_t *set_diplomacy(character_t *character1, character_t *character2,
const unsigned int status)
{
dipstatus_t *current = get_dipstatus_if_exist(character1, character2);
if (current) {
current->status = status;
current->pending_offer = NULL;
}
else current = add_dipstatus(character1, character2, status);
return current;
}
dipstatus_t *get_dipstatus(character_t *character1, character_t *character2)
{
dipstatus_t *current = get_dipstatus_if_exist(character1, character2);
if (current) return current;
/* if nothing found, create neutral diplomacy and return it */
return add_dipstatus(character1, character2, NEUTRAL);
}
unsigned char get_diplomacy(character_t *character1, character_t *character2)
{
dipstatus_t *dipstatus = get_dipstatus(character1, character2);
if (dipstatus) return dipstatus->status;
if ((character1->lord && character1->lord == character2)
|| (character2->lord && character2->lord == character1))
return ALLIANCE;
return NEUTRAL;
}
void remove_diplomacy(dipstatus_t *dipstatus)
{
if (dipstatus == NULL)
return;
dipstatus_t *current = world->diplomacylist;
while (current != NULL) {
if (current == dipstatus) {
if (current->prev) current->prev->next = current->next;
else world->diplomacylist = current->next;
if (current->next) current->next->prev = current->prev;
dipstatus_t *tmp = current;
free(tmp);
return;
}
current = current->next;
}
}
void clear_diplomacy_list() {
if (!world) return;
dipstatus_t *current = world->diplomacylist;
dipstatus_t *next = current;
while (current) {
next = current->next;
remove_diplomacy(current);
current = next;
}
}
void remove_redundant_diplomacy() {
dipstatus_t *current = world->diplomacylist;
dipstatus_t *next = current;
while (current) {
next = current->next;
if (current->character1 == current->character2 ||
(current->status == NEUTRAL && current->pending_offer == NULL) ||
current->character1->lord == current->character2 ||
current->character1 == current->character2->lord
) remove_diplomacy(current);
current = next;
}
}
void homage(character_t *character, character_t *lord)
{
if (character == NULL || lord == NULL)
return;
/* can not switch lord */
if (character->lord != NULL)
return;
/* can not be a lord of yourself */
if (character->id == lord->id)
return;
/* can not pay homage to a baron */
unsigned char character_rank = get_character_rank(character);
unsigned char lord_rank = get_character_rank(lord);
// piece_t *lord_noble = get_noble_by_owner(lord);
if (lord_rank <= 1)
return;
/**
* if lord's rank is lower or same as vassal,
* then vassal's rank decreases,
* and all his high-rank vassals become free
**/
if (character_rank >= lord_rank) {
/* demote the character */
set_character_rank(character, lord_rank - 1);
character_rank = lord_rank - 1;
character_t *current_character = world->characterlist;
while (current_character != NULL) {
/* free vassals */
if (current_character->lord != NULL
&& current_character->lord->id == character->id
&& get_character_rank(current_character) >= character_rank)
current_character->lord = NULL;
current_character = current_character->next;
}
}
character->lord = lord;
set_diplomacy(character, lord, ALLIANCE);
}
void unhomage(character_t *character) {
if (!character) return;
character->lord = NULL;
}
/* not used */
void promote_soldier(character_t *character, piece_t *piece, region_t *region,
char *name)
{
if (character == NULL || piece == NULL || region == NULL)
return;
/* promotion costs money */
if (character->money < COST_SOLDIER)
return;
/* can't promote somebody else's piece */
if (piece->owner != character)
return;
/* can't give somebody else's region */
if (region->owner != character)
return;
/* character must have at least two regions (one for new character) */
if (count_regions_by_owner(character) < 2)
return;
character_t *vassal = add_character_before(character, name);
// piece->rank = 1;
set_character_rank(vassal, 1);
piece->owner = vassal;
change_region_owner(vassal, region);
set_diplomacy(character, vassal, ALLIANCE);
character->money -= COST_SOLDIER;
}
void promote_vassal(character_t *lord, character_t *vassal)
{
if (lord == NULL || vassal == NULL)
return;
/* vassal's new rank should be lower than lord's */
if (vassal->rank + 1 <= lord->rank)
return;
/* promotion costs money */
if (lord->money < COST_SOLDIER)
return;
vassal->rank++;
lord->money -= COST_SOLDIER;
}
uint16_t count_vassals(character_t *character)
{
if (character == NULL)
return 0;
uint16_t counter = 0;
character_t *current_vassal = world->characterlist;
while (current_vassal != NULL) {
if (current_vassal->lord != NULL
&& current_vassal->lord->id == character->id)
counter++;
current_vassal = current_vassal->next;
}
return counter;
}
character_t *get_sovereign(character_t *character)
{
character_t *sovereign = character;
while (sovereign->lord != NULL)
sovereign = sovereign->lord;
return sovereign;
}
dipoffer_t *open_offer(character_t *from, character_t *to, const unsigned int offer)
{
dipstatus_t *dipstatus = get_dipstatus(from, to);
/* if dipstatus does not exist, set it to default */
if (!dipstatus) {
dipstatus = set_diplomacy(from, to, NEUTRAL);
if (!dipstatus) return NULL;
}
else {
/* if another offer is pending, reject it */
if (dipstatus->pending_offer)
close_offer(dipstatus->pending_offer, REJECT);
}
unsigned char status = dipstatus->status;
/* war->neutral or neutral->alliance */
dipoffer_t *dipoffer = NULL;
if ((status == WAR && offer == NEUTRAL)
|| (status == NEUTRAL && offer == ALLIANCE)) {
dipoffer = malloc(sizeof(dipoffer_t));
if (!dipoffer) exit(EXIT_FAILURE);
dipoffer->from = from;
dipoffer->to = to;
dipoffer->offer = offer;
dipstatus->pending_offer = dipoffer;
}
return dipoffer;
}
void close_offer(dipoffer_t *offer, const unsigned int result)
{
if (offer == NULL || offer->from == NULL || offer->to == NULL)
return;
dipstatus_t *dipstatus = get_dipstatus(offer->from, offer->to);
if (dipstatus == NULL) {
free(offer);
return;
}
if (result == ACCEPT) {
switch (dipstatus->status) {
case WAR:
if (offer->offer == NEUTRAL)
dipstatus->status = NEUTRAL;
break;
case NEUTRAL:
if (offer->offer == ALLIANCE)
dipstatus->status = ALLIANCE;
break;
}
}
free(offer);
dipstatus->pending_offer = NULL;
if (dipstatus->status == NEUTRAL) remove_diplomacy(dipstatus);
return;
}
void sort_diplomacy_list() {
if (world->diplomacylist == NULL || world->diplomacylist->next == NULL) return;
int permutations;
dipstatus_t *current, *next;
do {
current = world->diplomacylist;
next = current->next;
permutations = 0;
while (next != NULL) {
if (current->character1->id > next->character1->id || (current->character1->id == next->character1->id && current->character2->id > next->character2->id) ) {
permutations++;
if (current->prev != NULL) current->prev->next = next;
else world->diplomacylist = next;
if (next->next) next->next->prev = current;
next->prev = current->prev;
current->next = next->next;
current->prev = next;
next->next = current;
next = current->next;
}
else {
current = next;
next = next->next;
}
}
} while (permutations > 0);
}