summaryrefslogtreecommitdiff
path: root/worldmap_gen.rb
blob: e52eefc5b153b6691683231e3409ea2670ca4cb8 (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
require 'rexml/document'

doc = nil
File::open("#{ARGV[0]}.xml") do |f|
	doc = REXML::Document.new(f.read)
end

class Exit
	attr_accessor :path
	attr_accessor :reverse

	def initialize(element)
		@path = element.attribute('to').value
		@reverse = false

		unless element.attribute('reverse').nil?
			@reverse = true
		end
	end
end

class Point
	attr_accessor :exits
	attr_accessor :type
	attr_accessor :params
	attr_accessor :position
	attr_reader :id

	def initialize(element)
		@exits = {}
		@type = ''
		@params = [0,0,0,0]
		@position = [0,0,0]

		position = element.attribute 'position'
		@position = position.value.split(',').collect { |v| v.to_f }

		type = element.attribute 'type'
		params = element.attribute 'params'

		@type = type.value unless type.nil?
		@params = params.value.split(',').collect { |v| v.to_i } unless params.nil?

		element.each_element 'exit' do |e|
			@exits[e.attribute('direction').value] = Exit.new(e)
		end

		@id = $point_id
		$point_id += 1
	end
end

class Segment
	attr_accessor :start_pos
	attr_accessor :end_pos
	attr_accessor :speed
	attr_accessor :anim
	attr_accessor :anim_speed
	attr_accessor :direction, :same_dir
	attr_reader :id
	# todo: actions

	def initialize(element)
		from = element.attribute('from').value
		to = element.attribute('to').value

		if $points.has_key? from
			@start_pos = $points[from].position
			puts "From: point #{from} #{@start_pos}"
		else
			@start_pos = from.split(',').collect { |v| v.to_f }
			puts "From: position #{from}"
		end

		if $points.has_key? to
			@end_pos = $points[to].position
			puts "To: point #{to} #{@end_pos}"
		else
			@end_pos = to.split(',').collect { |v| v.to_f }
			puts "To: position #{to}"
		end

		@speed = 4.5
		speed = element.attribute 'speed'
		unless speed.nil?
			@speed = speed.value.to_f
		end

		@anim = 'run'
		@anim_speed = 2.0

		anim = element.attribute 'animation'
		unless anim.nil?
			@anim = anim.value
		end

		anim_speed = element.attribute 'animation_speed'
		unless anim_speed.nil?
			@anim_speed = anim_speed.value.to_f
		end

		@direction = nil
		direction = element.attribute 'direction'
		unless direction.nil?
			val = direction.value
			if val == 'last'
				@direction = nil
			elsif val == 'up'
				@direction = 270
			elsif val == 'right'
				@direction = 0
			elsif val == 'left'
				@direction = 180
			elsif val == 'down'
				@direction = 90
			else
				@direction = val.to_f
			end
		else
			#p @start_pos
			#p @end_pos
			if @start_pos[0] == @end_pos[0] and @start_pos[2] == @end_pos[2]
				# special case
				@direction = nil
			else
				@direction = Math.atan2(@end_pos[2]-@start_pos[2], @end_pos[0]-@start_pos[0])
				@direction *= 180 / Math::PI
			end
		end

		@same_dir = !(element.attribute('same_dir').nil?)

		@id = $segment_id
		$segment_id += 1

		$segments << self
	end
end

class Path
	attr_accessor :segments
	attr_accessor :segment_ids
	attr_accessor :start_point
	attr_accessor :end_point
	attr_reader :id

	def initialize(element)
		@start_point = element.attribute('start').value
		@end_point = element.attribute('end').value

		@segments = []
		@segment_ids = []

		element.each_element 'segment' do |e|
			@segments << Segment.new(e)
			@segment_ids << @segments.last.id
		end

		@id = $path_id
		$path_id += 1
	end
end



point_types = {}
point_types[''] = 0 # default
point_types['level'] = 1

anim_types = {}
anim_types[''] = 1 # default

open('player_anim_list.txt') do |f|
	f.each do |line|
		match = line.match /(\d+)=(.+)/
		if match
			anim_types[match[2]] = match[1].to_i unless anim_types.include? match[2]
		end
	end
end


directions = {'left' => 0, 'right' => 1, 'up' => 2, 'down' => 3}

# first, collect every point
$points = {}
$point_id = 0

puts "[[[ Getting points ]]]"

doc.each_element 'map/point' do |p|
	puts "Reading #{p.attribute 'name'}..."
	$points[p.attribute('name').value] = Point.new(p)
end

puts "[[[ Points done ]]]"

# next, collect every path
$paths = {}
$path_id = 0

$segments = []
$segment_id = 0

puts "[[[ Getting paths ]]]"

doc.each_element 'map/path' do |p|
	puts "Reading #{p.attribute 'name'}..."
	$paths[p.attribute('name').value] = Path.new(p)
end

puts "[[[ Paths done ]]]"


puts "Statistics: #{$points.count} points, #{$paths.count} paths, #{$segments.count} segments"

# now write the file
File::open("#{ARGV[0]}.wm", "w") do |f|
	f << 'NwWM'

	# calculate all this stuff
	header_size = 0x24

	path_list_offs = header_size
	point_list_offs = path_list_offs + ($paths.count * 4)
	segment_list_offs = point_list_offs + ($points.count * 4)
	action_list_offs = segment_list_offs + ($segments.count * 4)
	data_offset = action_list_offs # + ($actions.count * 4)

	f << [path_list_offs, $paths.count].pack('NN')
	f << [point_list_offs, $points.count].pack('NN')
	f << [segment_list_offs, $segments.count].pack('NN')
	f << [action_list_offs, 0].pack('NN')

	# now work out the offsets for everything
	current_offset = data_offset
	$paths.each_value do |p|
		f << [current_offset].pack('N')
		current_offset += 0xC + (p.segments.count * 4)
	end

	$points.each_value do |p|
		f << [current_offset].pack('N')
		current_offset += 0x40
	end

	$segments.each do |s|
		f << [current_offset].pack('N')
		current_offset += 0x2C
	end

	# nothing for actions yet!

	# now write the data
	$paths.each_value do |p|
		f << [$points[p.start_point].id, $points[p.end_point].id].pack('NN')
		f << [p.segments.count].pack('N')
		f << p.segment_ids.pack('N*')
	end

	$points.each_value do |p|
		['left', 'right', 'up', 'down'].each do |direction|
			if p.exits.include? direction
				e = p.exits[direction]
				f << [$paths[e.path].id].pack('N')
				f << [e.reverse ? 1 : 0].pack('Cxxx')
			else
				f << [-1,-1].pack('NN')
			end
		end

		f << [point_types[p.type]].pack('N')
		f << p.params.pack('N*')
		f << p.position.pack('g*')
	end

	$segments.each do |s|
		f << s.start_pos.pack('g*')
		f << s.end_pos.pack('g*')
		f << [s.speed].pack('g')
		f << [anim_types[s.anim]].pack('N')
		f << [s.anim_speed].pack('g')
		if s.direction.nil?
			f << [0, 1, 0].pack('nCC')
		else
			f << [((90.0 - s.direction) % 360) / (360.0 / 65536.0), 0, s.same_dir ? 1 : 0].pack('nCC')
		end
		f << [0].pack('N')
	end
end