forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.cpp
211 lines (170 loc) · 4.58 KB
/
gateway.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
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 Warzone 2100 Project
Warzone 2100 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.
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Gateway.c
*
* Routing gateway code.
*
*/
#include "lib/framework/frame.h"
#include "lib/framework/listmacs.h"
#include "map.h"
#include "wrappers.h"
#include "gateway.h"
/// the list of gateways on the current map
static GATEWAY *psGateways;
/******************************************************************************************************/
/* Gateway data access functions */
// get the size of the map
static SDWORD gwMapWidth(void)
{
return (SDWORD)mapWidth;
}
static SDWORD gwMapHeight(void)
{
return (SDWORD)mapHeight;
}
// set the gateway flag on a tile
static void gwSetGatewayFlag(SDWORD x, SDWORD y)
{
mapTile((UDWORD)x,(UDWORD)y)->tileInfoBits |= BITS_GATEWAY;
}
// clear the gateway flag on a tile
static void gwClearGatewayFlag(SDWORD x, SDWORD y)
{
mapTile((UDWORD)x,(UDWORD)y)->tileInfoBits &= ~BITS_GATEWAY;
}
/******************************************************************************************************/
/* Gateway functions */
// Initialise the gateway system
bool gwInitialise(void)
{
ASSERT( psGateways == NULL, "gwInitialise: gateway list has not been reset" );
psGateways = NULL;
return true;
}
// Shutdown the gateway system
void gwShutDown(void)
{
GATEWAY *psNext;
while (psGateways != NULL)
{
psNext = psGateways->psNext;
gwFreeGateway(psGateways);
psGateways = psNext;
}
}
// Add a gateway to the system
bool gwNewGateway(SDWORD x1, SDWORD y1, SDWORD x2, SDWORD y2)
{
GATEWAY *psNew;
SDWORD pos, temp;
ASSERT_OR_RETURN(false, x1 >= 0 && x1 < gwMapWidth() && y1 >= 0 && y1 < gwMapHeight()
&& x2 >= 0 && x2 < gwMapWidth() && y2 >= 0 && y2 < gwMapHeight()
&& (x1 == x2 || y1 == y2), "Invalid gateway coordinates (%d, %d, %d, %d)",
x1, y1, x2, y2);
psNew = (GATEWAY*)malloc(sizeof(GATEWAY));
// make sure the first coordinate is always the smallest
if (x2 < x1)
{
// y is the same, swap x
temp = x2;
x2 = x1;
x1 = temp;
}
else if (y2 < y1)
{
// x is the same, swap y
temp = y2;
y2 = y1;
y1 = temp;
}
// Initialise the gateway, correct out-of-map gateways
psNew->x1 = MAX(3, x1);
psNew->y1 = MAX(3, y1);
psNew->x2 = MIN(x2, mapWidth - 4);
psNew->y2 = MIN(y2, mapHeight - 4);
// add the gateway to the list
psNew->psNext = psGateways;
psGateways = psNew;
// set the map flags
if (psNew->x1 == psNew->x2)
{
// vertical gateway
for(pos = psNew->y1; pos <= psNew->y2; pos++)
{
gwSetGatewayFlag(psNew->x1, pos);
}
}
else
{
// horizontal gateway
for(pos = psNew->x1; pos <= psNew->x2; pos++)
{
gwSetGatewayFlag(pos, psNew->y1);
}
}
return true;
}
// Return the number of gateways.
UDWORD gwNumGateways(void)
{
GATEWAY *psCurr;
UDWORD NumGateways = 0;
for(psCurr = psGateways; psCurr; psCurr = psCurr->psNext)
{
NumGateways++;
}
return NumGateways;
}
GATEWAY *gwGetGateways(void)
{
return psGateways;
}
// Release a gateway
void gwFreeGateway(GATEWAY *psDel)
{
SDWORD pos;
LIST_REMOVE(psGateways, psDel, GATEWAY);
if (psMapTiles) // this lines fixes the bug where we were closing the gateways after freeing the map
{
// clear the map flags
if (psDel->x1 == psDel->x2)
{
// vertical gateway
for(pos = psDel->y1; pos <= psDel->y2; pos++)
{
gwClearGatewayFlag(psDel->x1, pos);
}
}
else
{
// horizontal gateway
for(pos = psDel->x1; pos <= psDel->x2; pos++)
{
gwClearGatewayFlag(pos, psDel->y1);
}
}
}
free(psDel);
}
void gwSetGateways(GATEWAY *ps)
{
// Notice that we do not recreate the gateway flags on tiles here. This is because we usually use this
// function to swap main and mission maps, where it will not be necessary.
psGateways = ps;
}