summaryrefslogtreecommitdiff
path: root/extractor/rsw_format.gd
blob: 18874e2b3093ec45ce030ea3e74f10179d883c91 (plain)
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
class_name RSWFormat


## Byte Length: 4 [br]
## GRAT
var signature: String = "GRSW"

## Byte Type: u8 [br]
## Byte Length: 2
var version: Version

## Byte Type: u8, u32 [br]
## Byte Length: 1, 4
## Versions: [2.2, 2.5]
var build_number: int

## Byte Type: u8 [br]
## Byte Length: 1
## Versions: [2.5]
var unknown_render_flag: int

## Byte Type: u8 [br]
## Byte Length: 40
var ini_file: String

## Byte Type: u8 [br]
## Byte Length: 40
var gnd_file: String

## Byte Type: u8 [br]
## Byte Length: 40
var gat_file: String

## Byte Type: u8 [br]
## Byte Length: 40
## Versions: [1.4]
var source_file: String

var water_configuration: WaterConfiguration
var lighting_parameters: LightingParameters

## Byte Type: u32 [br]
## Byte Length: 4
var map_resource_count: int

## Length: [member map_resource_count]
var map_resources: Array[MapResource]

var quad_tree


static func from_bytes(bytes: ByteStream) -> RSWFormat:
	var rsw_format = RSWFormat.new()
	
	bytes.advance(rsw_format.signature.length())
	
	@warning_ignore("shadowed_variable")
	var version = Version.new()
	version.major = bytes.decode_u8()
	version.minor = bytes.decode_u8()
	rsw_format.version = version
	
	if version.major < 2:
		print(version)
		return rsw_format
	
	if version.major >= 2 and version.minor >= 5:
		rsw_format.build_number = bytes.decode_u32()
		rsw_format.unknown_render_flag = bytes.decode_u8()
	elif version.major >= 2 and version.minor >= 2:
		rsw_format.build_number = bytes.decode_u8()
	
	rsw_format.ini_file = bytes.get_string_from_utf8(40)
	rsw_format.gnd_file = bytes.get_string_from_utf8(40)
	rsw_format.gat_file = bytes.get_string_from_utf8(40)
	rsw_format.source_file = bytes.get_string_from_utf8(40)
	
	if version.major >= 2 and version.minor <= 5:
		rsw_format.water_configuration = WaterConfiguration.from_bytes(bytes)
	
	rsw_format.lighting_parameters = LightingParameters.from_bytes(bytes)
	
	bytes.advance(16) # map boundaries - bounding box # TODO
	
	rsw_format.map_resource_count = bytes.decode_u32()
	rsw_format.map_resources = [] as Array[MapResource]
	for _n in rsw_format.map_resource_count:
		var resource_type = bytes.decode_u32()
		var resource: MapResource
		
		if resource_type == MapResourceType.Animated3DModel:
			resource = Animated3DModel.from_bytes(bytes)
		elif resource_type == MapResourceType.DynamicLightSource:
			resource = DynamicLightSource.from_bytes(bytes)
		elif resource_type == MapResourceType.SpatialAudioSource:
			resource = SpatialAudioSource.from_bytes(bytes)
		elif resource_type == MapResourceType.ParticleEffectEmitter:
			resource = ParticleEffectEmitter.from_bytes(bytes)
		
		rsw_format.map_resources.append(resource)
	
	# TODO: quadtree
	
	#print(inst_to_dict(rsw_format))
	return rsw_format


class WaterConfiguration:
	## Byte Type: f32 [br]
	## Byte Length: 4
	var water_level: int
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	var water_type: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var wave_height: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var wave_speed: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var wave_pitch: int
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	var water_animation_speed: int
	
	
	static func from_bytes(bytes: ByteStream) -> WaterConfiguration:
		var config = WaterConfiguration.new()
		
		config.water_level = bytes.decode_float()
		config.water_type = bytes.decode_u32()
		config.wave_height = bytes.decode_float()
		config.wave_speed = bytes.decode_float()
		config.wave_pitch = bytes.decode_float()
		config.water_animation_speed = bytes.decode_u32()
		
		return config


class LightingParameters:
	## Byte Type: i32 [br]
	## Byte Length: 4
	var longitude: int
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	var latitude: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var diffuse_color_r: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var diffuse_color_g: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var diffuse_color_b: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var ambient_color_r: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var ambient_color_g: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var ambient_color_b: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	## Shadow Map Alpha
	var intensity: int
	
	
	func get_diffuse_color() -> Color:
		return Color(diffuse_color_r, diffuse_color_g, diffuse_color_b)
	
	
	func get_ambient_color() -> Color:
		return Color(ambient_color_r, ambient_color_g, ambient_color_b)
	
	
	static func from_bytes(bytes: ByteStream) -> LightingParameters:
		var params = LightingParameters.new()
		
		params.longitude = bytes.decode_s32()
		params.latitude = bytes.decode_s32()
		params.diffuse_color_r = bytes.decode_float()
		params.diffuse_color_g = bytes.decode_float()
		params.diffuse_color_b = bytes.decode_float()
		params.ambient_color_r = bytes.decode_float()
		params.ambient_color_g = bytes.decode_float()
		params.ambient_color_b = bytes.decode_float()
		params.intensity = bytes.decode_float()
		
		return params


class MapResource:
	pass

enum MapResourceType {
	Animated3DModel = 1,
	DynamicLightSource = 2,
	SpatialAudioSource = 3,
	ParticleEffectEmitter = 4,
}

enum ModelAnimationType {
	None = 0,
	Looping = 2,
}

