forked from DFHack/stonesense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorldSegment.cpp
466 lines (404 loc) · 10.6 KB
/
WorldSegment.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include "common.h"
#include "WorldSegment.h"
#include "GUI.h"
#include "ContentLoader.h"
#include "Creatures.h"
using namespace std;
using namespace DFHack;
using namespace df::enums;
const GameState SegmentWrap::zeroState =
{
{0,0,0},0,{0,0,0},{0,0,0},{0,0,0},0,0
};
ALLEGRO_BITMAP * fog = 0;
void WorldSegment::CorrectTileForSegmentOffset(int32_t& xin, int32_t& yin, int32_t& zin)
{
xin -= segState.Position.x;
yin -= segState.Position.y; //Position.y;
zin -= segState.Position.z - 1; //need to remove the offset
}
void WorldSegment::CorrectTileForSegmentRotation(int32_t& x, int32_t& y, int32_t& z)
{
int32_t oldx = x;
int32_t oldy = y;
if(segState.Rotation == 1) {
x = segState.Size.x - oldy -1;
y = oldx;
}
if(segState.Rotation == 2) {
x = segState.Size.x - oldx -1;
y = segState.Size.y - oldy -1;
}
if(segState.Rotation == 3) {
x = oldy;
y = segState.Size.y - oldx -1;
}
}
//Converts a set of world coordinates into local coordinates, taking into account view rotation.
bool WorldSegment::ConvertToSegmentLocal(int32_t & x, int32_t & y, int32_t & z)
{
int32_t lx = x;
int32_t ly = y;
int32_t lz = z;
lx -= segState.Position.x;
ly -= segState.Position.y;
lz -= segState.Position.z;
CorrectTileForSegmentRotation(lx, ly, lz);
//even if we return false, we still might want to use the new coords, maybe.
x = lx;
y = ly;
z = lz;
if(lx < 0 || lx >= segState.Size.x) {
return false;
}
if(ly < 0 || ly >= segState.Size.y) {
return false;
}
if(lz < 0 || lz >= segState.Size.z) {
return false;
}
return true;
}
uint32_t WorldSegment::ConvertLocalToIndex(int32_t x, int32_t y, int32_t z)
{
return (uint32_t) (x + (y + (z*segState.Size.y))*segState.Size.x );
}
//Returns a blank tile at the specified world coordinates.
Tile* WorldSegment::ResetTile(int32_t x, int32_t y, int32_t z, df::tiletype type)
{
int32_t lx = x;
int32_t ly = y;
int32_t lz = z;
if(!ConvertToSegmentLocal(lx, ly, lz)){
return NULL;
}
uint32_t index = ConvertLocalToIndex(lx, ly, lz);
Tile* tptr = &(tiles[index]);
Tile::InvalidateAndDestroy(tptr);
Tile::CleanCreateAndValidate(tptr,this,type);
tptr->x = x;
tptr->y = y;
tptr->z = z;
return tptr;
}
//Returns an existing tile at the specified world coordinates.
Tile* WorldSegment::getTile(int32_t x, int32_t y, int32_t z)
{
int32_t lx = x;
int32_t ly = y;
int32_t lz = z;
if(!ConvertToSegmentLocal(lx, ly, lz)){
return NULL;
}
uint32_t index = ConvertLocalToIndex(lx, ly, lz);
return getTile(index);
}
Tile* WorldSegment::getTileRelativeTo(int32_t x, int32_t y, int32_t z, dirRelative direction)
{
int32_t lx = x;
int32_t ly = y;
int32_t lz = z;
if(!ConvertToSegmentLocal(lx, ly, lz)){
return NULL;
}
switch (direction) {
case eUp:
ly--;
break;
case eDown:
ly++;
break;
case eLeft:
lx--;
break;
case eRight:
lx++;
break;
case eAbove:
lz++;
break;
case eBelow:
lz--;
break;
case eUpLeft:
ly--;
lx--;
break;
case eUpRight:
ly--;
lx++;
break;
case eDownLeft:
ly++;
lx--;
break;
case eDownRight:
ly++;
lx++;
break;
}
return getTileLocal(lx, ly, lz);
}
Tile* WorldSegment::getTileRelativeTo(int32_t x, int32_t y, int32_t z, dirRelative direction, int distance)
{
int32_t lx = x;
int32_t ly = y;
int32_t lz = z;
if(!ConvertToSegmentLocal(lx, ly, lz)){
return NULL;
}
switch (direction) {
case eUp:
ly-= distance;
break;
case eDown:
ly+= distance;
break;
case eLeft:
lx-= distance;
break;
case eRight:
lx+= distance;
break;
case eAbove:
lz+= distance;
break;
case eBelow:
lz-= distance;
break;
case eUpLeft:
ly-= distance;
lx-= distance;
break;
case eUpRight:
ly-= distance;
lx+= distance;
break;
case eDownLeft:
ly+= distance;
lx-= distance;
break;
case eDownRight:
ly+= distance;
lx+= distance;
break;
}
return getTileLocal(lx, ly, lz);
}
Tile* WorldSegment::getTileLocal(int32_t x, int32_t y, int32_t z)
{
if(x < 0 || x >= segState.Size.x) {
return 0;
}
if(y < 0 || y >= segState.Size.y) {
return 0;
}
if(z < 0 || z >= segState.Size.z) {
return 0;
}
uint32_t index = ConvertLocalToIndex(x, y, z);
return getTile(index);
}
Tile* WorldSegment::getTile(uint32_t index)
{
if(index < 0 || index>=getNumTiles() ) {
return NULL;
}
return tiles[index].IsValid() ? &(tiles[index]) : NULL;
}
void WorldSegment::DrawAllTiles()
{
if(!loaded) {
return;
}
if(ssConfig.fogenable) {
ALLEGRO_BITMAP* temp = al_get_target_bitmap();
if(!fog) {
fog = al_create_bitmap(ssState.ScreenW, ssState.ScreenH);
al_set_target_bitmap(fog);
al_clear_to_color(premultiply(ssConfig.fogcol));
al_set_target_bitmap(temp);
}
if(!((al_get_bitmap_width(fog) == ssState.ScreenW) && (al_get_bitmap_height(fog) == ssState.ScreenH))) {
al_destroy_bitmap(fog);
fog = al_create_bitmap(ssState.ScreenW, ssState.ScreenH);
al_set_target_bitmap(fog);
al_clear_to_color(premultiply(ssConfig.fogcol));
al_set_target_bitmap(temp);
}
}
if (ssConfig.show_osd) {
DrawCurrentLevelOutline(true);
}
if(todraw.size()>0) {
al_hold_bitmap_drawing(true);
for(size_t i=0; i<todraw.size(); i++) {
if(i%ssConfig.bitmapHolds==0) {
al_hold_bitmap_drawing(false);
al_hold_bitmap_drawing(true);
}
switch(todraw[i].type) {
case TintedScaledBitmap:
al_draw_tinted_scaled_bitmap(
(ALLEGRO_BITMAP*) todraw[i].drawobject,
todraw[i].tint,
todraw[i].sx,
todraw[i].sy,
todraw[i].sw,
todraw[i].sh,
todraw[i].dx,
todraw[i].dy,
todraw[i].dw,
todraw[i].dh,
todraw[i].flags );
break;
case CreatureText:
DrawCreatureText(
todraw[i].dx,
todraw[i].dy,
(SS_Unit*) todraw[i].drawobject );
break;
}
}
}
if (ssConfig.show_osd) {
DrawCurrentLevelOutline(false);
}
al_hold_bitmap_drawing(false);
al_hold_bitmap_drawing(true);
}
/**
* Assembles sprites for all tiles in the segment.
* The draw order used draws tiles on a per-block basis, so blocks
* in the back are drawn before blocks in the front.
*/
void WorldSegment::AssembleAllTiles()
{
if(!loaded) {
return;
}
clock_t starttime = clock();
// x,y,z print prices
int32_t vsxmax = segState.Size.x-1;
int32_t vsymax = segState.Size.y-1;
int32_t vszmax = segState.Size.z-1; // grabbing one tile +z more than we should for tile rules
for(int32_t vsz=0; vsz < vszmax; vsz++) {
//add the fog to the queue
if(ssConfig.fogenable && fog) {
draw_event d = {
TintedScaledBitmap,
fog,
al_map_rgb(255,255,255),
0,
0,
(float)ssState.ScreenW,
(float)ssState.ScreenH,
0,
0,
(float)ssState.ScreenW,
(float)ssState.ScreenH,
0};
AssembleSprite(d);
}
//add the tiles to the queue
for(int32_t vsx=1; vsx < vsxmax; vsx++) {
for(int32_t vsy=1; vsy < vsymax; vsy++) {
Tile *b = getTileLocal(vsx,vsy,vsz);
if (b) {
b->AssembleTile();
}
}
}
}
ssTimers.assembly_time = (clock() - starttime)*0.1 + ssTimers.assembly_time*0.9;
}
bool WorldSegment::CoordinateInsideSegment(int32_t x, int32_t y, int32_t z)
{
int32_t lx = x;
int32_t ly = y;
int32_t lz = z;
if(!ConvertToSegmentLocal(lx, ly, lz)){
return NULL;
}
if(lx < 0 || lx >= segState.Size.x) {
return 0;
}
if(ly < 0 || ly >= segState.Size.y) {
return 0;
}
if(lz < 0 || lz >= segState.Size.z) {
return 0;
}
return true;
}
bool WorldSegment::RangeInsideSegment(int32_t min_x, int32_t min_y, int32_t min_z, int32_t max_x, int32_t max_y, int32_t max_z)
{
int32_t lnx = min_x;
int32_t lny = min_y;
int32_t lnz = min_z;
int32_t lxx = max_x;
int32_t lxy = max_y;
int32_t lxz = max_z;
ConvertToSegmentLocal(lnx, lny, lnz);
ConvertToSegmentLocal(lxx, lxy, lxz);
if (lxx < 0 || lnx >= segState.Size.x) {
return 0;
}
if (lxy < 0 || lny >= segState.Size.y) {
return 0;
}
if (lxz < 0 || lnz >= segState.Size.z) {
return 0;
}
return true;
}
bool WorldSegment::CoordinateInteriorSegment(int32_t x, int32_t y, int32_t z, uint32_t shellthick)
{
int32_t lx = x;
int32_t ly = y;
int32_t lz = z;
if(!ConvertToSegmentLocal(lx, ly, lz)){
return NULL;
}
if(lx < 0 + int32_t(shellthick) || lx >= int32_t(segState.Size.x - shellthick)) {
return 0;
}
if(ly < 0 + int32_t(shellthick) || ly >= int32_t(segState.Size.y - shellthick)) {
return 0;
}
if(lz < 0 /*bottom is "interior"*/ || lz >= int32_t(segState.Size.z - shellthick)) {
return 0;
}
return true;
}
void WorldSegment::AssembleSprite(draw_event d)
{
todraw.push_back(d);
}
void WorldSegment::PushBuilding( Buildings::t_building * building)
{
buildings.push_back(building);
}
void WorldSegment::ClearBuildings()
{
for(size_t i=0; i<buildings.size(); i++){
delete(buildings[i]);
buildings[i] = NULL;
}
buildings.clear();
}
void WorldSegment::PushUnit( SS_Unit * unit)
{
units.push_back(unit);
}
void WorldSegment::ClearUnits()
{
for(size_t i=0; i<units.size(); i++){
if(units[i]){
delete(units[i]->inv);
delete(units[i]);
units[i] = NULL;
}
}
units.clear();
}