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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
use super::*;
#[mt_derive(to = "clt", repr = "u8")]
pub enum Param1Type {
None = 0,
Light,
}
#[mt_derive(to = "clt", repr = "u8")]
pub enum Param2Type {
Nibble = 0,
Byte,
Flowing,
FaceDir,
Mounted,
Leveled,
Rotation,
Mesh,
Color,
ColorFaceDir,
ColorMounted,
GrassLikeLevel,
ColorRotation,
}
#[mt_derive(to = "clt", repr = "u8")]
pub enum DrawType {
Cube = 0,
None,
Liquid,
Flowing,
GlassLike,
AllFaces,
AllFacesOpt,
Torch,
Sign,
Plant,
Fence,
Rail,
NodeBox,
GlassFrame,
Fire,
GlassFrameOpt,
Mesh,
RootedPlant,
}
#[mt_derive(to = "clt", repr = "u8")]
pub enum Waving {
None = 0,
Plant,
Leaf,
Liquid,
}
#[mt_derive(to = "clt", repr = "u8")]
pub enum Liquid {
None = 0,
Flowing,
Source,
}
#[mt_derive(to = "clt", repr = "u8")]
pub enum Alpha {
Blend = 0,
Mask, // either fully opaque or transparent
Opaque,
Legacy,
}
#[mt_derive(to = "clt", repr = "u8", tag = "type")]
pub enum TileAnim {
None = 0,
VerticalFrame {
n_frames: [u16; 2],
duration: f32,
},
SpriteSheet {
aspect_ratio: [u8; 2],
duration: f32,
},
}
#[mt_derive(to = "clt", repr = "u8")]
pub enum TileAlign {
None = 0,
World,
User,
}
#[mt_derive(to = "clt", enumset, custom)]
pub enum TileFlag {
BackfaceCull,
TileHorizontal,
TileVertical,
}
#[mt_derive(to = "clt", repr = "u16", enumset)]
enum TileFlagInternal {
BackfaceCull,
TileHorizontal,
TileVertical,
Color,
Scale,
Align,
}
#[mt_derive(to = "clt", custom)]
pub struct TileDef {
// #[mt(const_before = "6u8")]
pub texture: String,
pub animation: TileAnim,
pub flags: EnumSet<TileFlag>,
pub color: Option<[u8; 3]>,
pub scale: Option<u8>,
pub align: Option<TileAlign>,
}
#[cfg(feature = "server")]
impl MtSerialize for TileDef {
fn mt_serialize<C: MtCfg>(
&self,
writer: &mut impl std::io::Write,
) -> Result<(), mt_ser::SerializeError> {
6u8.mt_serialize::<DefCfg>(writer)?;
self.texture.mt_serialize::<DefCfg>(writer)?;
self.animation.mt_serialize::<DefCfg>(writer)?;
let mut flags: EnumSet<TileFlagInternal> = self
.flags
.iter()
.map(|f| match f {
TileFlag::BackfaceCull => TileFlagInternal::BackfaceCull,
TileFlag::TileHorizontal => TileFlagInternal::TileHorizontal,
TileFlag::TileVertical => TileFlagInternal::TileVertical,
})
.collect();
if self.color.is_some() {
flags.insert(TileFlagInternal::Color);
}
if self.scale.is_some() {
flags.insert(TileFlagInternal::Scale);
}
if self.align.is_some() {
flags.insert(TileFlagInternal::Align);
}
flags.mt_serialize::<DefCfg>(writer)?;
self.color.mt_serialize::<DefCfg>(writer)?;
self.scale.mt_serialize::<DefCfg>(writer)?;
self.align.mt_serialize::<DefCfg>(writer)?;
Ok(())
}
}
#[cfg(feature = "client")]
impl MtDeserialize for TileDef {
fn mt_deserialize<C: MtCfg>(
reader: &mut impl std::io::Read,
) -> Result<Self, mt_ser::DeserializeError> {
let const6u8 = MtDeserialize::mt_deserialize::<DefCfg>(reader)?;
if 6u8 != const6u8 {
return Err(mt_ser::DeserializeError::InvalidConst(
Box::new(const6u8),
Box::new(6u8),
));
}
let texture = MtDeserialize::mt_deserialize::<DefCfg>(reader)?;
let animation = MtDeserialize::mt_deserialize::<DefCfg>(reader)?;
let flags_internal = EnumSet::<TileFlagInternal>::mt_deserialize::<DefCfg>(reader)?;
let color = if flags_internal.contains(TileFlagInternal::Color) {
Some(MtDeserialize::mt_deserialize::<DefCfg>(reader)?)
} else {
None
};
let scale = if flags_internal.contains(TileFlagInternal::Scale) {
Some(MtDeserialize::mt_deserialize::<DefCfg>(reader)?)
} else {
None
};
let align = if flags_internal.contains(TileFlagInternal::Align) {
Some(MtDeserialize::mt_deserialize::<DefCfg>(reader)?)
} else {
None
};
let flags = flags_internal
.iter()
.flat_map(|f| match f {
TileFlagInternal::BackfaceCull => Some(TileFlag::BackfaceCull),
TileFlagInternal::TileHorizontal => Some(TileFlag::TileHorizontal),
TileFlagInternal::TileVertical => Some(TileFlag::TileVertical),
_ => None,
})
.collect();
Ok(Self {
texture,
animation,
flags,
color,
scale,
align,
})
}
}
#[mt_derive(to = "clt", repr = "u8", tag = "type")]
#[mt(const_before = "6u8")]
pub enum NodeBox {
Cube = 0,
Fixed {
fixed: Vec<RangeInclusive<[f32; 3]>>,
},
Mounted {
wall_top: RangeInclusive<[f32; 3]>,
wall_bottom: RangeInclusive<[f32; 3]>,
wall_sides: RangeInclusive<[f32; 3]>,
},
Leveled {
fixed: Vec<RangeInclusive<[f32; 3]>>,
},
Connected {
fixed: Vec<RangeInclusive<[f32; 3]>>,
connect_dirs: [Vec<RangeInclusive<[f32; 3]>>; 6],
disconnect_dirs: [Vec<RangeInclusive<[f32; 3]>>; 6],
disconnect_all: Vec<RangeInclusive<[f32; 3]>>,
disconnect_sides: Vec<RangeInclusive<[f32; 3]>>,
},
}
#[mt_derive(to = "clt")]
#[mt(size = "u16")]
pub struct NodeDef {
// TODO: impl Default
#[mt(const_before = "13u8")]
pub name: String,
pub groups: HashMap<String, u16>,
pub param1_type: Param1Type,
pub param2_type: Param2Type,
pub draw_type: DrawType,
pub mesh: String,
pub scale: f32,
#[mt(const_before = "6u8")]
pub tiles: [TileDef; 6],
pub overlay_tiles: [TileDef; 6],
#[mt(const_before = "6u8")]
pub special_tiles: [TileDef; 6],
pub color: Color,
pub palette: String,
pub waving: Waving,
pub connect_sides: u8,
pub connect_to: Vec<u16>,
pub inside_tint: Color,
pub level: u8, // FIXME: must be below 128
pub translucent: bool, // sunlight scattering
pub transparent: bool,
pub light_source: u8, // FIXME: max: 14 (?)
pub ground_content: bool,
pub collision: bool,
pub pointable: bool,
pub diggable: bool,
pub climbable: bool,
pub replaceable: bool,
pub has_on_right_click: bool,
pub damage_per_second: i32,
pub liquid: Liquid,
pub flowing_alt: String,
pub source_alt: String,
pub viscosity: u8, // FIXME: 0-7
pub liquid_renewable: bool,
pub flow_range: u8,
pub drown_damage: u8,
pub floodable: bool,
pub draw_box: NodeBox,
pub collision_box: NodeBox,
pub selection_box: NodeBox,
pub footstep_sound: SoundDef,
pub digging_sound: SoundDef,
pub dug_sound: SoundDef,
pub legacy_face_dir: bool,
pub legacy_mounted: bool,
pub dig_predict: String,
pub max_level: u8,
pub alpha: Alpha,
pub move_resistance: u8,
pub liquid_move_physics: bool,
}
#[mt_derive(to = "clt", custom)]
pub struct NodeDefs(pub HashMap<u16, NodeDef>);
// stupid bullshit ahead
#[cfg(feature = "server")]
impl MtSerialize for NodeDefs {
fn mt_serialize<C: MtCfg>(
&self,
writer: &mut impl std::io::Write,
) -> Result<(), mt_ser::SerializeError> {
DefCfg::write_len(self.0.len(), writer)?;
let mut buf = Vec::new();
self.0.mt_serialize::<()>(&mut buf)?;
buf.mt_serialize::<u32>(writer)?;
Ok(())
}
}
#[cfg(feature = "client")]
impl MtDeserialize for NodeDefs {
fn mt_deserialize<C: MtCfg>(
reader: &mut impl std::io::Read,
) -> Result<Self, mt_ser::DeserializeError> {
use mt_ser::MtLen;
let len = DefCfg::read_len(reader)?;
let mut reader = u32::read_len(reader)?.take(mt_ser::WrapRead(reader));
let inner =
mt_ser::mt_deserialize_sized_seq::<DefCfg, _>(&len, &mut reader)?.try_collect()?;
Ok(Self(inner))
}
}
/* TODO: Rustify this
func BuiltinNodeDefs(n int) map[Content]NodeDef {
defs := make(map[Content]NodeDef, 3+n)
defs[Unknown] = NodeDef{
Name: "unknown",
}
defs[Air] = NodeDef{
Name: "air",
DrawType: DrawNothing,
P1Type: P1Light,
Translucent: true,
Transparent: true,
Replaceable: true,
Floodable: true,
GndContent: true,
}
defs[Ignore] = NodeDef{
Name: "ignore",
DrawType: DrawNothing,
Replaceable: true,
GndContent: true,
}
return defs
}
*/
#[mt_derive(to = "clt")]
pub struct ToolGroupCap {
pub uses: i16, // 32to16
pub max_level: i16,
#[mt(len = "u32")]
pub times: HashMap<i16, f32>,
}
#[mt_derive(to = "clt")]
pub struct ToolCaps {
#[mt(const_before = "5u8")]
pub attack_cooldown: f32,
pub max_drop_level: i16,
#[mt(len = "u32")]
pub group_caps: HashMap<String, ToolGroupCap>,
#[mt(len = "u32")]
pub dmg_groups: HashMap<String, u16>,
pub punch_uses: u16, // 32tou16
}
#[mt_derive(to = "clt", repr = "u8")]
pub enum ItemType {
Node = 1,
Craft,
Tool,
}
#[mt_derive(to = "clt", repr = "u8")]
pub enum SoundSource {
Nowhere = 0,
Pos,
Obj,
}
#[mt_derive(to = "clt")]
pub struct SoundDef {
pub name: String,
pub gain: f32,
pub pitch: f32,
pub fade: f32,
}
#[mt_derive(to = "clt")]
#[mt(size = "u16")]
pub struct ItemDef {
#[mt(const_before = "6u8")]
#[serde(rename = "type")]
pub item_type: ItemType,
pub name: String,
pub description: String,
pub inventory_image: String,
pub wield_image: String,
pub wield_scale: [f32; 3],
pub stack_max: u16,
pub usable: bool,
pub can_point_liquids: bool,
#[mt(size = "u16")]
pub tool_caps: Option<ToolCaps>,
pub groups: HashMap<String, u16>,
pub place_predict: String,
pub place_sound: SoundDef,
pub place_fail_sound: SoundDef,
pub point_range: f32,
pub palette: String,
pub color: Color,
pub inventory_overlay: String,
pub wield_overlay: String,
pub short_description: String,
pub place_param2: u8,
}
|