-
Notifications
You must be signed in to change notification settings - Fork 4
/
unit.c
157 lines (140 loc) · 3.19 KB
/
unit.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
#include "unit.h"
#include "region.h"
#include "atlantis.h"
#include <quicklist.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
unit * create_unit(struct faction *f, int no)
{
unit * self;
assert(no>0);
self = (unit *)calloc(1, sizeof(unit));
if (self) {
char name[NAMESIZE];
self->faction = f;
self->no = no;
self->combatspell = -1;
sprintf(name, "Unit %d", no);
unit_setname(self, name);
}
return self;
}
void free_unit(unit *u) {
unit_unstack(u);
ql_foreach(u->orders, free);
ql_free(u->orders);
free(u->litems);
free(u->name_);
free(u->display_);
free(u);
}
void unit_stack(unit* u, unit *stack) {
unit **up = &u->region->units;
unit **sp = &stack->next;
stack = unit_getstack(stack);
while (*up) {
unit *x = *up;
if (x==u) {
break;
}
up = &x->next;
}
assert(u->region);
assert(stack->region==u->region);
if (up!=sp) {
while (*up) {
unit *x = *up;
if (x==u || x->stack==u) {
*up = x->next;
x->next = *sp;
*sp = x;
sp = &x->next;
x->stack = stack;
} else {
break;
}
}
} else {
unit *x;
for (x=u->next;x;x=x->next) {
if (x->stack==u) {
x->stack = stack;
} else {
break;
}
}
u->stack = stack;
}
}
void unit_unstack(unit* u) {
if (u->stack) {
unit ** up = &u->stack->next;
while (*up!=u) {
up = &(*up)->next;
}
*up = u->next;
while (*up) {
unit *x = *up;
if (x->stack!=u->stack) {
break;
}
up = &x->next;
}
u->stack = 0;
u->next = *up;
*up = u;
} else if (u->next && u->next->stack==u) {
unit *stack = u->next;
stack->stack = 0;
for (u = stack->next;u;u=u->next) {
u->stack = stack;
}
}
}
struct unit * unit_getstack(struct unit *u) {
return u->stack ? u->stack : u;
}
const char * unit_getname(const struct unit *self)
{
return self->name_;
}
void unit_setname(struct unit *self, const char *name)
{
assert(name);
self->name_ = (char *)realloc(self->name_, strlen(name)+1);
strcpy(self->name_, name);
}
const char * unit_getdisplay(const struct unit *self)
{
return self->display_;
}
void unit_setdisplay(struct unit *self, const char *display)
{
if (display) {
self->display_ = (char *)realloc(self->display_, strlen(display)+1);
strcpy(self->display_, display);
} else {
free(self->display_);
self->display_ = 0;
}
}
void unit_setbuilding(struct unit *u, struct building *b)
{
if (u->building!=b) {
u->building = b;
if (u->region) {
region_addunit(u->region, u, 0);
}
}
}
void unit_setship(struct unit *u, struct ship *s)
{
if (u->ship!=s) {
u->ship = s;
if (u->region) {
region_addunit(u->region, u, 0);
}
}
}