enum ParticlePresetEffect {
	None = -1,
	Hit_1 = 1,
	Hit_2 = 2,
	Hit_3 = 3,
	Torch = 47,
	TunaParty = 1097,
}


class Animated3DModel extends MapResource:
	## Byte Type: u8 [br]
	## Byte Length: 40
	var name: String
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var animation_type: ModelAnimationType
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var animation_speed_percent: float
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var collision_flags: int
	
	## Byte Type: u8 [br]
	## Byte Length: 80
	var model_file: String
	
	## Byte Type: u8 [br]
	## Byte Length: 80
	var root_node_name: String
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_x: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_y: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_z: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var rotation_x: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var rotation_y: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var rotation_z: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var scale_x: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var scale_y: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var scale_z: float
	
	
	func get_position() -> Vector3:
		return Vector3(position_x, position_y, position_z)
	
	
	func get_rotation() -> Vector3:
		return Vector3(rotation_x, rotation_y, rotation_z)
	
	
	func get_scale() -> Vector3:
		return Vector3(scale_x, scale_y, scale_z)
	
	
	static func from_bytes(bytes: ByteStream) -> Animated3DModel:
		var resource = Animated3DModel.new()
		
		resource.name = bytes.get_string_from_ascii(40)
		resource.animation_type = bytes.decode_u32()
		resource.animation_speed_percent = bytes.decode_float()
		resource.collision_flags = bytes.decode_u32()
		resource.model_file = bytes.get_string_from_ascii(80)
		resource.root_node_name = bytes.get_string_from_utf8(80)
		resource.position_x = bytes.decode_float()
		resource.position_y = bytes.decode_float()
		resource.position_z = bytes.decode_float()
		resource.rotation_x = bytes.decode_float()
		resource.rotation_y = bytes.decode_float()
		resource.rotation_z = bytes.decode_float()
		resource.scale_x = bytes.decode_float()
		resource.scale_y = bytes.decode_float()
		resource.scale_z = bytes.decode_float()
		
		#print(inst_to_dict(resource))
		return resource


class DynamicLightSource extends MapResource:
	## Byte Type: u8 [br]
	## Byte Length: 80
	var name: String
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_x: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_y: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_z: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var diffuse_color_r: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var diffuse_color_g: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var diffuse_color_b: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var light_range: float
	
	
	func get_position() -> Vector3:
		return Vector3(position_x, position_y, position_z)
	
	
	func get_diffuse_color() -> Color:
		return Color(diffuse_color_r, diffuse_color_g, diffuse_color_b)
	
	
	static func from_bytes(bytes: ByteStream) -> DynamicLightSource:
		var resource = DynamicLightSource.new()
		
		resource.name = bytes.get_string_from_ascii(80)
		resource.position_x = bytes.decode_float()
		resource.position_y = bytes.decode_float()
		resource.position_z = bytes.decode_float()
		resource.diffuse_color_r = bytes.decode_float()
		resource.diffuse_color_g = bytes.decode_float()
		resource.diffuse_color_b = bytes.decode_float()
		resource.light_range = bytes.decode_float()
		
		#print(inst_to_dict(resource))
		return resource


class SpatialAudioSource extends MapResource:
	## Byte Type: u8 [br]
	## Byte Length: 80
	var name: String
	
	## Byte Type: u8 [br]
	## Byte Length: 80
	var audio_file: String
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_x: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_y: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_z: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var volume_gain: float
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var width: int
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var height: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var audio_range: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var duration: float
	
	
	func get_position() -> Vector3:
		return Vector3(position_x, position_y, position_z)
	
	
	func get_size() -> Vector2:
		return Vector2(width, height)
	
	
	static func from_bytes(bytes: ByteStream) -> SpatialAudioSource:
		var resource = SpatialAudioSource.new()
		
		resource.name = bytes.get_string_from_ascii(80)
		resource.audio_file = bytes.get_string_from_ascii(80)
		resource.position_x = bytes.decode_float()
		resource.position_y = bytes.decode_float()
		resource.position_z = bytes.decode_float()
		resource.volume_gain = bytes.decode_float()
		resource.width = bytes.decode_u32()
		resource.height = bytes.decode_u32()
		resource.audio_range = bytes.decode_float()
		resource.duration = bytes.decode_float()
		
		return resource


class ParticleEffectEmitter extends MapResource:
	## Byte Type: u8 [br]
	## Byte Length: 80
	var name: String
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_x: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_y: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var position_z: float
	
	@warning_ignore("enum_variable_without_default")
	## Byte Type: u32 [br]
	## Byte Length: 4
	var preset_effect: ParticlePresetEffect
	
	## Byte Type: f32 [br]
	## Byte Length: 4 [br]
	## in frames; 60 means 1 second
	var emission_delay: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var launch_parameter_a: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var launch_parameter_b: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var launch_parameter_c: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var launch_parameter_d: float
	
	
	func get_position() -> Vector3:
		return Vector3(position_x, position_y, position_z)
	
	
	static func from_bytes(bytes: ByteStream) -> ParticleEffectEmitter:
		var resource = ParticleEffectEmitter.new()
		
		resource.name = bytes.get_string_from_ascii(80)
		resource.position_x = bytes.decode_float()
		resource.position_y = bytes.decode_float()
		resource.position_z = bytes.decode_float()
		resource.preset_effect = bytes.decode_u32()
		resource.emission_delay = bytes.decode_float()
		resource.launch_parameter_a = bytes.decode_float()
		resource.launch_parameter_b = bytes.decode_float()
		resource.launch_parameter_c = bytes.decode_float()
		resource.launch_parameter_d = bytes.decode_float()
		
		return resource