author | dominik |
Fri, 13 Aug 2004 11:28:59 +0000 | |
changeset 37 | 61bf1df68d82 |
parent 0 | 29654efe3188 |
child 39 | d177340ed556 |
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 |
||
17 |
extern const TileTypeProcs |
|
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; |
|
70 |
||
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; |
|
139 |
||
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; |
|
216 |
||
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 |
||
241 |
return _tile_type_procs[ti.type]->get_slope_z_proc(&ti); |
|
242 |
} |
|
243 |
||
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
244 |
/* TODO: add check if this tile has a foundation or not. Since this can't be done easily with the |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
245 |
current landscape arrays, we might have to add a new TileTypeProc. */ |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
246 |
bool hasFoundation(uint tile) |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
247 |
{ |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
248 |
return true; |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
249 |
} |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
250 |
|
0 | 251 |
void DrawFoundation(TileInfo *ti, uint f) |
252 |
{ |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
253 |
uint32 sprite_base = SPR_SLOPES_BASE-14; |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
254 |
if(hasFoundation( TILE_FROM_XY(ti->x, ti->y-1) )) sprite_base += 22; // foundation in NW direction |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
255 |
if(hasFoundation( TILE_FROM_XY(ti->x-1, ti->y) )) sprite_base += 22*2; // foundation in NE direction |
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
256 |
|
0 | 257 |
if (f < 15) { |
258 |
// leveled foundation |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
259 |
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
|
260 |
|
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
261 |
AddSortableSpriteToDraw(f-1 + sprite_base, ti->x, ti->y, 16, 16, 7, ti->z); |
0 | 262 |
ti->z += 8; |
263 |
ti->tileh = 0; |
|
264 |
OffsetGroundSprite(31, 1); |
|
265 |
} else { |
|
266 |
// inclined foundation |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
267 |
sprite_base += 14; |
0 | 268 |
|
269 |
AddSortableSpriteToDraw( |
|
37
61bf1df68d82
(svn r38) Preliminary slopes graphics fix. Neighboring tile check not done yet
dominik
parents:
0
diff
changeset
|
270 |
HASBIT( (1<<1) | (1<<2) | (1<<4) | (1<<8), ti->tileh) ? sprite_base + (f - 15) : ti->tileh + 0x3DE - 1, |
0 | 271 |
ti->x, ti->y, 1, 1, 1, ti->z |
272 |
); |
|
273 |
||
274 |
ti->tileh = _inclined_tileh[f - 15]; |
|
275 |
OffsetGroundSprite(31, 9); |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
void DoClearSquare(uint tile) |
|
280 |
{ |
|
281 |
ModifyTile(tile, |
|
282 |
MP_SETTYPE(MP_CLEAR) | |
|
283 |
MP_MAP2_CLEAR | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | MP_MAPOWNER | MP_MAP5, |
|
284 |
OWNER_NONE, /* map_owner */ |
|
285 |
_generating_world ? 3 : 0 /* map5 */ |
|
286 |
); |
|
287 |
} |
|
288 |
||
289 |
uint32 GetTileTrackStatus(uint tile, int mode) |
|
290 |
{ |
|
291 |
return _tile_type_procs[GET_TILETYPE(tile)]->get_tile_track_status_proc(tile, mode); |
|
292 |
} |
|
293 |
||
294 |
void ChangeTileOwner(uint tile, byte old_player, byte new_player) |
|
295 |
{ |
|
296 |
_tile_type_procs[GET_TILETYPE(tile)]->change_tile_owner_proc(tile, old_player, new_player); |
|
297 |
} |
|
298 |
||
299 |
void GetAcceptedCargo(uint tile, AcceptedCargo *ac) |
|
300 |
{ |
|
301 |
memset(ac, 0, sizeof(AcceptedCargo)); |
|
302 |
_tile_type_procs[GET_TILETYPE(tile)]->get_accepted_cargo_proc(tile, ac); |
|
303 |
} |
|
304 |
||
305 |
void AnimateTile(uint tile) |
|
306 |
{ |
|
307 |
_tile_type_procs[GET_TILETYPE(tile)]->animate_tile_proc(tile); |
|
308 |
} |
|
309 |
||
310 |
void ClickTile(uint tile) |
|
311 |
{ |
|
312 |
_tile_type_procs[GET_TILETYPE(tile)]->click_tile_proc(tile); |
|
313 |
} |
|
314 |
||
315 |
void DrawTile(TileInfo *ti) |
|
316 |
{ |
|
317 |
_tile_type_procs[ti->type]->draw_tile_proc(ti); |
|
318 |
} |
|
319 |
||
320 |
void GetTileDesc(uint tile, TileDesc *td) |
|
321 |
{ |
|
322 |
_tile_type_procs[GET_TILETYPE(tile)]->get_tile_desc_proc(tile, td); |
|
323 |
} |
|
324 |
||
325 |
/* Clear a piece of landscape |
|
326 |
* p1 = 0, |
|
327 |
* p2 = 0 |
|
328 |
*/ |
|
329 |
||
330 |
int32 CmdLandscapeClear(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
331 |
{ |
|
332 |
uint tile; |
|
333 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
|
334 |
||
335 |
tile = TILE_FROM_XY(x,y); |
|
336 |
return _tile_type_procs[GET_TILETYPE(tile)]->clear_tile_proc(tile, flags); |
|
337 |
} |
|
338 |
||
339 |
// p1 = end tile |
|
340 |
int32 CmdClearArea(int ex, int ey, uint32 flags, uint32 p1, uint32 p2) |
|
341 |
{ |
|
342 |
int32 cost,ret, money; |
|
343 |
int sx,sy; |
|
344 |
int x,y; |
|
345 |
bool success = false; |
|
346 |
||
347 |
// make sure sx,sy are smaller than ex,ey |
|
348 |
sx = GET_TILE_X(p1)*16; |
|
349 |
sy = GET_TILE_Y(p1)*16; |
|
350 |
if (ex < sx) intswap(ex, sx); |
|
351 |
if (ey < sy) intswap(ey, sy); |
|
352 |
||
353 |
money = GetAvailableMoneyForCommand(); |
|
354 |
cost = 0; |
|
355 |
||
356 |
for(x=sx; x<=ex; x+=16) { |
|
357 |
for(y=sy; y<=ey; y+=16) { |
|
358 |
ret = DoCommandByTile(TILE_FROM_XY(x,y), 0, 0, flags &~DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
359 |
if (ret == CMD_ERROR) continue; |
|
360 |
cost += ret; |
|
361 |
success = true; |
|
362 |
||
363 |
if (flags & DC_EXEC) { |
|
364 |
if ( ret>0 && (money -= ret) < 0) { |
|
365 |
_additional_cash_required = ret; |
|
366 |
return cost - ret; |
|
367 |
} |
|
368 |
DoCommandByTile(TILE_FROM_XY(x,y), 0, 0, flags, CMD_LANDSCAPE_CLEAR); |
|
369 |
||
370 |
// draw explosion animation... |
|
371 |
if ((x==sx || x==ex) && (y==sy || y==ey)) { |
|
372 |
// big explosion in each corner, or small explosion for single tiles |
|
373 |
CreateEffectVehicleAbove(x + 8,y + 8, 2, sy==ey && sx==ex ? EV_DEMOLISH : EV_CRASHED_SMOKE); |
|
374 |
} |
|
375 |
} |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
if (!success) |
|
380 |
cost = CMD_ERROR; |
|
381 |
return cost; |
|
382 |
} |
|
383 |
||
384 |
||
385 |
/* utility function used to modify a tile */ |
|
386 |
void CDECL ModifyTile(uint tile, uint flags, ...) |
|
387 |
{ |
|
388 |
va_list va; |
|
389 |
int i; |
|
390 |
||
391 |
va_start(va, flags); |
|
392 |
||
393 |
if ((i = (flags >> 8) & 0xF) != 0) { |
|
394 |
_map_type_and_height[tile] = (_map_type_and_height[tile]&~0xF0)|((i-1) << 4); |
|
395 |
} |
|
396 |
||
397 |
if (flags & (MP_MAP2_CLEAR | MP_MAP2)) { |
|
398 |
int x = 0; |
|
399 |
if (flags & MP_MAP2) x = va_arg(va, int); |
|
400 |
_map2[tile] = x; |
|
401 |
} |
|
402 |
||
403 |
if (flags & (MP_MAP3LO_CLEAR | MP_MAP3LO)) { |
|
404 |
int x = 0; |
|
405 |
if (flags & MP_MAP3LO) x = va_arg(va, int); |
|
406 |
_map3_lo[tile] = x; |
|
407 |
} |
|
408 |
||
409 |
if (flags & (MP_MAP3HI_CLEAR | MP_MAP3HI)) { |
|
410 |
int x = 0; |
|
411 |
if (flags & MP_MAP3HI) x = va_arg(va, int); |
|
412 |
_map3_hi[tile] = x; |
|
413 |
} |
|
414 |
||
415 |
if (flags & (MP_MAPOWNER|MP_MAPOWNER_CURRENT)) { |
|
416 |
byte x = _current_player; |
|
417 |
if (flags & MP_MAPOWNER) x = va_arg(va, int); |
|
418 |
_map_owner[tile] = x; |
|
419 |
} |
|
420 |
||
421 |
if (flags & MP_MAP5) { |
|
422 |
_map5[tile] = va_arg(va, int); |
|
423 |
} |
|
424 |
||
425 |
va_end(va); |
|
426 |
||
427 |
if (!(flags & MP_NODIRTY)) |
|
428 |
MarkTileDirtyByTile(tile); |
|
429 |
} |
|
430 |
||
431 |
void SetMapExtraBits(uint tile, byte bits) |
|
432 |
{ |
|
433 |
_map_extra_bits[tile >> 2] &= ~(3 << ((tile&3)*2)); |
|
434 |
_map_extra_bits[tile >> 2] |= (bits&3) << ((tile&3)*2); |
|
435 |
} |
|
436 |
||
437 |
uint GetMapExtraBits(uint tile) |
|
438 |
{ |
|
439 |
return (_map_extra_bits[tile >> 2] >> (tile&3)*2)&3; |
|
440 |
} |
|
441 |
||
442 |
#define TILELOOP_BITS 4 |
|
443 |
#define TILELOOP_SIZE (1 << TILELOOP_BITS) |
|
444 |
#define TILELOOP_ASSERTMASK ((TILELOOP_SIZE-1) + ((TILELOOP_SIZE-1) << TILE_X_BITS)) |
|
445 |
#define TILELOOP_CHKMASK (((1 << (TILE_X_BITS - TILELOOP_BITS))-1) << TILELOOP_BITS) |
|
446 |
||
447 |
void RunTileLoop() |
|
448 |
{ |
|
449 |
uint tile; |
|
450 |
uint count; |
|
451 |
||
452 |
tile = _cur_tileloop_tile; |
|
453 |
||
454 |
assert( (tile & ~TILELOOP_ASSERTMASK) == 0); |
|
455 |
count = (TILES_X/TILELOOP_SIZE) * (TILES_Y/TILELOOP_SIZE); |
|
456 |
do { |
|
457 |
_tile_type_procs[GET_TILETYPE(tile)]->tile_loop_proc(tile); |
|
458 |
||
459 |
if ( GET_TILE_X(tile) < TILES_X - TILELOOP_SIZE) { |
|
460 |
tile += TILELOOP_SIZE; /* no overflow */ |
|
461 |
} else { |
|
462 |
tile = TILE_MASK(tile - TILELOOP_SIZE * (TILES_X/TILELOOP_SIZE-1) + TILE_XY(0, TILELOOP_SIZE)); /* x would overflow, also increase y */ |
|
463 |
} |
|
464 |
} while (--count); |
|
465 |
assert( (tile & ~TILELOOP_ASSERTMASK) == 0); |
|
466 |
||
467 |
tile += 9; |
|
468 |
if (tile & TILELOOP_CHKMASK) |
|
469 |
tile = (tile + TILES_X) & TILELOOP_ASSERTMASK; |
|
470 |
_cur_tileloop_tile = tile; |
|
471 |
} |
|
472 |
||
473 |
void InitializeLandscape() |
|
474 |
{ |
|
475 |
int i; |
|
476 |
||
477 |
memset(_map_owner, OWNER_NONE, sizeof(_map_owner)); |
|
478 |
memset(_map2, 0, sizeof(_map2)); |
|
479 |
memset(_map3_lo, 0, sizeof(_map3_lo)); |
|
480 |
memset(_map3_hi, 0, sizeof(_map3_hi)); |
|
481 |
memset(_map_extra_bits, 0, sizeof(_map_extra_bits)); |
|
482 |
memset(_map_type_and_height, MP_WATER << 4, sizeof(_map_type_and_height)); |
|
483 |
||
484 |
for(i=0; i!=TILES_Y-1; i++) |
|
485 |
memset(_map_type_and_height + i*TILES_X, 0, TILES_X-1); |
|
486 |
||
487 |
memset(_map5, 3, sizeof(_map5)); |
|
488 |
} |
|
489 |
||
490 |
void ConvertGroundTilesIntoWaterTiles() |
|
491 |
{ |
|
492 |
uint tile = 0; |
|
493 |
int h; |
|
494 |
||
495 |
while(true) { |
|
496 |
if (IS_TILETYPE(tile, MP_CLEAR) && GetTileSlope(tile, &h) == 0 && h == 0) { |
|
497 |
_map_type_and_height[tile] = MP_WATER << 4; |
|
498 |
_map5[tile] = 0; |
|
499 |
_map_owner[tile] = OWNER_WATER; |
|
500 |
} |
|
501 |
tile++; |
|
502 |
if (GET_TILE_X(tile) == TILE_X_MAX) { |
|
503 |
tile += TILE_XY(-TILE_X_MAX, 1); |
|
504 |
if (GET_TILE_Y(tile) == TILE_Y_MAX) |
|
505 |
break; |
|
506 |
} |
|
507 |
} |
|
508 |
} |
|
509 |
||
510 |
static const byte _genterrain_tbl_1[5] = { 10, 22, 33, 37, 4 }; |
|
511 |
static const byte _genterrain_tbl_2[5] = { 0, 0, 0, 0, 33 }; |
|
512 |
||
513 |
static void GenerateTerrain(int type, int flag) |
|
514 |
{ |
|
515 |
uint32 r; |
|
516 |
int x,y; |
|
517 |
int w,h; |
|
518 |
byte *p,*tile; |
|
519 |
byte direction; |
|
520 |
||
521 |
r = Random(); |
|
522 |
p = GetSpritePtr((((r >> 24) * _genterrain_tbl_1[type]) >> 8) + _genterrain_tbl_2[type] + 4845); |
|
523 |
||
524 |
x = r & TILE_X_MAX; |
|
525 |
y = (r >> TILE_X_BITS) & TILE_Y_MAX; |
|
526 |
||
527 |
||
528 |
if (x < 2 || y < 2) |
|
529 |
return; |
|
530 |
||
531 |
direction = (byte)(r >> 22) & 3; |
|
532 |
w = p[2]; |
|
533 |
h = p[1]; |
|
534 |
if (direction & 1) { w = p[1]; h = p[2]; } |
|
535 |
p += 8; |
|
536 |
||
537 |
if (flag & 4) { |
|
538 |
if (!(flag & 2)) { |
|
539 |
if (!(flag & 1)) { |
|
540 |
if (x + y > 190) |
|
541 |
return; |
|
542 |
} else { |
|
543 |
if (y < 30 + x) |
|
544 |
return; |
|
545 |
} |
|
546 |
} else { |
|
547 |
if (!(flag & 1)) { |
|
548 |
if (x + y < 256) |
|
549 |
return; |
|
550 |
} else { |
|
551 |
if (x < 30 + y) |
|
552 |
return; |
|
553 |
} |
|
554 |
} |
|
555 |
} |
|
556 |
||
557 |
if (x + w >= TILE_X_MAX-1) |
|
558 |
return; |
|
559 |
||
560 |
if (y + h >= TILE_Y_MAX-1) |
|
561 |
return; |
|
562 |
||
563 |
tile = &_map_type_and_height[TILE_XY(x,y)]; |
|
564 |
||
565 |
if (direction == 0) { |
|
566 |
do { |
|
567 |
int w_cur = w; |
|
568 |
byte *tile_cur = tile; |
|
569 |
do { |
|
570 |
if (*p >= *tile_cur) *tile_cur = *p; |
|
571 |
p++; |
|
572 |
tile_cur++; |
|
573 |
} while (--w_cur != 0); |
|
574 |
tile += TILE_XY(0,1); |
|
575 |
} while (--h != 0); |
|
576 |
} else if (direction == 1) { |
|
577 |
do { |
|
578 |
int h_cur = h; |
|
579 |
byte *tile_cur = tile; |
|
580 |
do { |
|
581 |
if (*p >= *tile_cur) *tile_cur = *p; |
|
582 |
p++; |
|
583 |
tile_cur+=TILE_XY(0,1); |
|
584 |
} while (--h_cur != 0); |
|
585 |
tile++; |
|
586 |
} while (--w != 0); |
|
587 |
} else if (direction == 2) { |
|
588 |
tile += w - 1; |
|
589 |
do { |
|
590 |
int w_cur = w; |
|
591 |
byte *tile_cur = tile; |
|
592 |
do { |
|
593 |
if (*p >= *tile_cur) *tile_cur = *p; |
|
594 |
p++; |
|
595 |
tile_cur--; |
|
596 |
} while (--w_cur != 0); |
|
597 |
tile += TILE_XY(0,1); |
|
598 |
} while (--h != 0); |
|
599 |
} else { |
|
600 |
tile += (h - 1) * TILE_XY(0,1); |
|
601 |
do { |
|
602 |
int h_cur = h; |
|
603 |
byte *tile_cur = tile; |
|
604 |
do { |
|
605 |
if (*p >= *tile_cur) *tile_cur = *p; |
|
606 |
p++; |
|
607 |
tile_cur-=TILE_XY(0,1); |
|
608 |
} while (--h_cur != 0); |
|
609 |
tile++; |
|
610 |
} while (--w != 0); |
|
611 |
} |
|
612 |
} |
|
613 |
||
614 |
||
615 |
#include "table/genland.h" |
|
616 |
||
617 |
static void CreateDesertOrRainForest() |
|
618 |
{ |
|
619 |
uint tile; |
|
620 |
const TileIndexDiff *data; |
|
621 |
byte mt; |
|
622 |
int i; |
|
623 |
||
624 |
tile = 0; |
|
625 |
do { |
|
626 |
data = _make_desert_or_rainforest_data; |
|
627 |
do { |
|
628 |
if ((i = *data++) == MDORD_LAST) { |
|
629 |
SetMapExtraBits(tile, 1); |
|
630 |
break; |
|
631 |
} |
|
632 |
mt = _map_type_and_height[tile + i]; |
|
633 |
} while ((mt & 0xC) == 0 && (mt >> 4) != MP_WATER); |
|
634 |
} while (++tile != TILES_X*TILES_Y); |
|
635 |
||
636 |
for(i=0; i!=256; i++) |
|
637 |
RunTileLoop(); |
|
638 |
||
639 |
tile = 0; |
|
640 |
do { |
|
641 |
data = _make_desert_or_rainforest_data; |
|
642 |
do { |
|
643 |
if ((i = *data++) == MDORD_LAST) { |
|
644 |
SetMapExtraBits(tile, 2); |
|
645 |
break; |
|
646 |
} |
|
647 |
} while ( !IS_TILETYPE(tile+i, MP_CLEAR) || (_map5[tile + i]&0x1C) != 0x14); |
|
648 |
} while (++tile != TILES_X*TILES_Y); |
|
649 |
} |
|
650 |
||
651 |
void GenerateLandscape() |
|
652 |
{ |
|
653 |
int i,flag; |
|
654 |
uint32 r; |
|
655 |
||
656 |
if (_opt.landscape == LT_HILLY) { |
|
657 |
i = ((Random() & 0x7F) + 950) * LANDSCAPE_SIZE_FACTOR; |
|
658 |
do { |
|
659 |
GenerateTerrain(2, 0); |
|
660 |
} while (--i); |
|
661 |
||
662 |
r = Random(); |
|
663 |
flag = (r & 3) | 4; |
|
664 |
i = (((r >> 16) & 0x7F) + 450) * LANDSCAPE_SIZE_FACTOR; |
|
665 |
do { |
|
666 |
GenerateTerrain(4, flag); |
|
667 |
} while (--i); |
|
668 |
} else if (_opt.landscape == LT_DESERT) { |
|
669 |
i = ((Random()&0x7F) + 170) * LANDSCAPE_SIZE_FACTOR; |
|
670 |
do { |
|
671 |
GenerateTerrain(0, 0); |
|
672 |
} while (--i); |
|
673 |
||
674 |
r = Random(); |
|
675 |
flag = (r & 3) | 4; |
|
676 |
i = (((r >> 16) & 0xFF) + 1700) * LANDSCAPE_SIZE_FACTOR; |
|
677 |
do { |
|
678 |
GenerateTerrain(0, flag); |
|
679 |
} while (--i); |
|
680 |
||
681 |
flag ^= 2; |
|
682 |
||
683 |
i = ((Random() & 0x7F) + 410) * LANDSCAPE_SIZE_FACTOR; |
|
684 |
do { |
|
685 |
GenerateTerrain(3, flag); |
|
686 |
} while (--i); |
|
687 |
} else { |
|
688 |
i = ((Random() & 0x7F) + (3 - _opt.diff.quantity_sea_lakes)*256 + 100) * LANDSCAPE_SIZE_FACTOR; |
|
689 |
do { |
|
690 |
GenerateTerrain(_opt.diff.terrain_type, 0); |
|
691 |
} while (--i); |
|
692 |
} |
|
693 |
||
694 |
ConvertGroundTilesIntoWaterTiles(); |
|
695 |
||
696 |
if (_opt.landscape == LT_DESERT) |
|
697 |
CreateDesertOrRainForest(); |
|
698 |
} |
|
699 |
||
700 |
void OnTick_Town(); |
|
701 |
void OnTick_Trees(); |
|
702 |
void OnTick_Station(); |
|
703 |
void OnTick_Industry(); |
|
704 |
||
705 |
void OnTick_Players(); |
|
706 |
void OnTick_Train(); |
|
707 |
||
708 |
void CallLandscapeTick() |
|
709 |
{ |
|
710 |
OnTick_Town(); |
|
711 |
OnTick_Trees(); |
|
712 |
OnTick_Station(); |
|
713 |
OnTick_Industry(); |
|
714 |
||
715 |
OnTick_Players(); |
|
716 |
OnTick_Train(); |
|
717 |
} |
|
718 |
||
719 |
TileIndex AdjustTileCoordRandomly(TileIndex a, byte rng) |
|
720 |
{ |
|
721 |
int rn = rng; |
|
722 |
uint32 r = Random(); |
|
723 |
||
724 |
return TILE_XY( |
|
725 |
GET_TILE_X(a) + ((byte)r * rn * 2 >> 8) - rn, |
|
726 |
GET_TILE_Y(a) + ((byte)(r>>8) * rn * 2 >> 8) - rn |
|
727 |
); |
|
728 |
} |
|
729 |
||
730 |
uint TileAddWrap(TileIndex tile, int add) |
|
731 |
{ |
|
732 |
uint t = tile + add; |
|
733 |
if (t < TILES_X * TILE_Y_MAX && GET_TILE_X(t) != TILE_X_MAX) |
|
734 |
return t; |
|
735 |
return TILE_WRAPPED; |
|
736 |
} |
|
737 |
||
738 |
bool IsValidTile(uint tile) |
|
739 |
{ |
|
740 |
return (tile < TILES_X * TILE_Y_MAX && GET_TILE_X(tile) != TILE_X_MAX); |
|
741 |
} |