author | truelight |
Wed, 15 Dec 2004 22:06:47 +0000 | |
changeset 678 | f62cae2abc0c |
parent 473 | 77f0ef57e7cf |
child 679 | 04ca2cd69420 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include <stdarg.h> |
|
4 |
#include "gfx.h" |
|
5 |
#include "viewport.h" |
|
6 |
#include "command.h" |
|
7 |
#include "vehicle.h" |
|
8 |
||
9 |
byte _map_type_and_height[TILES_X * TILES_Y]; |
|
10 |
byte _map5[TILES_X * TILES_Y]; |
|
11 |
byte _map3_lo[TILES_X * TILES_Y]; |
|
12 |
byte _map3_hi[TILES_X * TILES_Y]; |
|
13 |
byte _map_owner[TILES_X * TILES_Y]; |
|
14 |
byte _map2[TILES_X * TILES_Y]; |
|
15 |
byte _map_extra_bits[TILES_X * TILES_Y/4]; |
|
16 |
||
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
17 |
extern const TileTypeProcs |
0 | 18 |
_tile_type_clear_procs, |
19 |
_tile_type_rail_procs, |
|
20 |
_tile_type_road_procs, |
|
21 |
_tile_type_town_procs, |
|
22 |
_tile_type_trees_procs, |
|
23 |
_tile_type_station_procs, |
|
24 |
_tile_type_water_procs, |
|
25 |
_tile_type_dummy_procs, |
|
26 |
_tile_type_industry_procs, |
|
27 |
_tile_type_tunnelbridge_procs, |
|
28 |
_tile_type_unmovable_procs; |
|
29 |
||
30 |
const TileTypeProcs * const _tile_type_procs[16] = { |
|
31 |
&_tile_type_clear_procs, |
|
32 |
&_tile_type_rail_procs, |
|
33 |
&_tile_type_road_procs, |
|
34 |
&_tile_type_town_procs, |
|
35 |
&_tile_type_trees_procs, |
|
36 |
&_tile_type_station_procs, |
|
37 |
&_tile_type_water_procs, |
|
38 |
&_tile_type_dummy_procs, |
|
39 |
&_tile_type_industry_procs, |
|
40 |
&_tile_type_tunnelbridge_procs, |
|
41 |
&_tile_type_unmovable_procs, |
|
42 |
}; |
|
43 |
||
44 |
/* landscape slope => sprite */ |
|
45 |
const byte _tileh_to_sprite[32] = { |
|
46 |
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0, |
|
47 |
0,0,0,0,0,0,0,16,0,0,0,17,0,15,18,0, |
|
48 |
}; |
|
49 |
||
50 |
uint GetTileSlope(uint tile, int *h) |
|
51 |
{ |
|
52 |
uint a,b,c,d,min; |
|
53 |
int r; |
|
54 |
||
55 |
if (GET_TILE_X(tile) == TILE_X_MAX || GET_TILE_Y(tile) == TILE_Y_MAX) { |
|
56 |
if (h) |
|
57 |
*h = 0; |
|
58 |
return 0; |
|
59 |
} |
|
60 |
||
61 |
assert(tile < TILES_X * TILES_Y && GET_TILE_X(tile) != TILE_X_MAX && GET_TILE_Y(tile) != TILE_Y_MAX); |
|
62 |
||
63 |
min = a = _map_type_and_height[tile] & 0xF; |
|
64 |
b = _map_type_and_height[tile+TILE_XY(1,0)] & 0xF; |
|
65 |
if (min >= b) min = b; |
|
66 |
c = _map_type_and_height[tile+TILE_XY(0,1)] & 0xF; |
|
67 |
if (min >= c) min = c; |
|
68 |
d = _map_type_and_height[tile+TILE_XY(1,1)] & 0xF; |
|
69 |
if (min >= d) min = d; |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
70 |
|
0 | 71 |
r = 0; |
72 |
if ((a-=min)!=0) { r += (--a << 4) + 8; } |
|
73 |
if ((c-=min)!=0) { r += (--c << 4) + 4; } |
|
74 |
if ((d-=min)!=0) { r += (--d << 4) + 2; } |
|
75 |
if ((b-=min)!=0) { r += (--b << 4) + 1; } |
|
76 |
||
77 |
if (h != 0) |
|
78 |
*h = min * 8; |
|
79 |
||
80 |
return r; |
|
81 |
} |
|
82 |
||
83 |
int GetTileZ(uint tile) |
|
84 |
{ |
|
85 |
int h; |
|
86 |
GetTileSlope(tile, &h); |
|
87 |
return h; |
|
88 |
} |
|
89 |
||
90 |
void FindLandscapeHeightByTile(TileInfo *ti, uint tile) |
|
91 |
{ |
|
92 |
if (GET_TILE_X(tile) == TILE_X_MAX || |
|
93 |
GET_TILE_Y(tile) == TILE_Y_MAX) { |
|
94 |
ti->tileh = 0; |
|
95 |
ti->type = MP_STRANGE; |
|
96 |
ti->tile = 0; |
|
97 |
ti->map5 = 0; |
|
98 |
ti->z = 0; |
|
99 |
return; |
|
100 |
} |
|
101 |
||
102 |
ti->tile = tile; |
|
103 |
ti->map5 = _map5[tile]; |
|
104 |
ti->type = GET_TILETYPE(tile); |
|
105 |
ti->tileh = GetTileSlope(tile, &ti->z); |
|
106 |
// ti->z = min * 8; |
|
107 |
} |
|
108 |
||
109 |
/* find the landscape height for the coordinates x y */ |
|
110 |
void FindLandscapeHeight(TileInfo *ti, uint x, uint y) |
|
111 |
{ |
|
112 |
int tile; |
|
113 |
||
114 |
ti->x = x; |
|
115 |
ti->y = y; |
|
116 |
||
117 |
if (x >= TILE_X_MAX*16-1 || y >= TILE_Y_MAX*16-1) { |
|
118 |
ti->tileh = 0; |
|
119 |
ti->type = MP_STRANGE; |
|
120 |
ti->tile = 0; |
|
121 |
ti->map5 = 0; |
|
122 |
ti->z = 0; |
|
123 |
return; |
|
124 |
} |
|
125 |
||
126 |
tile = TILE_FROM_XY(x,y); |
|
127 |
FindLandscapeHeightByTile(ti, tile); |
|
128 |
} |
|
129 |
||
130 |
uint GetPartialZ(int x, int y, int corners) |
|
131 |
{ |
|
132 |
int z = 0; |
|
133 |
||
134 |
switch(corners) { |
|
135 |
case 1: |
|
136 |
if (x - y >= 0) |
|
137 |
z = (x - y) >> 1; |
|
138 |
break; |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
139 |
|
0 | 140 |
case 2: |
141 |
y^=0xF; |
|
142 |
if ( (x - y) >= 0) |
|
143 |
z = (x - y) >> 1; |
|
144 |
break; |
|
145 |
||
146 |
case 3: |
|
147 |
z = (x>>1) + 1; |
|
148 |
break; |
|
149 |
||
150 |
case 4: |
|
151 |
if (y - x >= 0) |
|
152 |
z = (y - x) >> 1; |
|
153 |
break; |
|
154 |
||
155 |
case 5: |
|
156 |
case 10: |
|
157 |
case 15: |
|
158 |
z = 4; |
|
159 |
break; |
|
160 |
||
161 |
case 6: |
|
162 |
z = (y>>1) + 1; |
|
163 |
break; |
|
164 |
||
165 |
case 7: |
|
166 |
z = 8; |
|
167 |
y^=0xF; |
|
168 |
if (x - y < 0) |
|
169 |
z += (x - y) >> 1; |
|
170 |
break; |
|
171 |
||
172 |
case 8: |
|
173 |
y ^= 0xF; |
|
174 |
if (y - x >= 0) |
|
175 |
z = (y - x) >> 1; |
|
176 |
break; |
|
177 |
||
178 |
case 9: |
|
179 |
z = (y^0xF)>>1; |
|
180 |
break; |
|
181 |
||
182 |
case 11: |
|
183 |
z = 8; |
|
184 |
if (x - y < 0) |
|
185 |
z += (x - y) >> 1; |
|
186 |
break; |
|
187 |
||
188 |
case 12: |
|
189 |
z = (x^0xF)>>1; |
|
190 |
break; |
|
191 |
||
192 |
case 13: |
|
193 |
z = 8; |
|
194 |
y ^= 0xF; |
|
195 |
if (y - x < 0) |
|
196 |
z += (y - x) >> 1; |
|
197 |
break; |
|
198 |
||
199 |
case 14: |
|
200 |
z = 8; |
|
201 |
if (y - x < 0) |
|
202 |
z += (y - x) >> 1; |
|
203 |
break; |
|
204 |
||
205 |
case 23: |
|
206 |
z = 1 + ((x+y)>>1); |
|
207 |
break; |
|
208 |
||
209 |
case 27: |
|
210 |
z = 1 + ((x+(y^0xF))>>1); |
|
211 |
break; |
|
212 |
||
213 |
case 29: |
|
214 |
z = 1 + (((x^0xF)+(y^0xF))>>1); |
|
215 |
break; |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
216 |
|
0 | 217 |
case 30: |
218 |
z = 1 + (((x^0xF)+(y^0xF))>>1); |
|
219 |
break; |
|
220 |
} |
|
221 |
||
222 |
return z; |
|
223 |
} |
|
224 |
||
225 |
uint GetSlopeZ(int x, int y) |
|
226 |
{ |
|
227 |
TileInfo ti; |
|
228 |
// int z; |
|
229 |
||
230 |
FindLandscapeHeight(&ti, x, y); |
|
231 |
||
232 |
/* |
|
233 |
z = ti.z; |
|
234 |
x &= 0xF; |
|
235 |
y &= 0xF; |
|
236 |
||
237 |
||
238 |
assert(z < 256); |
|
239 |
*/ |
|
240 |
return _tile_type_procs[ti.type]->get_slope_z_proc(&ti); |
|
241 |
} |
|
242 |
||
39 | 243 |
// direction=true: check for foundation in east and south corner |
244 |
// direction=false: check for foundation in west and south corner |
|
245 |
bool hasFoundation(TileInfo *ti, bool direction) |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
246 |
{ |
39 | 247 |
bool south, other; // southern corner and east/west corner |
248 |
uint slope = _tile_type_procs[ti->type]->get_slope_tileh_proc(ti); |
|
50 | 249 |
uint tileh = ti->tileh; |
250 |
||
251 |
if(slope==0 && slope!=tileh) tileh=15; |
|
252 |
south = (tileh & 2) != (slope & 2); |
|
39 | 253 |
|
254 |
if(direction) |
|
50 | 255 |
other = (tileh & 4) != (slope & 4); |
39 | 256 |
else |
50 | 257 |
other = (tileh & 1) != (slope & 1); |
258 |
return south || other; |
|
39 | 259 |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
260 |
} |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
261 |
|
0 | 262 |
void DrawFoundation(TileInfo *ti, uint f) |
263 |
{ |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
264 |
uint32 sprite_base = SPR_SLOPES_BASE-14; |
39 | 265 |
|
266 |
TileInfo ti2; |
|
267 |
FindLandscapeHeight(&ti2, ti->x, ti->y-1); |
|
268 |
if(hasFoundation( &ti2, true )) sprite_base += 22; // foundation in NW direction |
|
269 |
FindLandscapeHeight(&ti2, ti->x-1, ti->y); |
|
270 |
if(hasFoundation( &ti2, false )) sprite_base += 22*2; // foundation in NE direction |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
271 |
|
0 | 272 |
if (f < 15) { |
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
273 |
// leveled foundation |
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
274 |
if( sprite_base < SPR_SLOPES_BASE ) sprite_base = 990; // use original slope sprites |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
275 |
|
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
276 |
AddSortableSpriteToDraw(f-1 + sprite_base, ti->x, ti->y, 16, 16, 7, ti->z); |
0 | 277 |
ti->z += 8; |
278 |
ti->tileh = 0; |
|
279 |
OffsetGroundSprite(31, 1); |
|
280 |
} else { |
|
281 |
// inclined foundation |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
282 |
sprite_base += 14; |
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
283 |
|
0 | 284 |
AddSortableSpriteToDraw( |
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
285 |
HASBIT( (1<<1) | (1<<2) | (1<<4) | (1<<8), ti->tileh) ? sprite_base + (f - 15) : ti->tileh + 0x3DE - 1, |
0 | 286 |
ti->x, ti->y, 1, 1, 1, ti->z |
287 |
); |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
288 |
|
0 | 289 |
ti->tileh = _inclined_tileh[f - 15]; |
290 |
OffsetGroundSprite(31, 9); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
void DoClearSquare(uint tile) |
|
295 |
{ |
|
296 |
ModifyTile(tile, |
|
297 |
MP_SETTYPE(MP_CLEAR) | |
|
298 |
MP_MAP2_CLEAR | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | MP_MAPOWNER | MP_MAP5, |
|
299 |
OWNER_NONE, /* map_owner */ |
|
300 |
_generating_world ? 3 : 0 /* map5 */ |
|
301 |
); |
|
302 |
} |
|
303 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
131
diff
changeset
|
304 |
uint32 GetTileTrackStatus(uint tile, TransportType mode) |
0 | 305 |
{ |
306 |
return _tile_type_procs[GET_TILETYPE(tile)]->get_tile_track_status_proc(tile, mode); |
|
307 |
} |
|
308 |
||
309 |
void ChangeTileOwner(uint tile, byte old_player, byte new_player) |
|
310 |
{ |
|
311 |
_tile_type_procs[GET_TILETYPE(tile)]->change_tile_owner_proc(tile, old_player, new_player); |
|
312 |
} |
|
313 |
||
473
77f0ef57e7cf
(svn r724) Remove restriction that a tile can only accept 3 cargo types.
tron
parents:
183
diff
changeset
|
314 |
void GetAcceptedCargo(uint tile, AcceptedCargo ac) |
0 | 315 |
{ |
316 |
memset(ac, 0, sizeof(AcceptedCargo)); |
|
317 |
_tile_type_procs[GET_TILETYPE(tile)]->get_accepted_cargo_proc(tile, ac); |
|
318 |
} |
|
319 |
||
320 |
void AnimateTile(uint tile) |
|
321 |
{ |
|
322 |
_tile_type_procs[GET_TILETYPE(tile)]->animate_tile_proc(tile); |
|
323 |
} |
|
324 |
||
325 |
void ClickTile(uint tile) |
|
326 |
{ |
|
327 |
_tile_type_procs[GET_TILETYPE(tile)]->click_tile_proc(tile); |
|
328 |
} |
|
329 |
||
330 |
void DrawTile(TileInfo *ti) |
|
331 |
{ |
|
332 |
_tile_type_procs[ti->type]->draw_tile_proc(ti); |
|
333 |
} |
|
334 |
||
335 |
void GetTileDesc(uint tile, TileDesc *td) |
|
336 |
{ |
|
337 |
_tile_type_procs[GET_TILETYPE(tile)]->get_tile_desc_proc(tile, td); |
|
338 |
} |
|
339 |
||
340 |
/* Clear a piece of landscape |
|
341 |
* p1 = 0, |
|
342 |
* p2 = 0 |
|
343 |
*/ |
|
344 |
||
345 |
int32 CmdLandscapeClear(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
346 |
{ |
|
347 |
uint tile; |
|
348 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
|
349 |
||
350 |
tile = TILE_FROM_XY(x,y); |
|
351 |
return _tile_type_procs[GET_TILETYPE(tile)]->clear_tile_proc(tile, flags); |
|
352 |
} |
|
353 |
||
354 |
// p1 = end tile |
|
355 |
int32 CmdClearArea(int ex, int ey, uint32 flags, uint32 p1, uint32 p2) |
|
356 |
{ |
|
357 |
int32 cost,ret, money; |
|
358 |
int sx,sy; |
|
359 |
int x,y; |
|
360 |
bool success = false; |
|
361 |
||
362 |
// make sure sx,sy are smaller than ex,ey |
|
363 |
sx = GET_TILE_X(p1)*16; |
|
364 |
sy = GET_TILE_Y(p1)*16; |
|
365 |
if (ex < sx) intswap(ex, sx); |
|
366 |
if (ey < sy) intswap(ey, sy); |
|
367 |
||
368 |
money = GetAvailableMoneyForCommand(); |
|
369 |
cost = 0; |
|
370 |
||
371 |
for(x=sx; x<=ex; x+=16) { |
|
372 |
for(y=sy; y<=ey; y+=16) { |
|
373 |
ret = DoCommandByTile(TILE_FROM_XY(x,y), 0, 0, flags &~DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
374 |
if (ret == CMD_ERROR) continue; |
|
375 |
cost += ret; |
|
376 |
success = true; |
|
377 |
||
378 |
if (flags & DC_EXEC) { |
|
379 |
if ( ret>0 && (money -= ret) < 0) { |
|
380 |
_additional_cash_required = ret; |
|
381 |
return cost - ret; |
|
382 |
} |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
383 |
DoCommandByTile(TILE_FROM_XY(x,y), 0, 0, flags, CMD_LANDSCAPE_CLEAR); |
0 | 384 |
|
385 |
// draw explosion animation... |
|
386 |
if ((x==sx || x==ex) && (y==sy || y==ey)) { |
|
387 |
// big explosion in each corner, or small explosion for single tiles |
|
388 |
CreateEffectVehicleAbove(x + 8,y + 8, 2, sy==ey && sx==ex ? EV_DEMOLISH : EV_CRASHED_SMOKE); |
|
389 |
} |
|
390 |
} |
|
391 |
} |
|
392 |
} |
|
393 |
||
394 |
if (!success) |
|
395 |
cost = CMD_ERROR; |
|
396 |
return cost; |
|
397 |
} |
|
398 |
||
399 |
||
400 |
/* utility function used to modify a tile */ |
|
401 |
void CDECL ModifyTile(uint tile, uint flags, ...) |
|
402 |
{ |
|
403 |
va_list va; |
|
404 |
int i; |
|
405 |
||
406 |
va_start(va, flags); |
|
407 |
||
408 |
if ((i = (flags >> 8) & 0xF) != 0) { |
|
409 |
_map_type_and_height[tile] = (_map_type_and_height[tile]&~0xF0)|((i-1) << 4); |
|
410 |
} |
|
411 |
||
412 |
if (flags & (MP_MAP2_CLEAR | MP_MAP2)) { |
|
413 |
int x = 0; |
|
414 |
if (flags & MP_MAP2) x = va_arg(va, int); |
|
415 |
_map2[tile] = x; |
|
416 |
} |
|
417 |
||
418 |
if (flags & (MP_MAP3LO_CLEAR | MP_MAP3LO)) { |
|
419 |
int x = 0; |
|
420 |
if (flags & MP_MAP3LO) x = va_arg(va, int); |
|
421 |
_map3_lo[tile] = x; |
|
422 |
} |
|
423 |
||
424 |
if (flags & (MP_MAP3HI_CLEAR | MP_MAP3HI)) { |
|
425 |
int x = 0; |
|
426 |
if (flags & MP_MAP3HI) x = va_arg(va, int); |
|
427 |
_map3_hi[tile] = x; |
|
428 |
} |
|
429 |
||
430 |
if (flags & (MP_MAPOWNER|MP_MAPOWNER_CURRENT)) { |
|
431 |
byte x = _current_player; |
|
432 |
if (flags & MP_MAPOWNER) x = va_arg(va, int); |
|
433 |
_map_owner[tile] = x; |
|
434 |
} |
|
435 |
||
436 |
if (flags & MP_MAP5) { |
|
437 |
_map5[tile] = va_arg(va, int); |
|
438 |
} |
|
439 |
||
440 |
va_end(va); |
|
441 |
||
442 |
if (!(flags & MP_NODIRTY)) |
|
443 |
MarkTileDirtyByTile(tile); |
|
444 |
} |
|
445 |
||
446 |
void SetMapExtraBits(uint tile, byte bits) |
|
447 |
{ |
|
448 |
_map_extra_bits[tile >> 2] &= ~(3 << ((tile&3)*2)); |
|
449 |
_map_extra_bits[tile >> 2] |= (bits&3) << ((tile&3)*2); |
|
450 |
} |
|
451 |
||
452 |
uint GetMapExtraBits(uint tile) |
|
453 |
{ |
|
454 |
return (_map_extra_bits[tile >> 2] >> (tile&3)*2)&3; |
|
455 |
} |
|
456 |
||
457 |
#define TILELOOP_BITS 4 |
|
458 |
#define TILELOOP_SIZE (1 << TILELOOP_BITS) |
|
459 |
#define TILELOOP_ASSERTMASK ((TILELOOP_SIZE-1) + ((TILELOOP_SIZE-1) << TILE_X_BITS)) |
|
460 |
#define TILELOOP_CHKMASK (((1 << (TILE_X_BITS - TILELOOP_BITS))-1) << TILELOOP_BITS) |
|
461 |
||
462 |
void RunTileLoop() |
|
463 |
{ |
|
464 |
uint tile; |
|
465 |
uint count; |
|
466 |
||
467 |
tile = _cur_tileloop_tile; |
|
468 |
||
469 |
assert( (tile & ~TILELOOP_ASSERTMASK) == 0); |
|
470 |
count = (TILES_X/TILELOOP_SIZE) * (TILES_Y/TILELOOP_SIZE); |
|
471 |
do { |
|
472 |
_tile_type_procs[GET_TILETYPE(tile)]->tile_loop_proc(tile); |
|
473 |
||
474 |
if ( GET_TILE_X(tile) < TILES_X - TILELOOP_SIZE) { |
|
475 |
tile += TILELOOP_SIZE; /* no overflow */ |
|
476 |
} else { |
|
477 |
tile = TILE_MASK(tile - TILELOOP_SIZE * (TILES_X/TILELOOP_SIZE-1) + TILE_XY(0, TILELOOP_SIZE)); /* x would overflow, also increase y */ |
|
478 |
} |
|
479 |
} while (--count); |
|
480 |
assert( (tile & ~TILELOOP_ASSERTMASK) == 0); |
|
481 |
||
482 |
tile += 9; |
|
483 |
if (tile & TILELOOP_CHKMASK) |
|
484 |
tile = (tile + TILES_X) & TILELOOP_ASSERTMASK; |
|
485 |
_cur_tileloop_tile = tile; |
|
486 |
} |
|
487 |
||
488 |
void InitializeLandscape() |
|
489 |
{ |
|
490 |
int i; |
|
491 |
||
492 |
memset(_map_owner, OWNER_NONE, sizeof(_map_owner)); |
|
493 |
memset(_map2, 0, sizeof(_map2)); |
|
494 |
memset(_map3_lo, 0, sizeof(_map3_lo)); |
|
495 |
memset(_map3_hi, 0, sizeof(_map3_hi)); |
|
496 |
memset(_map_extra_bits, 0, sizeof(_map_extra_bits)); |
|
497 |
memset(_map_type_and_height, MP_WATER << 4, sizeof(_map_type_and_height)); |
|
498 |
||
499 |
for(i=0; i!=TILES_Y-1; i++) |
|
500 |
memset(_map_type_and_height + i*TILES_X, 0, TILES_X-1); |
|
501 |
||
502 |
memset(_map5, 3, sizeof(_map5)); |
|
503 |
} |
|
504 |
||
505 |
void ConvertGroundTilesIntoWaterTiles() |
|
506 |
{ |
|
507 |
uint tile = 0; |
|
508 |
int h; |
|
509 |
||
510 |
while(true) { |
|
511 |
if (IS_TILETYPE(tile, MP_CLEAR) && GetTileSlope(tile, &h) == 0 && h == 0) { |
|
512 |
_map_type_and_height[tile] = MP_WATER << 4; |
|
513 |
_map5[tile] = 0; |
|
514 |
_map_owner[tile] = OWNER_WATER; |
|
515 |
} |
|
516 |
tile++; |
|
517 |
if (GET_TILE_X(tile) == TILE_X_MAX) { |
|
518 |
tile += TILE_XY(-TILE_X_MAX, 1); |
|
519 |
if (GET_TILE_Y(tile) == TILE_Y_MAX) |
|
520 |
break; |
|
521 |
} |
|
522 |
} |
|
523 |
} |
|
524 |
||
525 |
static const byte _genterrain_tbl_1[5] = { 10, 22, 33, 37, 4 }; |
|
526 |
static const byte _genterrain_tbl_2[5] = { 0, 0, 0, 0, 33 }; |
|
527 |
||
528 |
static void GenerateTerrain(int type, int flag) |
|
529 |
{ |
|
530 |
uint32 r; |
|
531 |
int x,y; |
|
532 |
int w,h; |
|
533 |
byte *p,*tile; |
|
534 |
byte direction; |
|
535 |
||
536 |
r = Random(); |
|
537 |
p = GetSpritePtr((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845); |
|
538 |
||
539 |
x = r & TILE_X_MAX; |
|
540 |
y = (r >> TILE_X_BITS) & TILE_Y_MAX; |
|
541 |
||
542 |
||
543 |
if (x < 2 || y < 2) |
|
544 |
return; |
|
545 |
||
546 |
direction = (byte)(r >> 22) & 3; |
|
547 |
w = p[2]; |
|
548 |
h = p[1]; |
|
549 |
if (direction & 1) { w = p[1]; h = p[2]; } |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
550 |
p += 8; |
0 | 551 |
|
552 |
if (flag & 4) { |
|
553 |
if (!(flag & 2)) { |
|
554 |
if (!(flag & 1)) { |
|
555 |
if (x + y > 190) |
|
556 |
return; |
|
557 |
} else { |
|
558 |
if (y < 30 + x) |
|
559 |
return; |
|
560 |
} |
|
561 |
} else { |
|
562 |
if (!(flag & 1)) { |
|
563 |
if (x + y < 256) |
|
564 |
return; |
|
565 |
} else { |
|
566 |
if (x < 30 + y) |
|
567 |
return; |
|
568 |
} |
|
569 |
} |
|
570 |
} |
|
571 |
||
572 |
if (x + w >= TILE_X_MAX-1) |
|
573 |
return; |
|
574 |
||
575 |
if (y + h >= TILE_Y_MAX-1) |
|
576 |
return; |
|
577 |
||
578 |
tile = &_map_type_and_height[TILE_XY(x,y)]; |
|
579 |
||
580 |
if (direction == 0) { |
|
581 |
do { |
|
582 |
int w_cur = w; |
|
583 |
byte *tile_cur = tile; |
|
584 |
do { |
|
585 |
if (*p >= *tile_cur) *tile_cur = *p; |
|
586 |
p++; |
|
587 |
tile_cur++; |
|
588 |
} while (--w_cur != 0); |
|
589 |
tile += TILE_XY(0,1); |
|
590 |
} while (--h != 0); |
|
591 |
} else if (direction == 1) { |
|
592 |
do { |
|
593 |
int h_cur = h; |
|
594 |
byte *tile_cur = tile; |
|
595 |
do { |
|
596 |
if (*p >= *tile_cur) *tile_cur = *p; |
|
597 |
p++; |
|
598 |
tile_cur+=TILE_XY(0,1); |
|
599 |
} while (--h_cur != 0); |
|
600 |
tile++; |
|
601 |
} while (--w != 0); |
|
602 |
} else if (direction == 2) { |
|
603 |
tile += w - 1; |
|
604 |
do { |
|
605 |
int w_cur = w; |
|
606 |
byte *tile_cur = tile; |
|
607 |
do { |
|
608 |
if (*p >= *tile_cur) *tile_cur = *p; |
|
609 |
p++; |
|
610 |
tile_cur--; |
|
611 |
} while (--w_cur != 0); |
|
612 |
tile += TILE_XY(0,1); |
|
613 |
} while (--h != 0); |
|
614 |
} else { |
|
615 |
tile += (h - 1) * TILE_XY(0,1); |
|
616 |
do { |
|
617 |
int h_cur = h; |
|
618 |
byte *tile_cur = tile; |
|
619 |
do { |
|
620 |
if (*p >= *tile_cur) *tile_cur = *p; |
|
621 |
p++; |
|
622 |
tile_cur-=TILE_XY(0,1); |
|
623 |
} while (--h_cur != 0); |
|
624 |
tile++; |
|
625 |
} while (--w != 0); |
|
626 |
} |
|
627 |
} |
|
628 |
||
629 |
||
630 |
#include "table/genland.h" |
|
631 |
||
632 |
static void CreateDesertOrRainForest() |
|
633 |
{ |
|
634 |
uint tile; |
|
635 |
const TileIndexDiff *data; |
|
636 |
byte mt; |
|
637 |
int i; |
|
638 |
||
639 |
tile = 0; |
|
640 |
do { |
|
641 |
data = _make_desert_or_rainforest_data; |
|
642 |
do { |
|
643 |
if ((i = *data++) == MDORD_LAST) { |
|
644 |
SetMapExtraBits(tile, 1); |
|
645 |
break; |
|
646 |
} |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
647 |
mt = _map_type_and_height[TILE_MASK(tile + i)]; |
0 | 648 |
} while ((mt & 0xC) == 0 && (mt >> 4) != MP_WATER); |
649 |
} while (++tile != TILES_X*TILES_Y); |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
650 |
|
0 | 651 |
for(i=0; i!=256; i++) |
652 |
RunTileLoop(); |
|
653 |
||
654 |
tile = 0; |
|
655 |
do { |
|
656 |
data = _make_desert_or_rainforest_data; |
|
657 |
do { |
|
658 |
if ((i = *data++) == MDORD_LAST) { |
|
659 |
SetMapExtraBits(tile, 2); |
|
660 |
break; |
|
661 |
} |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
662 |
} while ( !IS_TILETYPE(TILE_MASK(tile+i), MP_CLEAR) || (_map5[TILE_MASK(tile + i)]&0x1C) != 0x14); |
0 | 663 |
} while (++tile != TILES_X*TILES_Y); |
664 |
} |
|
665 |
||
666 |
void GenerateLandscape() |
|
667 |
{ |
|
668 |
int i,flag; |
|
669 |
uint32 r; |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
670 |
|
0 | 671 |
if (_opt.landscape == LT_HILLY) { |
672 |
i = ((Random() & 0x7F) + 950) * LANDSCAPE_SIZE_FACTOR; |
|
673 |
do { |
|
674 |
GenerateTerrain(2, 0); |
|
675 |
} while (--i); |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
676 |
|
0 | 677 |
r = Random(); |
678 |
flag = (r & 3) | 4; |
|
679 |
i = (((r >> 16) & 0x7F) + 450) * LANDSCAPE_SIZE_FACTOR; |
|
680 |
do { |
|
681 |
GenerateTerrain(4, flag); |
|
682 |
} while (--i); |
|
683 |
} else if (_opt.landscape == LT_DESERT) { |
|
684 |
i = ((Random()&0x7F) + 170) * LANDSCAPE_SIZE_FACTOR; |
|
685 |
do { |
|
686 |
GenerateTerrain(0, 0); |
|
687 |
} while (--i); |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
688 |
|
0 | 689 |
r = Random(); |
690 |
flag = (r & 3) | 4; |
|
691 |
i = (((r >> 16) & 0xFF) + 1700) * LANDSCAPE_SIZE_FACTOR; |
|
692 |
do { |
|
693 |
GenerateTerrain(0, flag); |
|
694 |
} while (--i); |
|
695 |
||
696 |
flag ^= 2; |
|
697 |
||
698 |
i = ((Random() & 0x7F) + 410) * LANDSCAPE_SIZE_FACTOR; |
|
699 |
do { |
|
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
700 |
GenerateTerrain(3, flag); |
0 | 701 |
} while (--i); |
702 |
} else { |
|
703 |
i = ((Random() & 0x7F) + (3 - _opt.diff.quantity_sea_lakes)*256 + 100) * LANDSCAPE_SIZE_FACTOR; |
|
704 |
do { |
|
705 |
GenerateTerrain(_opt.diff.terrain_type, 0); |
|
706 |
} while (--i); |
|
707 |
} |
|
708 |
||
709 |
ConvertGroundTilesIntoWaterTiles(); |
|
710 |
||
711 |
if (_opt.landscape == LT_DESERT) |
|
712 |
CreateDesertOrRainForest(); |
|
713 |
} |
|
714 |
||
715 |
void OnTick_Town(); |
|
716 |
void OnTick_Trees(); |
|
717 |
void OnTick_Station(); |
|
718 |
void OnTick_Industry(); |
|
719 |
||
720 |
void OnTick_Players(); |
|
721 |
void OnTick_Train(); |
|
722 |
||
723 |
void CallLandscapeTick() |
|
724 |
{ |
|
725 |
OnTick_Town(); |
|
726 |
OnTick_Trees(); |
|
727 |
OnTick_Station(); |
|
728 |
OnTick_Industry(); |
|
729 |
||
730 |
OnTick_Players(); |
|
731 |
OnTick_Train(); |
|
732 |
} |
|
733 |
||
734 |
TileIndex AdjustTileCoordRandomly(TileIndex a, byte rng) |
|
735 |
{ |
|
736 |
int rn = rng; |
|
737 |
uint32 r = Random(); |
|
738 |
||
739 |
return TILE_XY( |
|
740 |
GET_TILE_X(a) + ((byte)r * rn * 2 >> 8) - rn, |
|
741 |
GET_TILE_Y(a) + ((byte)(r>>8) * rn * 2 >> 8) - rn |
|
742 |
); |
|
743 |
} |
|
744 |
||
131
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
745 |
// This function checks if we add addx/addy to tile, if we |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
746 |
// do wrap around the edges. For example, tile = (10,2) and |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
747 |
// addx = +3 and addy = -4. This function will now return |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
748 |
// TILE_WRAPPED, because the y is wrapped. This is needed in |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
749 |
// for example, farmland. When the tile is not wrapped, |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
750 |
// the result will be tile + TILE_XY(addx, addy) |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
751 |
uint TileAddWrap(TileIndex tile, int addx, int addy) |
0 | 752 |
{ |
131
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
753 |
int x, y; |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
754 |
x = GET_TILE_X(tile) + addx; |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
755 |
y = GET_TILE_Y(tile) + addy; |
183
ec2b02ea4c88
(svn r184) -Fix: starting a new game in DesertLandscape crashed the game
truelight
parents:
159
diff
changeset
|
756 |
|
131
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
757 |
// Are we about to wrap? |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
758 |
if (x > 0 && x < TILE_X_MAX && y > 0 && y < TILE_Y_MAX) |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
759 |
return tile + TILE_XY(addx, addy); |
16e59d059384
(svn r132) -Fix: [1014278] TileAddWrap() gave wrong results. Fixed now.
truelight
parents:
50
diff
changeset
|
760 |
|
0 | 761 |
return TILE_WRAPPED; |
762 |
} |
|
763 |
||
764 |
bool IsValidTile(uint tile) |
|
765 |
{ |
|
766 |
return (tile < TILES_X * TILE_Y_MAX && GET_TILE_X(tile) != TILE_X_MAX); |
|
767 |
} |