-
Notifications
You must be signed in to change notification settings - Fork 55
/
WorldSegment.h
181 lines (169 loc) · 5.56 KB
/
WorldSegment.h
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
#pragma once
#include "Tile.h"
extern SegmentWrap map_segment;
enum draw_event_type{
TintedScaledBitmap,
CreatureText
};
struct draw_event{
draw_event_type type;
void * drawobject;
ALLEGRO_COLOR tint;
float sx;
float sy;
float sw;
float sh;
float dx;
float dy;
float dw;
float dh;
int flags;
};
class WorldSegment
{
private:
Tile* tiles;
vector<draw_event> todraw;
public:
bool loaded;
bool processed;
//these are the coordinates and size of the loaded segment
Crd3D pos;
Crd3D size;
//these are the coordinates at which the viewport is currently located
// note that this may not be the same as the actual coordinates of the segment
// due to concurrency
Crd3D displayed;
unsigned char rotation;
//these are the size and position of the DF map region to which this segment is a part
Crd3D regionSize;
Crd3D regionPos;
WorldSegment(int x=0, int y=0, int z=0, int sizex=0, int sizey=0, int sizez=0) {
this->pos.x = x;
this->pos.y = y;
this->pos.z = z - sizez + 1;
this->size.x = sizex;
this->size.y = sizey;
this->size.z = sizez;
this->displayed.x = ssState.DisplayedSegmentX;
this->displayed.y = ssState.DisplayedSegmentY;
this->displayed.z = ssState.DisplayedSegmentZ;
regionSize.x = regionSize.y = regionSize.z = 0;
regionPos.x = regionPos.y = regionPos.z = 0;
uint32_t memoryNeeded = sizex * sizey * sizez * sizeof(Tile);
tiles = (Tile*) malloc( memoryNeeded );
memset(tiles, 0, memoryNeeded);
}
~WorldSegment() {
uint32_t num = getNumTiles();
for(uint32_t i = 0; i < num; i++) {
if(tiles[i].IsValid()) {
tiles[i].~Tile();
}
}
free(tiles);
}
void Reset(int x=0, int y=0, int z=0, int sizex=0, int sizey=0, int sizez=0, bool hard=false) {
uint32_t num = getNumTiles();
for(uint32_t i = 0; i < num; i++) {
tiles[i].Invalidate();
}
uint32_t memoryNeeded = sizex * sizey * sizez * sizeof(Tile);
//if this is a hard reset, or if the size doesn't match what is needed, get a new segment
if(hard || sizex*sizey*sizez != size.x*size.y*size.z) {
free(tiles);
uint32_t memoryNeeded = sizex * sizey * sizez * sizeof(Tile);
tiles = (Tile*) malloc( memoryNeeded );
}
if(hard) {
memset(tiles, 0, memoryNeeded);
}
this->pos.x = x;
this->pos.y = y;
this->pos.z = z - sizez + 1;
this->size.x = sizex;
this->size.y = sizey;
this->size.z = sizez;
this->displayed.x = ssState.DisplayedSegmentX;
this->displayed.y = ssState.DisplayedSegmentY;
this->displayed.z = ssState.DisplayedSegmentZ;
regionSize.x = regionSize.y = regionSize.z = 0;
regionPos.x = regionPos.y = regionPos.z = 0;
}
uint32_t getNumTiles() {
return size.x * size.y * size.z;
}
Tile* ResetTile(int32_t x, int32_t y, int32_t z, df::tiletype type=tiletype::OpenSpace);
Tile* getTile(int32_t x, int32_t y, int32_t z);
Tile* getTileLocal(uint32_t x, uint32_t y, uint32_t z);
Tile* getTileRelativeTo(uint32_t x, uint32_t y, uint32_t z, dirRelative direction);
Tile* getTileRelativeTo(uint32_t x, uint32_t y, uint32_t z, dirRelative direction, int distance);
Tile* getTile(uint32_t index);
void CorrectTileForSegmentOffset(int32_t& x, int32_t& y, int32_t& z);
void CorrectTileForSegmentRotation(int32_t& x, int32_t& y, int32_t& z);
//void addTile(Tile* b);
void AssembleBlockTiles(int32_t firstX, int32_t firstY, int32_t lastX, int32_t lastY, int32_t incrx, int32_t incry, int32_t z, vector<vector<int16_t>>* allLayers);
void AssembleAllTiles();
void AssembleSprite(draw_event d);
void DrawAllTiles();
//void drawPixels();
bool CoordinateInsideSegment(uint32_t x, uint32_t y, uint32_t z);
bool CoordinateInteriorSegment(uint32_t x, uint32_t y, uint32_t z, uint32_t shellthick);
};
// FIXME: make nicer. one day. maybe.
class SegmentWrap
{
public:
SegmentWrap() {
drawsegment = new WorldSegment();
readsegment = new WorldSegment();
drawmutex = al_create_mutex();
readmutex = al_create_mutex();
}
~SegmentWrap() {
delete drawsegment;
delete readsegment;
al_destroy_mutex(drawmutex);
al_destroy_mutex(readmutex);
}
void shutdown(){
drawsegment->Reset();
readsegment->Reset();
}
void lock() {
al_lock_mutex(drawmutex);
al_lock_mutex(readmutex);
}
void unlock() {
al_unlock_mutex(drawmutex);
al_unlock_mutex(readmutex);
}
void lockDraw() {
al_lock_mutex(drawmutex);
}
void unlockDraw() {
al_unlock_mutex(drawmutex);
}
void lockRead() {
al_lock_mutex(readmutex);
}
void unlockRead() {
al_unlock_mutex(readmutex);
}
void swap() {
WorldSegment * temp = drawsegment;
drawsegment = readsegment;
readsegment = temp;
}
WorldSegment * getRead() {
return readsegment;
}
WorldSegment * getDraw() {
return drawsegment;
}
private:
ALLEGRO_MUTEX * drawmutex;
ALLEGRO_MUTEX * readmutex;
WorldSegment * drawsegment;
WorldSegment * readsegment;
};