-
Notifications
You must be signed in to change notification settings - Fork 2
/
Block.cpp
336 lines (273 loc) · 9.73 KB
/
Block.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
#include "Block.h"
#include "common.h"
#include "GUI.h"
#include "SpriteMaps.h"
#include "GameBuildings.h"
#include "Creatures.h"
#include "WorldSegment.h"
#include "BlockFactory.h"
#include "dfhack/library/DFTypes.h"
Block::Block(WorldSegment* ownerSegment)
{
//clear out own memory
memset(this, 0, sizeof(Block));
this->ownerSegment = ownerSegment;
building.info.type = BUILDINGTYPE_NA;
building.index = -1;
this->material.type = INVALID_INDEX;
this->material.index = INVALID_INDEX;
}
Block::~Block(void){
if( creature )
delete(creature);
}
void* Block::operator new (size_t size){
return blockFactory.allocateBlock( );
}
void Block::operator delete (void *p){
blockFactory.deleteBlock( (Block*)p );
}
inline BITMAP* imageSheet(t_SpriteWithOffset sprite, BITMAP* defaultBmp)
{
if (sprite.fileIndex == -1)
{
return defaultBmp;
}
else
{
return getImgFile(sprite.fileIndex);
}
}
void Block::Draw(BITMAP* target){
int sheetOffsetX, sheetOffsetY;
t_SpriteWithOffset sprite;
/*if(config.hide_outer_blocks){
if(x == ownerSegment->x || x == ownerSegment->x + ownerSegment->sizex - 1) return;
if(y == ownerSegment->y || y == ownerSegment->y + ownerSegment->sizey - 1) return;
}*/
int32_t drawx = x;
int32_t drawy = y;
int32_t drawz = z; //- ownerSegment->sizez + 1;
correctBlockForSegmetOffset( drawx, drawy, drawz);
correctBlockForRotation( drawx, drawy, drawz);
pointToScreen((int*)&drawx, (int*)&drawy, drawz);
drawx -= TILEWIDTH>>1;
int tileBorderColor = makecol(85,85,85);
//Draw Floor
if(floorType > 0 || wallType > 0 || ramp.type > 0 || stairType > 0){
//If tile has no floor, look for a Filler Floor from it's wall
if (floorType > 0)
{
sprite = GetFloorSpriteMap(floorType, this->material);
}
else if (wallType > 0)
{
sprite = GetFloorSpriteMap(wallType, this->material);
}
else if (ramp.type > 0)
{
sprite = GetFloorSpriteMap(ramp.type, this->material);
}
else if (stairType > 0)
{
sprite = GetFloorSpriteMap(stairType, this->material);
}
if(sprite.sheetIndex != INVALID_INDEX)
{
//if floor is muddy, override regular floor
if( occ.bits.mud && water.index == 0)
{
sprite.sheetIndex = SPRITEFLOOR_WATERFLOOR;
sprite.fileIndex = INVALID_INDEX;
}
//if floor is snowed down, override regular floor
if( occ.bits.snow )
{
sprite.sheetIndex = SPRITEFLOOR_SNOW;
sprite.fileIndex = INVALID_INDEX;
}
if (sprite.sheetIndex == UNCONFIGURED_INDEX)
{
sprite.sheetIndex = SPRITEOBJECT_FLOOR_NA;
sprite.fileIndex = INVALID_INDEX;
}
sheetOffsetX = TILEWIDTH * (sprite.sheetIndex % SHEET_OBJECTSWIDE);
sheetOffsetY = (TILEHEIGHT + FLOORHEIGHT) * (sprite.sheetIndex / SHEET_OBJECTSWIDE);
masked_blit(imageSheet(sprite,IMGObjectSheet), target, sheetOffsetX,sheetOffsetY, drawx,drawy, TILEWIDTH,TILEHEIGHT + FLOORHEIGHT);
}
//Northern frame
if(this->depthBorderNorth)
line(target, drawx + (TILEWIDTH>>1), drawy, drawx+TILEWIDTH-1, drawy+(TILEHEIGHT>>1)-1, tileBorderColor);
//Western frame
if(this->depthBorderWest)
line(target, drawx, drawy+(TILEHEIGHT>>1)-1, drawx+(TILEWIDTH>>1)-1, drawy, tileBorderColor);
}
//Draw Ramp
if(ramp.type > 0){
sprite = GetBlockSpriteMap(ramp.type, material);
if (sprite.sheetIndex == UNCONFIGURED_INDEX)
{
sprite.sheetIndex = 0;
sprite.fileIndex = INVALID_INDEX;
}
if (sprite.sheetIndex != INVALID_INDEX)
{
sheetOffsetX = SPRITEWIDTH * ramp.index;
sheetOffsetY = ((TILEHEIGHT + FLOORHEIGHT + SPRITEHEIGHT) * sprite.sheetIndex)+(TILEHEIGHT + FLOORHEIGHT);
masked_blit(imageSheet(sprite,IMGRampSheet), target, sheetOffsetX,sheetOffsetY, drawx,drawy - (WALLHEIGHT), SPRITEWIDTH, SPRITEHEIGHT);
}}
//vegetation
if(tree.index > 0 || tree.type > 0){
sprite = GetSpriteVegetation( (TileClass) getVegetationType( this->floorType ), tree.index );
DrawSpriteFromSheet( sprite.sheetIndex, target, imageSheet(sprite,IMGObjectSheet), drawx, drawy );
}
//shadow
if (shadow > 0)
{
DrawSpriteFromSheet( BASE_SHADOW_TILE + shadow - 1, target, IMGObjectSheet, drawx, (ramp.type > 0)?(drawy - (WALLHEIGHT/2)):drawy );
}
//Building
bool skipBuilding =
(building.info.type == BUILDINGTYPE_STOCKPILE && !config.show_stockpiles) ||
(building.info.type == BUILDINGTYPE_ZONE && !config.show_zones);
if(building.info.type != BUILDINGTYPE_NA && !skipBuilding){
int spriteNum = SPRITEOBJECT_NA; //getBuildingSprite(this->building, mirroredBuilding);
for(uint32_t i=0; i < building.sprites.size(); i++){
sprite = building.sprites[i];
if (!(sprite.animFrames & (1 << currentAnimationFrame)))
continue;
DrawSpriteFromSheet(sprite.sheetIndex , target, imageSheet(sprite,IMGObjectSheet),
drawx + building.sprites[i].x,
drawy + building.sprites[i].y);
}
}
//Draw Stairs
if(stairType > 0){
//down part
//skipping at the moment?
//int spriteNum = GetFloorSpriteMap(stairType,material);
//DrawSpriteFromSheet( spriteNum, target, IMGObjectSheet, drawx, drawy );
//up part
bool mirrored = false;
if(findWallCloseTo(ownerSegment, this) == eSimpleW)
mirrored = true;
sprite = GetBlockSpriteMap(stairType, material);
if(sprite.sheetIndex != INVALID_INDEX && sprite.sheetIndex != UNCONFIGURED_INDEX)
{
if (mirrored)
sprite.sheetIndex += 1;
DrawSpriteFromSheet( sprite.sheetIndex, target, imageSheet(sprite,IMGObjectSheet), drawx, drawy );
}
}
//Draw Walls
if(wallType > 0){
//draw wall
sprite = GetBlockSpriteMap(wallType, material);
if (sprite.sheetIndex == UNCONFIGURED_INDEX)
{
sprite.sheetIndex = SPRITEOBJECT_WALL_NA;
sprite.fileIndex = INVALID_INDEX;
}
if (sprite.sheetIndex == INVALID_INDEX)
{
//skip
}
else if( config.truncate_walls && this->z == ownerSegment->z + ownerSegment->sizez -2){
int sheetx = sprite.sheetIndex % SHEET_OBJECTSWIDE;
int sheety = sprite.sheetIndex / SHEET_OBJECTSWIDE;
//draw a tiny bit of wall
masked_blit(imageSheet(sprite,IMGObjectSheet), target,
sheetx * SPRITEWIDTH, sheety * SPRITEHEIGHT+WALL_CUTOFF_HEIGHT,
drawx, drawy - (WALLHEIGHT)+WALL_CUTOFF_HEIGHT, SPRITEWIDTH, SPRITEHEIGHT-WALL_CUTOFF_HEIGHT);
//draw cut-off floor thing
masked_blit(IMGObjectSheet, target,
TILEWIDTH * SPRITEFLOOR_CUTOFF, 0,
drawx, drawy-(SPRITEHEIGHT-WALL_CUTOFF_HEIGHT)/2, SPRITEWIDTH, SPRITEWIDTH);
}
else {
DrawSpriteFromSheet(sprite.sheetIndex, target, imageSheet(sprite,IMGObjectSheet), drawx, drawy );
drawy -= (WALLHEIGHT);
//Northern border
if(this->depthBorderNorth)
line(target, drawx + (TILEWIDTH>>1), drawy, drawx+TILEWIDTH-1, drawy+(TILEHEIGHT>>1)-1, tileBorderColor);
//Western border
if(this->depthBorderWest)
line(target, drawx, drawy+(TILEHEIGHT>>1)-1, drawx+(TILEWIDTH>>1)-1, drawy, tileBorderColor);
drawy += (WALLHEIGHT);
}
}
//water
if(water.index > 0){
int spriteNum = 0;
int waterlevel = water.index;
//if(waterlevel == 7) waterlevel--;
if(water.type == 0)
spriteNum = SPRITEOBJECT_WATERLEVEL1 + waterlevel - 1;
if(water.type == 1)
spriteNum = SPRITEOBJECT_WATERLEVEL1_LAVA + waterlevel - 1;
DrawSpriteFromSheet( spriteNum, target, IMGObjectSheet, drawx, drawy );
}
// creature
// ensure there is *some* creature according to the map data
// (no guarantee it is the right one)
if(creature != null && (occ.bits.unit || occ.bits.unit_grounded)){
DrawCreature( target, drawx, drawy, creature);
}
}
void Block::DrawRamptops(BITMAP* target){
if (ramp.type > 0)
{
int sheetOffsetX, sheetOffsetY;
/*if(config.hide_outer_blocks){
if(x == ownerSegment->x || x == ownerSegment->x + ownerSegment->sizex - 1) return;
if(y == ownerSegment->y || y == ownerSegment->y + ownerSegment->sizey - 1) return;
}*/
int32_t drawx = x;
int32_t drawy = y;
int32_t drawz = z+1; //- ownerSegment->sizez + 1;
correctBlockForSegmetOffset( drawx, drawy, drawz);
correctBlockForRotation( drawx, drawy, drawz);
pointToScreen((int*)&drawx, (int*)&drawy, drawz);
drawx -= TILEWIDTH>>1;
t_SpriteWithOffset sprite = GetBlockSpriteMap(ramp.type,material);
if (sprite.sheetIndex == UNCONFIGURED_INDEX)
{
sprite.sheetIndex = 0;
sprite.fileIndex = INVALID_INDEX;
}
if (sprite.sheetIndex != INVALID_INDEX)
{
sheetOffsetX = SPRITEWIDTH * ramp.index;
sheetOffsetY = (TILEHEIGHT + FLOORHEIGHT + SPRITEHEIGHT) * sprite.sheetIndex;
masked_blit( imageSheet(sprite,IMGRampSheet), target, sheetOffsetX,sheetOffsetY, drawx,drawy, SPRITEWIDTH, TILEHEIGHT + FLOORHEIGHT);
}}
}
bool hasWall(Block* b){
if(!b) return false;
return b->wallType > 0;
}
bool hasBuildingOfID(Block* b, int ID){
if(!b) return false;
return b->building.info.type == ID;
}
bool hasBuildingIdentity(Block* b, uint32_t index, int buildingOcc){
if(!b) return false;
if (!(b->building.index == index)) return false;
return b->occ.bits.building == buildingOcc;
}
bool hasBuildingOfIndex(Block* b, uint32_t index){
if(!b) return false;
return b->building.index == index;
}
bool wallShouldNotHaveBorders( int in ){
switch( in ){
case 65: //stone fortification
case 436: //minstone fortification
case 326: //lavastone fortification
case 327: //featstone fortification
case 494: //constructed fortification
return true;
break;
};
return false;
}