1 /**
2 Copyright: Copyright (c) 2016-2018 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 module voxelman.world.storage.dimensionman;
7 
8 import std.typecons : Nullable;
9 import voxelman.log;
10 import voxelman.math;
11 import voxelman.core.config;
12 import voxelman.world.storage;
13 import voxelman.world.gen.generatorman;
14 
15 struct DimensionInfo
16 {
17 	string name;
18 	ClientDimPos spawnPos;
19 
20 	Box borders = Box(ivec3(-int.max/2, -int.max/2, -int.max/2), ivec3(int.max, int.max, int.max));
21 }
22 
23 struct DimensionManager {
24 	DimensionInfo[DimensionId] dimensions;
25 	GeneratorManager generatorMan;
26 	auto dbKey = IoKey("voxelman.world.storage.dimensionman");
27 
28 	void load(ref PluginDataLoader loader) {
29 		generatorMan.load(loader);
30 		loader.readEntryDecoded(dbKey, dimensions);
31 	}
32 
33 	void save(ref PluginDataSaver saver) {
34 		generatorMan.save(saver);
35 		saver.writeEntryEncoded(dbKey, dimensions);
36 	}
37 
38 	bool contains(DimensionId dim) {
39 		return !!(dim in dimensions);
40 	}
41 
42 	/// returns internal pointer to hashmap. add/remove can invalidate pointers.
43 	DimensionInfo* opIndex(DimensionId dim) {
44 		return dim in dimensions;
45 	}
46 
47 	DimensionInfo* getOrCreate(DimensionId dim) {
48 		auto dimension = dim in dimensions;
49 		if (dimension)
50 			return dimension;
51 
52 		dimensions[dim] = DimensionInfo();
53 		return dim in dimensions;
54 	}
55 
56 	void opIndexAssign(DimensionInfo value, DimensionId key) {
57 		assert(key !in dimensions);
58 		dimensions[key] = value;
59 	}
60 
61 	void remove(DimensionId dim) {
62 		dimensions.remove(dim);
63 	}
64 
65 	Box dimensionBorders(DimensionId dim) {
66 		auto dimension = dim in dimensions;
67 		if (dimension)
68 			return dimension.borders;
69 		return DimensionInfo.init.borders;
70 	}
71 }