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
|
#pragma once
#include "StarGameTypes.hpp"
#include "StarXXHash.hpp"
#include "StarLiquidTypes.hpp"
#include "StarTileDamage.hpp"
#include "StarTileSectorArray.hpp"
#include "StarCollisionGenerator.hpp"
#include "StarWorldLayout.hpp"
#include "StarVersion.hpp"
namespace Star {
struct WorldTile {
WorldTile();
// Copy constructor and operator= do not preserve collision cache.
WorldTile(WorldTile const& worldTile);
WorldTile& operator=(WorldTile const& worldTile);
MaterialId material(TileLayer layer) const;
ModId mod(TileLayer layer) const;
MaterialColorVariant materialColor(TileLayer layer) const;
CollisionKind getCollision() const;
tuple<MaterialId, MaterialHue, MaterialColorVariant> materialAndColor(TileLayer layer) const;
bool isConnectable(TileLayer layer, bool materialOnly) const;
bool isColliding(CollisionSet const& collisionSet) const;
MaterialId foreground;
MaterialHue foregroundHueShift;
ModId foregroundMod;
MaterialHue foregroundModHueShift;
MaterialColorVariant foregroundColorVariant;
MaterialId background;
MaterialHue backgroundHueShift;
ModId backgroundMod;
MaterialHue backgroundModHueShift;
MaterialColorVariant backgroundColorVariant;
CollisionKind collision;
bool collisionCacheDirty;
StaticList<CollisionBlock, CollisionGenerator::MaximumCollisionsPerSpace> collisionCache;
BiomeIndex blockBiomeIndex;
BiomeIndex environmentBiomeIndex;
bool biomeTransition;
TileDamageStatus foregroundDamage;
TileDamageStatus backgroundDamage;
// If block is part of a dungeon then that affects spawns/drops,
// as well as governing block protection
DungeonId dungeonId;
};
struct ServerTile : public WorldTile {
static VersionNumber const CurrentSerializationVersion;
ServerTile();
ServerTile(ServerTile const& serverTile);
ServerTile& operator=(ServerTile const& serverTile);
bool isColliding(CollisionSet const& collisionSet) const;
void write(DataStream& ds) const;
void read(DataStream& ds, VersionNumber serializationVersion);
// Updates collision, clears cache, and if the collision kind does not
// support liquid destroys it.
bool updateCollision(CollisionKind kind);
// Used for setting the second collision kind calculated by object material spaces.
bool updateObjectCollision(CollisionKind kind);
// Calculates the actually-used collision kind based on the tile and object collision kinds.
CollisionKind getCollision() const;
LiquidStore liquid;
// If set, a plant or object is rooted to the tile and tile damage
// should be redirected to this position
Maybe<Vec2I> rootSource;
// Do not serialize - calculated at runtime
CollisionKind objectCollision;
};
typedef TileSectorArray<ServerTile, WorldSectorSize> ServerTileSectorArray;
typedef shared_ptr<ServerTileSectorArray> ServerTileSectorArrayPtr;
struct ClientTile : public WorldTile {
ClientTile();
ClientTile(ClientTile const& clientTile);
ClientTile& operator=(ClientTile const& clientTile);
bool backgroundLightTransparent;
bool foregroundLightTransparent;
LiquidLevel liquid;
float gravity;
};
typedef TileSectorArray<ClientTile, WorldSectorSize> ClientTileSectorArray;
typedef shared_ptr<ClientTileSectorArray> ClientTileSectorArrayPtr;
// Tile structure to transfer all data from client to server
struct NetTile {
NetTile();
MaterialId background;
MaterialHue backgroundHueShift;
MaterialColorVariant backgroundColorVariant;
ModId backgroundMod;
MaterialHue backgroundModHueShift;
MaterialId foreground;
MaterialHue foregroundHueShift;
MaterialColorVariant foregroundColorVariant;
ModId foregroundMod;
MaterialHue foregroundModHueShift;
CollisionKind collision;
BiomeIndex blockBiomeIndex;
BiomeIndex environmentBiomeIndex;
LiquidNetUpdate liquid;
DungeonId dungeonId;
};
DataStream& operator>>(DataStream& ds, NetTile& tile);
DataStream& operator<<(DataStream& ds, NetTile const& tile);
// For storing predicted tile state.
struct PredictedTile {
int64_t time;
Maybe<MaterialId> background;
Maybe<MaterialHue> backgroundHueShift;
Maybe<MaterialColorVariant> backgroundColorVariant;
Maybe<ModId> backgroundMod;
Maybe<MaterialHue> backgroundModHueShift;
Maybe<MaterialId> foreground;
Maybe<MaterialHue> foregroundHueShift;
Maybe<MaterialColorVariant> foregroundColorVariant;
Maybe<ModId> foregroundMod;
Maybe<MaterialHue> foregroundModHueShift;
Maybe<LiquidLevel> liquid;
Maybe<CollisionKind> collision;
operator bool() const;
template <typename Tile>
void apply(Tile& tile) {
if (foreground) tile.foreground = *foreground;
if (foregroundMod) tile.foregroundMod = *foregroundMod;
if (foregroundHueShift) tile.foregroundHueShift = *foregroundHueShift;
if (foregroundModHueShift) tile.foregroundModHueShift = *foregroundModHueShift;
if (background) tile.background = *background;
if (backgroundMod) tile.backgroundMod = *backgroundMod;
if (backgroundHueShift) tile.backgroundHueShift = *backgroundHueShift;
if (backgroundModHueShift) tile.backgroundModHueShift = *backgroundModHueShift;
}
};
// Just the parts of a tile that are used to render. The members here are laid
// out specifically to avoid padding bytes so that a fast path can be taken
// when hashing for chunk render caching.
struct RenderTile {
MaterialId foreground;
ModId foregroundMod;
MaterialId background;
ModId backgroundMod;
MaterialHue foregroundHueShift;
MaterialHue foregroundModHueShift;
MaterialColorVariant foregroundColorVariant;
TileDamageType foregroundDamageType;
uint8_t foregroundDamageLevel;
MaterialHue backgroundHueShift;
MaterialHue backgroundModHueShift;
MaterialColorVariant backgroundColorVariant;
TileDamageType backgroundDamageType;
uint8_t backgroundDamageLevel;
LiquidId liquidId;
uint8_t liquidLevel;
template <typename Hasher>
void hashPushTerrain(Hasher& hasher) const;
template <typename Hasher>
void hashPushLiquid(Hasher& hasher) const;
};
DataStream& operator>>(DataStream& ds, RenderTile& tile);
DataStream& operator<<(DataStream& ds, RenderTile const& tile);
typedef MultiArray<RenderTile, 2> RenderTileArray;
inline WorldTile::WorldTile()
: foreground(NullMaterialId),
foregroundHueShift(),
foregroundMod(NoModId),
foregroundModHueShift(),
foregroundColorVariant(DefaultMaterialColorVariant),
background(NullMaterialId),
backgroundHueShift(),
backgroundMod(NoModId),
backgroundModHueShift(),
backgroundColorVariant(DefaultMaterialColorVariant),
collision(CollisionKind::Null),
collisionCacheDirty(true),
blockBiomeIndex(),
environmentBiomeIndex(),
dungeonId(NoDungeonId) {}
inline WorldTile::WorldTile(WorldTile const& worldTile) {
*this = worldTile;
}
inline WorldTile& WorldTile::operator=(WorldTile const& worldTile) {
foreground = worldTile.foreground;
foregroundHueShift = worldTile.foregroundHueShift;
foregroundMod = worldTile.foregroundMod;
foregroundModHueShift = worldTile.foregroundModHueShift;
foregroundColorVariant = worldTile.foregroundColorVariant;
background = worldTile.background;
backgroundHueShift = worldTile.backgroundHueShift;
backgroundMod = worldTile.backgroundMod;
backgroundModHueShift = worldTile.backgroundModHueShift;
backgroundColorVariant = worldTile.backgroundColorVariant;
// Don't bother copying collision cache
collisionCacheDirty = true;
collision = worldTile.collision;
blockBiomeIndex = worldTile.blockBiomeIndex;
environmentBiomeIndex = worldTile.environmentBiomeIndex;
foregroundDamage = worldTile.foregroundDamage;
backgroundDamage = worldTile.backgroundDamage;
dungeonId = worldTile.dungeonId;
return *this;
}
inline MaterialId WorldTile::material(TileLayer layer) const {
if (layer == TileLayer::Foreground)
return foreground;
else
return background;
}
inline ModId WorldTile::mod(TileLayer layer) const {
if (layer == TileLayer::Foreground)
return foregroundMod;
else
return backgroundMod;
}
inline MaterialColorVariant WorldTile::materialColor(TileLayer layer) const {
if (layer == TileLayer::Foreground)
return foregroundColorVariant;
else
return backgroundColorVariant;
}
inline CollisionKind WorldTile::getCollision() const {
return collision;
}
inline tuple<MaterialId, MaterialHue, MaterialColorVariant> WorldTile::materialAndColor(TileLayer layer) const {
if (layer == TileLayer::Foreground)
return std::tuple<MaterialId, MaterialHue, MaterialColorVariant>{
foreground, foregroundHueShift, foregroundColorVariant};
else
return std::tuple<MaterialId, MaterialHue, MaterialColorVariant>{
background, backgroundHueShift, backgroundColorVariant};
}
inline ClientTile::ClientTile() : backgroundLightTransparent(true), foregroundLightTransparent(true), gravity() {}
inline ClientTile::ClientTile(ClientTile const& clientTile) : WorldTile() {
*this = clientTile;
}
inline ClientTile& ClientTile::operator=(ClientTile const& clientTile) {
WorldTile::operator=(clientTile);
backgroundLightTransparent = clientTile.backgroundLightTransparent;
foregroundLightTransparent = clientTile.foregroundLightTransparent;
liquid = clientTile.liquid;
gravity = clientTile.gravity;
return *this;
}
inline NetTile::NetTile()
: background(NullMaterialId),
backgroundHueShift(),
backgroundColorVariant(),
backgroundMod(NoModId),
backgroundModHueShift(),
foreground(NullMaterialId),
foregroundHueShift(),
foregroundColorVariant(),
foregroundMod(NoModId),
foregroundModHueShift(),
collision(),
blockBiomeIndex(),
environmentBiomeIndex(),
dungeonId(NoDungeonId) {}
template <typename Hasher>
inline void RenderTile::hashPushTerrain(Hasher& hasher) const {
// Do the fast path hash if the last (terrain relevant) field is at byte 20, because that means
// there are no padding bytes between any field and we can simply pass the
// entire tile as one block of memory.
static size_t const TerrainEndOffset = offsetof(RenderTile, liquidId);
static size_t const TotalTerrainSize =
sizeof(MaterialId) * 2 + sizeof(ModId) * 2 + sizeof(MaterialHue) * 4 + sizeof(MaterialColorVariant) * 2 * sizeof(TileDamageType) * 2 + 2;
#ifdef STAR_DEBUG
static bool const FastHash = false;
#else
static bool const FastHash = TerrainEndOffset == TotalTerrainSize;
#endif
if (FastHash) {
hasher.push((char const*)this, TerrainEndOffset);
} else {
char buffer[TotalTerrainSize];
size_t bufferSize = 0;
auto hashTilePart = [&](void const* data, size_t size) {
memcpy(buffer + bufferSize, data, size);
bufferSize += size;
};
hashTilePart(&foreground, sizeof(foreground));
hashTilePart(&foregroundMod, sizeof(foregroundMod));
hashTilePart(&background, sizeof(background));
hashTilePart(&backgroundMod, sizeof(backgroundMod));
hashTilePart(&foregroundHueShift, sizeof(foregroundHueShift));
hashTilePart(&foregroundModHueShift, sizeof(foregroundModHueShift));
hashTilePart(&foregroundColorVariant, sizeof(foregroundColorVariant));
hashTilePart(&foregroundDamageType, sizeof(foregroundDamageType));
hashTilePart(&foregroundDamageLevel, sizeof(foregroundDamageLevel));
hashTilePart(&backgroundHueShift, sizeof(backgroundHueShift));
hashTilePart(&backgroundModHueShift, sizeof(backgroundModHueShift));
hashTilePart(&backgroundColorVariant, sizeof(backgroundColorVariant));
hashTilePart(&backgroundDamageType, sizeof(backgroundDamageType));
hashTilePart(&backgroundDamageLevel, sizeof(backgroundDamageLevel));
hasher.push(buffer, TotalTerrainSize);
}
}
template <typename Hasher>
inline void RenderTile::hashPushLiquid(Hasher& hasher) const {
char buffer[sizeof(liquidLevel) + sizeof(liquidId)];
memcpy(buffer, &liquidLevel, sizeof(liquidLevel));
memcpy(buffer + sizeof(liquidLevel), &liquidId, sizeof(liquidId));
hasher.push(buffer, sizeof(liquidLevel) + sizeof(liquidId));
}
}
|