1 // Types compatible with NWN
2 module nwn.types;
3 
4 import std.stdint;
5 import std.string;
6 
7 /// int
8 alias NWInt = int32_t;
9 
10 /// float
11 alias NWFloat = float;
12 
13 /// string
14 alias NWString = string;
15 
16 /// object
17 alias NWObject = uint32_t;
18 
19 
20 /// vector
21 struct NWVector{
22 	NWFloat[3] value = [0.0, 0.0, 0.0];
23 
24 	alias value this;
25 
26 	/// Get/set vector values
27 	@property{
28 		NWFloat x() const { return value[0]; }
29 		void x(NWFloat rhs) { value[0] = rhs; }
30 		NWFloat y() const { return value[1]; }
31 		void y(NWFloat rhs) { value[1] = rhs; }
32 		NWFloat z() const { return value[2]; }
33 		void z(NWFloat rhs) { value[2] = rhs; }
34 	}
35 
36 	string toString() const {
37 		import std.format: format;
38 		return format("[%f, %f, %f]", value[0], value[1], value[2]);
39 	}
40 	enum NWVector init = NWVector([0.0, 0.0, 0.0]);
41 }
42 /// location
43 ///
44 /// Warning: The area is stored as an onject ID and can change between module runs.
45 struct NWLocation{
46 	NWObject area;
47 	NWVector position;
48 	NWFloat facing;
49 
50 	string toString() const {
51 		import std.format: format;
52 		return format("%#x %s %f", area, position.toString(), facing);
53 	}
54 	enum NWLocation init = NWLocation(NWInitValue!NWObject, NWInitValue!NWVector, NWInitValue!NWFloat);
55 }
56 
57 
58 template NWInitValue(T){
59 	static if(is(T == NWInt))           enum NWInitValue = cast(NWInt)0;
60 	else static if(is(T == NWFloat))    enum NWInitValue = 0.0f;
61 	else static if(is(T == NWString))   enum NWInitValue = "";
62 	else static if(is(T == NWObject))   enum NWInitValue = NWObject.max;
63 	else static if(is(T == NWVector))   enum NWInitValue = NWVector.init;
64 	else static if(is(T == NWLocation)) enum NWInitValue = NWLocation.init;
65 	else static if(is(T == NWItemproperty)) enum NWInitValue = NWItemproperty();
66 	else static assert(0, "Unknown type");
67 }
68 
69 /// itemproperty
70 struct NWItemproperty {
71 	uint16_t type = uint16_t.max;
72 	uint16_t subType = 0;
73 	uint16_t costValue = 0;
74 	uint8_t p1 = 0;
75 
76 	string toString() const{
77 		return format!"%d.%d(%d, %d)"(type, subType, costValue, p1);
78 	}
79 }