-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.gd
More file actions
338 lines (292 loc) · 9.58 KB
/
script.gd
File metadata and controls
338 lines (292 loc) · 9.58 KB
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
extends Control
const SKIP_ARR: PackedStringArray = [
"Mario.levelWid",
"Mario.levelHei",
"Mario.drawLevel",
"Mario.Hud.Time",
]
const BLOCKS_STRING: String = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
## array of tile strings
var level: PackedStringArray
var data: PackedByteArray
var output: PackedStringArray
var output_plain: PackedStringArray
@onready var button_3: CheckBox = $Button3
func read_file(file: FileAccess) -> void:
# reading the input file as text
level.clear()
while !file.eof_reached():
var _line: String = file.get_line()
_line = _line.dedent()
level.append(_line)
file.close()
output.clear()
output_plain.clear()
data.clear()
$Label.text = "Reading..."
$CodeEdit.text = "\n".join(level)
await get_tree().process_frame
# the first 4 bytes are width and height of the level, these are always 500 and 15 accordingly
data.resize(4)
data.encode_u16(0, 500)
data.encode_u16(2, 15)
# initiating tile data arrays
var level_arrays: Array
level_arrays.resize(15)
for i in len(level_arrays):
# filling with empty tiles
var _s: String = " ".repeat(500)
level_arrays[i] = _s
# initiating object arrays
var obj_arrays: Array
# reading the input file line by line
for line in len(level):
# first 15 lines are tiles, reading them as tile data
if line < 15:
var line_header := "Mario.level[%d]" % line
if !level[line].begins_with(line_header):
return _print("Could not find %s" % line_header)
var is_level_data: bool = false
var char_index: int = 0
for i in len(level[line]):
# Skipping the "Mario.level..." string
if i < len(line_header): continue
var lvl_byte: String = level[line][i]
# Write only what's inside the quotes
if lvl_byte == "\"":
is_level_data = !is_level_data
continue
if is_level_data:
level_arrays[line][char_index] = lvl_byte
char_index += 1
continue
# skip lines that we don't need
var _skip: bool
for i in SKIP_ARR:
if level[line].begins_with(i):
_skip = true
continue
if _skip: continue
# reading object data
var _obj = obj_logic(line)
if _obj.size() == 3:
obj_arrays.append(_obj)
# writing tile data
for h in level_arrays[0].length():
for tile in level_arrays:
tile_logic(tile[h])
# writing object data
for i in obj_arrays:
print(i)
write_short(i[0])
write_short(i[1])
write_short(i[2])
$Button.disabled = false
$Label.text = "Success, press the SAVE button."
if output.size() > 0 && button_3.button_pressed:
OS.alert("\n".join(output))
func tile_logic(tile: String) -> void:
match tile:
" ": write_byte(0xDE)
_:
#print(tile)
# index of the tile character in BLOCKS_STRING
var i: int = BLOCKS_STRING.find(tile)
write_byte(i)
func obj_logic(line: int) -> Array:
var line_chars: String = level[line]
# we don't need semicolons
if line_chars.ends_with(";"):
line_chars = line_chars.left(-1)
# get an array of X pos, Y pos and ID of the object
var obj_arr: Array = get_obj(line_chars, line)
# if ID is invalid, don't add the object
if obj_arr[2] == -1: return []
return obj_arr
func get_obj(line: String, line_index: int) -> Array:
var state: int
var obj_header: String = ""
var x: String = ""
var y: String = ""
var obj_footer: String = ""
for chara in line:
match state:
0:
if !chara.is_valid_int():
obj_header += chara
else:
state = 1
x += chara
1:
if chara.is_valid_int():
x += chara
else:
state = 2
2:
if chara.is_valid_int():
state = 3
y += chara
3:
if chara.is_valid_int():
y += chara
else:
state = 4
4:
if chara == "\"":
obj_footer += "'"
else:
obj_footer += chara
var can_ignore: String
var id: int = -1
match obj_header:
"Mario.instObjects.push(new Cannon(": id = 0
"Mario.instEnemies.push(new Goomba(": id = 1
"Mario.instEnemies.push(new TroopaGreen(": id = 2
"Mario.instEnemies.push(new TroopaRed(": id = 3
"Mario.instEnemies.push(new PPlant(":
if !"-1))" in obj_footer: id = 4
else: id = 5
"Mario.instEnemies.push(new Spiny(": id = 6
"Mario.instEnemies.push(new BBeetle(": id = 7
"Mario.instEnemies.push(new Lakitu(": id = 8
"Mario.instEnemies.push(new FlyGreen(": id = 9
"Mario.instEnemies.push(new FlyRed(": id = 10
"Mario.instEnemies.push(new JumperThrower(": id = 11
"Mario.instEnemies.push(new HammerTroopa(":
if !"true))" in obj_footer: id = 12
else: id = 13
"Mario.instEnemies.push(new PStatic(": id = 14
"Mario.instEnemies.push(new PFlame(":
if !"-1))" in obj_footer: id = 15
else: id = 16
"Mario.instObjects.push(new Lava(": id = 17
"Mario.instObjects.push(new Mill(": id = 18
"Player.ResetPlayer(": id = 19
"Mario.instObjects.push(new Finish(": id = 20
# WARNING: All these elevators are saved in the editor as one object! They are different.
"Mario.instObjects.push(new Elevator(", \
"Mario.instObjects.push(new ElevatorFall(", \
"Mario.instObjects.push(new ElevatorLoop(", \
"Mario.instObjects.push(new ElevatorBounce(":
output_plain.append("Line %d: %s" % [line_index + 1, line])
can_ignore = line
if "100,'elevator_4'))" in obj_footer: id = 21
if "75,'elevator_3'))" in obj_footer: id = 22
if "50,'elevator_2'))" in obj_footer: id = 23
if "25,'elevator_1'))" in obj_footer: id = 24
"Mario.instBlocks.push(new Brick(": id = 25
"Mario.instBlocks.push(new BrickMulti(": id = 26
"Mario.instBlocks.push(new Bonus(":
if "0))" in obj_footer: id = 27
if "1))" in obj_footer: id = 28
if "2))" in obj_footer: id = 29
if "3))" in obj_footer: id = 30
if "4))" in obj_footer: id = 31
if "5))" in obj_footer: id = 32
if "1,true))" in obj_footer: id = 33
if "2,true))" in obj_footer: id = 34
if "3,true))" in obj_footer: id = 35
if "4,true))" in obj_footer: id = 36
if "5,true))" in obj_footer: id = 37
"Mario.instObjects.push(new Coin(": id = 38
"Mario.layerWall.addChild(AddGraphic(":
if "'back_fence_1'))" in obj_footer: id = 39
if "'back_fence_2'))" in obj_footer: id = 40
if "'back_fence_3'))" in obj_footer: id = 41
# Grass ids are 42, 43, 44
if "'back_tree_1'))" in obj_footer: id = 45
if "'back_tree_2'))" in obj_footer: id = 46
if "'back_tree_3'))" in obj_footer: id = 47
if "'back_tree_4'))" in obj_footer: id = 48
if "'back_tree_5'))" in obj_footer: id = 49
"Mario.instEffects.push(new Grass(":
if "'l'))" in obj_footer: id = 42
if "'m'))" in obj_footer: id = 43
if "'r'))" in obj_footer: id = 44
"Mario.instEffects.push(new Cloud(": id = 50
"Mario.instEffects.push(new Background(":
if "'back_clouds_1'," in obj_footer: id = 51
if "'back_clouds_2'," in obj_footer: id = 52
if "'back_clouds_3'," in obj_footer: id = 53
if "'back_clouds_4'," in obj_footer: id = 54
if "'back_clouds_5'," in obj_footer: id = 55
if "'back_clouds_6'," in obj_footer: id = 56
if "'back_clouds_7'," in obj_footer: id = 57
if "'back_clouds_8'," in obj_footer: id = 58
if "'back_column_1'," in obj_footer: id = 59
if "'back_column_2'," in obj_footer: id = 60
if "'back_column_3'," in obj_footer: id = 61
if "'back_column_4'," in obj_footer: id = 62
if "'back_forest_1'," in obj_footer: id = 63
if "'back_forest_2'," in obj_footer: id = 64
if "'back_forest_3'," in obj_footer: id = 65
if "'back_forest_4'," in obj_footer: id = 66
if "'back_hill'," in obj_footer: id = 67
if "'back_hill_big'," in obj_footer: id = 68
"Mario.instEffects.push(new Lantern(": id = 69
# TODO: background objects
_:
push_warning("Unknown header: %s" % obj_header)
id = -1
if can_ignore != line:
output_plain.append("Line %d: %s" % [line_index + 1, line])
can_ignore = line
if output.size() == 10:
output.append("<truncated>")
if output.size() < 10:
output.append("Unknown header: %s\n at line %d" % [obj_header, line_index + 1])
return [0, 0, -1]
if id == -1:
push_warning("Unknown footer: %s" % obj_footer)
if can_ignore != line:
output_plain.append("Line %d: %s" % [line_index + 1, line])
can_ignore = line
if output.size() == 10:
output.append("<truncated>")
if output.size() < 10:
output.append("Unknown footer: %s\n at line %d" % [obj_footer, line_index + 1])
return [floor(int(x)) / 25, floor(int(y)) / 25, id]
func write_byte(byte: int) -> int:
var offset = data.size()
data.resize(offset + 1)
data.encode_u8(offset, byte)
return offset + 1
func write_short(short: int) -> int:
var offset = data.size()
data.resize(offset + 2)
data.encode_u16(offset, short)
return offset + 2
func write_file(path: String) -> void:
var file = FileAccess.open(path, FileAccess.WRITE)
file.store_buffer(data)
file.close()
var out_path = path.left((len(path.get_extension()) * -1) - 1) + "-log.txt"
if output_plain.size() > 0:
var file2 = FileAccess.open(out_path, FileAccess.WRITE)
if !file2:
if $Button3.button_pressed: OS.alert("Could not save the log file")
return
file2.store_csv_line(output_plain, "\n")
file2.close()
func _on_file_dialog_file_selected(path: String) -> void:
var file = FileAccess.open(path, FileAccess.READ)
if file.get_error() != OK:
return _print(error_string(file.get_error()))
read_file(file)
func _on_file_dialog_canceled() -> void:
if $CodeEdit.text == "": return
get_tree().quit()
func _on_button_pressed() -> void:
if len(level) == 0: return _print("Empty level")
$FileDialog2.current_file = "level.lev"
$FileDialog2.show()
func _on_button_2_pressed() -> void:
$FileDialog2.hide()
$FileDialog.show()
level.clear()
data.clear()
func _print(text: String) -> void:
$Label.text = text
func _on_file_dialog_2_file_selected(path: String) -> void:
print(path)
write_file(path)