-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.c
449 lines (408 loc) · 12.1 KB
/
character.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "map.h"
#include "world.h"
char *const rank_name[] = {
"knight",
"baron",
"count",
"duke",
"king"
};
inline static void set_expected_age(character_t *character);
character_t *create_characterlist()
{
/* single instance */
if (world->characterlist != NULL)
return world->characterlist;
world->characterlist = malloc(sizeof(character_t));
if (world->characterlist == NULL) exit(EXIT_FAILURE);
return world->characterlist;
}
static void fill_character_details(character_t *character, const char *name)
{
if (!character) return;
character->id = world->next_character_id;
world->next_character_id++;
strcpy(character->name, name);
character->money = 0;
character->rank = 0;
character->prev = NULL;
character->next = NULL;
character->birthdate.tm_year = world->current_time.tm_year;
character->birthdate.tm_mon = world->current_time.tm_mon;
character->lord = NULL;
character->heir = NULL;
set_expected_age(character);
}
character_t *add_character(const char *name)
{
if (world->characterlist == NULL) {
world->characterlist = create_characterlist();
fill_character_details(world->characterlist, name);
return world->characterlist;
}
character_t *current = world->characterlist;
/*fast-forward to the end of list */
while (current->next != NULL) {
current = current->next;
}
/* now we can add a new variable */
current->next = malloc(sizeof(character_t));
if (!current->next) exit(EXIT_FAILURE);
fill_character_details(current->next, name);
current->next->prev = current;
return current->next;
}
character_t *add_character_before(character_t *parent, const char *name)
{
if (world->characterlist == NULL) {
world->characterlist = create_characterlist();
fill_character_details(world->characterlist, name);
return world->characterlist;
}
character_t *current = world->characterlist;
/*fast-forward to parent or to the end of list*/
while (current != parent && current->next != NULL) {
current = current->next;
}
/* now we can add a new variable */
character_t *new = malloc(sizeof(character_t));
if (!new) exit(EXIT_FAILURE);
fill_character_details(new, name);
if (current->prev) {
current->prev->next = new;
new->prev = current->prev;
}
else world->characterlist = new;
new->next = current;
current->prev = new;
return new;
}
void remove_character(character_t *character)
{
if (character == NULL)
return;
/* remove all pieces that belong to this character */
piece_t *current_piece = world->piecelist;
piece_t *previous_piece = NULL;
while (current_piece != NULL) {
previous_piece = current_piece;
current_piece = current_piece->next;
if (previous_piece->owner->id == character->id) {
remove_piece(previous_piece);
}
}
/* free all regions that belong to this character */
region_t *current_region = world->regionlist;
while (current_region != NULL) {
if (current_region->owner != NULL
&& current_region->owner->id == character->id) {
current_region->owner = NULL;
}
current_region = current_region->next;
}
/* remove all diplomacy that included this character */
dipstatus_t *dipstatus = world->diplomacylist;
while (dipstatus != NULL) {
if (dipstatus->character1 == character
|| dipstatus->character2 == character) {
dipstatus_t *tmp = dipstatus->next;
remove_diplomacy(dipstatus);
dipstatus = tmp;
} else
dipstatus = dipstatus->next;
}
/* if this character was set as heir or lord to somebody else, remove him */
character_t *current = world->characterlist;
while (current != NULL) {
if (current->heir != NULL && current->heir->id == character->id)
current->heir = NULL;
if (current->lord != NULL && current->lord->id == character->id)
current->lord = NULL;
current = current->next;
}
if (character == world->selected_character) {
if (character->next != NULL) world->selected_character = character->next;
else if (character != world->characterlist) world->selected_character = world->characterlist;
else world->selected_character = NULL;
}
// character_t *prev = NULL;
current = world->characterlist;
while (current != NULL) {
if (current == character) {
if (current->prev) current->prev->next = current->next;
else world->characterlist = current->next;
if (current->next) current->next->prev = current->prev;
character_t *tmp = current;
free(tmp);
return;
}
// prev = current;
current = current->next;
}
}
void clear_character_list()
{
while (world->characterlist != NULL)
remove_character(world->characterlist);
world->next_character_id = 1;
world->characterlist = NULL;
}
uint16_t get_money(character_t *character)
{
/* can be accessed without a function */
return character->money;
}
void set_money(character_t *character, const uint16_t money)
{
if (character == NULL)
return;
if (money > MONEY_MAX) character->money = MONEY_MAX;
else character->money = money;
return;
}
character_t *get_character_by_name(const char *name)
{
character_t *current = world->characterlist;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
return current;
}
if (current->next == NULL)
return NULL;
current = current->next;
}
return NULL;
}
character_t *get_character_by_id(const uint16_t id)
{
character_t *current = world->characterlist;
while (current != NULL) {
if (id == current->id) {
return current;
}
if (current->next == NULL)
return NULL;
current = current->next;
}
return NULL;
}
uint16_t get_character_order(character_t *character)
{
if (character == NULL)
return 0;
uint16_t counter = 0;
character_t *current = world->characterlist;
while (current != NULL && current->id != character->id) {
counter++;
current = current->next;
}
return counter;
}
unsigned char get_character_rank(character_t *character)
{
if (character == NULL)
return 0;
return character->rank;
}
void set_character_rank(character_t *character, unsigned char rank)
{
if (character == NULL)
return;
character->rank = rank;
}
int transfer_money(character_t *source, character_t *destination, const int amount)
{
/* check if source and destination exist. NEEDED? */
if (source == NULL || destination == NULL
|| source->id == destination->id)
return 1;
/* check if source has enough */
if (source->money < amount)
return 1;
/* check if destination will not exceed the limit */
if (destination->money + amount > MONEY_MAX) return 1;
source->money -= amount;
destination->money += amount;
add_to_chronicle("%s granted %i coins to %s.\n", source->name, amount, destination->name);
return 0;
}
inline static void set_expected_age(character_t *character)
{
unsigned int months =
(character->birthdate.tm_year + MIN_AGE) * 12 +
character->birthdate.tm_mon;
months += rand() % ((MAX_AGE - MIN_AGE) * 12);
character->deathdate.tm_year = months / 12;
character->deathdate.tm_mon = months % 12;
}
void set_successor(character_t *character, character_t *successor)
{
if (character == NULL
|| (successor != NULL && character->id == successor->id))
return; /* can not name yourself a heir */
character->heir = successor;
return;
}
uint16_t count_characters()
{
uint16_t count = 0;
character_t *current = world->characterlist;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
int is_gameover()
{
/**
* The game is over if one character, or one sovereign with his vassals,
* is left
**/
character_t *character = NULL;
int nr_sovereigns = 0;
character_t *current = world->characterlist;
while (current != NULL) {
if (current->lord == NULL)
nr_sovereigns++;
current = current->next;
}
if (nr_sovereigns == 1) {
character = get_sovereign(world->characterlist);
add_to_chronicle
("%s is the only sovereign left. The game is over!\n",
character->name);
return 1;
}
return 0;
}
void check_death()
{
world->message[0] = '\0';
if (world->check_death == 0)
return;
/**
* loop through characterlist, check if every character has a noble and
* at least one region
**/
character_t *current_character = world->characterlist;
character_t *next = NULL;
while (current_character != NULL) {
next = current_character->next;
/* if a character loses all his land, he loses the game */
if (count_tiles_by_owner(current_character) == 0) {
add_to_chronicle("%s %s lost all his land.\n",
rank_name[current_character->rank],
current_character->name);
add_to_chronicle(world->message);
succession(current_character);
remove_character(current_character);
}
/* if a character loses his noble, he loses the game */
else if (get_noble_by_owner(current_character) == NULL) {
add_to_chronicle("%s %s was killed.\n",
rank_name[current_character->rank],
current_character->name);
add_to_chronicle(world->message);
succession(current_character);
remove_character(current_character);
}
/* if a character reaches old age, he loses the game */
else if (world->current_time.tm_year +
12 * world->current_time.tm_mon >=
current_character->deathdate.tm_year +
12 * current_character->deathdate.tm_mon) {
add_to_chronicle("%s %s died of old age.\n",
rank_name[current_character->rank],
current_character->name);
add_to_chronicle(world->message);
succession(current_character);
remove_character(current_character);
}
current_character = next;
}
world->check_death = 0;
}
void succession(character_t *character)
{
/**
* First, find the successor.
* If character named a heir, the heir will succeed.
* If the character did not name a heir, but has a lord, the lord will succeed.
* If the character did not name a heir and has no lord, the property is lost.
**/
character_t *heir = NULL;
if (character->heir != NULL) {
heir = character->heir;
add_to_chronicle("%s %s inherited the property of %s %s.\n",
rank_name[heir->rank], heir->name,
rank_name[character->rank], character->name);
} else if (character->lord != NULL) {
heir = character->lord;
add_to_chronicle
("%s %s had not named a heir, and their lord, %s %s, inherited everything.\n",
rank_name[character->rank], character->name, rank_name[heir->rank],
heir->name);
} else {
add_to_chronicle("%s %s had no one to inherit their property.\n",
rank_name[character->rank], character->name);
return;
}
/* the heir always inherits money */
if (heir->money + character->money > MONEY_MAX) heir->money = MONEY_MAX;
else heir->money += character->money;
/* the heir always inherits land */
region_t *current_region = world->regionlist;
while (current_region != NULL) {
if (current_region->owner != NULL
&& current_region->owner->id == character->id)
current_region->owner = heir;
current_region = current_region->next;
}
/* the heir always inherits soldiers */
piece_t *current_piece = world->piecelist;
while (current_piece != NULL) {
if (current_piece->owner->id == character->id
&& current_piece->type == 1)
current_piece->owner = heir;
current_piece = current_piece->next;
}
/* the heir inherits grantor's title, if it's higher than his own */
if (character->rank > heir->rank) {
add_to_chronicle("%s inherited the %s title.\n", heir->name,
rank_name[character->rank]);
heir->rank = character->rank;
}
/* the heir always inherits grantor's vassals */
character_t *vassal = world->characterlist;
while (vassal != NULL) {
if (vassal->lord != NULL && vassal->lord->id == character->id) {
vassal->lord = heir;
set_diplomacy(vassal, character, ALLIANCE);
}
vassal = vassal->next;
}
/* if the heir has no lord, he does not inherit grantor's lord allegiance */
if (heir->lord == NULL)
return;
/* if possible, the heir keeps his lord allegiance */
// piece_t *heir_lord_noble = get_noble_by_owner(heir->lord);
if (character->rank < heir->lord->rank)
return;
/* only if character's new title is incompatible with current lord allegiance, he has to change allegiance */
else {
/* if grantor had no lord, the heir becomes free */
if (character->lord == NULL)
heir->lord = NULL;
else {
add_to_chronicle
("%s %s paid homage to %s %s for their title.\n",
rank_name[heir->rank], heir->name,
rank_name[character->lord->rank], character->lord->name);
homage(heir, character->lord);
}
}
}