-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.c
164 lines (146 loc) · 3.97 KB
/
piece.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
#include <stdlib.h>
#include <stdio.h> /* for printf */
#include <string.h>
#include "piece.h"
#include "map.h"
#include "world.h"
char *const piece_name[] = {
[NOBLE] = "noble",
[SOLDIER] = "soldier",
[SHIP] = "ship"
};
//uint16_t piece_id = 0;
//piece_t *piecelist = NULL;
piece_t *create_piecelist()
{
/* single instance */
if (world->piecelist != NULL)
return world->piecelist;
world->piecelist = calloc(sizeof(piece_t), 1);
if (world->piecelist == NULL) exit(EXIT_FAILURE);
return world->piecelist;
}
static void fill_piece_details(piece_t * piece, const enum piece_type type,
const uint16_t height, const uint16_t width,
character_t * owner)
{
if (!piece) return;
piece->id = world->next_piece_id;
piece->type = type;
piece->tile = world->grid->tiles[height][width];
piece->owner = owner;
world->grid->tiles[height][width]->piece = piece;
world->next_piece_id++;
}
piece_t *add_piece(const enum piece_type type, const uint16_t height,
const uint16_t width, character_t * owner)
{
if (owner == NULL)
return NULL; /* bad owner */
if (world->grid == NULL)
return NULL; /* grid not created */
if (world->grid->tiles[height][width]->walkable != 1)
return NULL; /* unwalkable tile */
if (world->grid->tiles[height][width]->piece != NULL)
return NULL; /* tile occupied */
/* only one noble allowed */
if ((type == 0) && (get_noble_by_owner(owner) != NULL)) return NULL;
piece_t *current = world->piecelist;
if (!current) {
world->piecelist = calloc(sizeof(piece_t), 1);
if (!world->piecelist) exit(EXIT_FAILURE);
fill_piece_details(world->piecelist, type, height, width, owner);
return world->piecelist;
}
/*fast-forward to the end of list */
while (current->next != NULL) {
if (current->owner == owner && current->next->owner != owner) break;
current = current->next;
}
/* now we can add a new variable */
piece_t *next = current->next;
current->next = calloc(sizeof(piece_t), 1);
if (!current->next) exit(EXIT_FAILURE);
fill_piece_details(current->next, type, height, width, owner);
current->next->next = next;
return current->next;
}
piece_t *get_noble_by_owner(character_t * owner)
{
if (owner == NULL)
return NULL;
piece_t *current = world->piecelist;
while (current != NULL) {
if (current->owner->id == owner->id && current->type == 0)
return current;
else
current = current->next;
}
return NULL;
}
piece_t *next_piece(piece_t * start_piece)
{
character_t *active_character = start_piece->owner;
piece_t *current = start_piece;
while (current != NULL) {
if (current->next != NULL)
current = current->next;
else {
current = world->piecelist;
// increment_gametime(); /* cycling around pieces of same character should not change time */
}
if (current->owner->id == active_character->id)
return current;
}
return NULL;
}
void remove_piece(piece_t * piece)
{
/* TODO if it was a noble, remove all character's soldiers, land ownership, diplomacy etc. AND the character himself */
if (world->grid == NULL || piece == NULL || world->piecelist == NULL)
return;
piece_t *prev = NULL;
piece_t *current = world->piecelist;
while (current != NULL) {
if (current == piece) {
if (prev) prev->next = current->next;
else world->piecelist = current->next;
piece_t *tmp = current;
tmp->tile->piece = NULL;
free(tmp);
return;
}
prev = current;
current = current->next;
}
}
void clear_piece_list()
{
if (world->grid == NULL)
return;
while (world->piecelist != NULL)
remove_piece(world->piecelist);
world->next_piece_id = 0;
world->piecelist = NULL;
}
uint16_t count_pieces()
{
uint16_t count = 0;
piece_t *current = world->piecelist;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
uint16_t count_pieces_by_owner(character_t * owner)
{
uint16_t count = 0;
piece_t *current = world->piecelist;
while (current != NULL) {
if (current->owner->id == owner->id && current->type == 1)
count++;
current = current->next;
}
return count;
}