Skip to content

Commit 77bbaca

Browse files
committed
cfg refactor wip 9
1 parent 6ab15d3 commit 77bbaca

File tree

8 files changed

+157
-561
lines changed

8 files changed

+157
-561
lines changed

Autoload/Graphics.gd

+115
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,120 @@
11
extends Node
22

3+
func load_extra_images_from_harddrive():
4+
var CODETIME_START = OS.get_ticks_msec()
5+
var custom_images_dir = Settings.unearthdata.plus_file("custom-object-images")
6+
var image_paths = get_png_files_in_dir(custom_images_dir)
7+
for image_path in image_paths:
8+
var texture = load(image_path)
9+
if texture is Texture:
10+
var image_name = image_path.get_file().get_basename().to_upper()
11+
sprite_id[image_name] = texture
12+
else:
13+
print("Failed to load texture: ", image_path)
14+
print('Loaded extra images from HDD: ' + str(OS.get_ticks_msec() - CODETIME_START) + 'ms')
15+
16+
func get_png_files_in_dir(path):
17+
var png_files = []
18+
var dir = Directory.new()
19+
if dir.open(path) == OK:
20+
dir.list_dir_begin()
21+
var file_name = dir.get_next()
22+
while file_name != "":
23+
if not dir.current_is_dir() and file_name.get_extension().to_lower() == "png":
24+
png_files.append(path.plus_file(file_name))
25+
file_name = dir.get_next()
26+
else:
27+
print("An error occurred when trying to access the directory: ", path)
28+
return png_files
29+
30+
31+
#func load_custom_images_into_array(DATA_ARRAY, thingtypeImageFolder):
32+
# print("Loading /thing-images/" + thingtypeImageFolder + " directory ...")
33+
# var arrayOfFilenames = get_png_files_in_dir(Settings.unearthdata.plus_file("thing-images").plus_file(thingtypeImageFolder))
34+
# for i in arrayOfFilenames:
35+
# var subtypeID = int(i.get_file().get_basename())
36+
# var img = Image.new()
37+
# var err = img.load(i)
38+
# if err == OK:
39+
# var tex = ImageTexture.new()
40+
# tex.create_from_image(img)
41+
# if DATA_ARRAY.has(subtypeID):
42+
# DATA_ARRAY[subtypeID][TEXTURE] = tex
43+
44+
45+
#func look_for_images_to_load(DATA_ARRAY, objectID, thingCfgName):
46+
# if custom_images_list.empty() == true:
47+
# custom_images_list = get_png_filenames_in_dir(Settings.unearthdata.plus_file("custom-object-images"))
48+
#
49+
# var dir = Settings.unearthdata.plus_file("custom-object-images")
50+
#
51+
# var uppercaseImageFilename = thingCfgName+".PNG".to_upper()
52+
# var uppercasePortraitFilename = thingCfgName+"_PORTRAIT.PNG".to_upper()
53+
#
54+
# var realImageFilename = ""
55+
# var realPortraitFilename = ""
56+
#
57+
# if custom_images_list.has(uppercaseImageFilename):
58+
# realImageFilename = custom_images_list[uppercaseImageFilename]
59+
#
60+
# if custom_images_list.has(uppercasePortraitFilename):
61+
# realPortraitFilename = custom_images_list[uppercasePortraitFilename]
62+
#
63+
# if realImageFilename != "":
64+
# var img = Image.new()
65+
# var err = img.load(dir.plus_file(realImageFilename))
66+
# if err == OK:
67+
# var tex = ImageTexture.new()
68+
# tex.create_from_image(img, Texture.FLAG_MIPMAPS+Texture.FLAG_ANISOTROPIC_FILTER)
69+
# #DATA_ARRAY[objectID][Things.TEXTURE] = tex
70+
#
71+
# if realPortraitFilename != "":
72+
# var img = Image.new()
73+
# var err = img.load(dir.plus_file(realPortraitFilename))
74+
# if err == OK:
75+
# var tex = ImageTexture.new()
76+
# tex.create_from_image(img, Texture.FLAG_MIPMAPS+Texture.FLAG_ANISOTROPIC_FILTER)
77+
# #DATA_ARRAY[objectID][Things.PORTRAIT] = tex
78+
#
79+
#func get_png_filenames_in_dir(path):
80+
# var dictionary = {}
81+
# var dir = Directory.new()
82+
# if dir.open(path) == OK:
83+
# dir.list_dir_begin()
84+
# var file_name = dir.get_next()
85+
# while file_name != "":
86+
# if dir.current_is_dir():
87+
# pass
88+
# else:
89+
# if file_name.get_extension().to_upper() == "PNG":
90+
# dictionary[file_name.to_upper().replace(" ", "_")] = file_name
91+
# file_name = dir.get_next()
92+
# else:
93+
# print("An error occurred when trying to access the path.")
94+
# return dictionary
95+
#
96+
#func get_png_files_in_dir(path):
97+
# var array = []
98+
# var dir = Directory.new()
99+
# if dir.open(path) == OK:
100+
# dir.list_dir_begin()
101+
# var file_name = dir.get_next()
102+
# while file_name != "":
103+
# if dir.current_is_dir():
104+
# pass
105+
# else:
106+
# if file_name.get_extension().to_upper() == "PNG":
107+
# var fileNumber = file_name.get_file().get_basename()
108+
# if Utils.string_has_letters(fileNumber) == false:
109+
# array.append(path.plus_file(file_name))
110+
# file_name = dir.get_next()
111+
# else:
112+
# print("An error occurred when trying to access the path.")
113+
# return array
114+
115+
116+
117+
3118
# The keys can be integers or strings (as read from objects.cfg's AnimationID field)
4119
# When they're a String, they can be either read the 'Name' field or the 'AnimationID' field, whichever one is prioritized
5120
var sprite_id = {

Autoload/ObjectNames.gd

+3-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ const NAME_MAPPINGS = {
185185
"WRKBOX_LIGHTNG" : "Box: Lightning Trap",
186186
"WRKBOX_WRDOFPW" : "Box: Word of Power Trap",
187187
"WRKBOX_LAVA" : "Box: Lava Trap",
188-
"WRKBOX_DEMOLTN" : "Box: Dummy Trap 2",
188+
"WRKBOX_DEMOLTN" : "Box: Demolition Trap",
189+
"WRKBOX_DUMMY1" : "Box: Dummy Trap 1",
190+
"WRKBOX_DUMMY2" : "Box: Dummy Trap 2",
189191
"WRKBOX_DUMMY3" : "Box: Dummy Trap 3",
190192
"WRKBOX_DUMMY4" : "Box: Dummy Trap 4",
191193
"WRKBOX_DUMMY5" : "Box: Dummy Trap 5",

Autoload/Things.gd

+12-8
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ var LIST_OF_BOXES = {
171171
097 : [TYPE.TRAP, 4], # Lightning Trap
172172
098 : [TYPE.TRAP, 5], # Word of Power Trap
173173
099 : [TYPE.TRAP, 6], # Lava Trap
174-
100 : [TYPE.TRAP, 7], # Demolition Trap
174+
100 : [TYPE.TRAP, 7], # Dummy Trap 2
175175
101 : [TYPE.TRAP, 8], # Dummy Trap 3
176176
102 : [TYPE.TRAP, 9], # Dummy Trap 4
177177
103 : [TYPE.TRAP, 10], # Dummy Trap 5
@@ -184,6 +184,7 @@ var LIST_OF_BOXES = {
184184
}
185185

186186

187+
187188
var LIST_OF_GOLDPILES = [
188189
3, 6, 43, 128, 136
189190
]
@@ -243,6 +244,15 @@ func convert_relative_256_to_float(datnum):
243244
datnum -= 65536 # Convert to signed by subtracting 2^16
244245
return datnum / 256.0 # Scale it to floating-point
245246

247+
248+
func find_subtype_by_name(thingType, findName):
249+
var data = data_structure(thingType)
250+
for subtype_key in data:
251+
var subtype_data = data[subtype_key]
252+
if subtype_data and subtype_data[NAME_ID] == findName:
253+
return subtype_key
254+
return null
255+
246256
var DATA_EXTRA = {
247257
0 : [null, null, null, null],
248258
1 : ["ACTIONPOINT", "ACTIONPOINT", TAB_ACTION],
@@ -262,15 +272,9 @@ var DATA_TRAP = {
262272
01 : ["BOULDER", "BOULDER", TAB_TRAP],
263273
02 : ["ALARM", "ALARM", TAB_TRAP],
264274
03 : ["POISON_GAS", "POISON_GAS", TAB_TRAP],
265-
04 : ["LIGHTNING", "LIGHTNING_TRAP", TAB_TRAP],
275+
04 : ["LIGHTNING", "LIGHTNING", TAB_TRAP],
266276
05 : ["WORD_OF_POWER", "WORD_OF_POWER", TAB_TRAP],
267277
06 : ["LAVA", "LAVA", TAB_TRAP],
268-
07 : ["TNT", "TNT", TAB_TRAP],
269-
08 : ["DUMMYTRAP3", null, TAB_TRAP],
270-
09 : ["DUMMYTRAP4", null, TAB_TRAP],
271-
10 : ["DUMMYTRAP5", null, TAB_TRAP],
272-
11 : ["DUMMYTRAP6", null, TAB_TRAP],
273-
12 : ["DUMMYTRAP7", null, TAB_TRAP],
274278
}
275279

276280
var DATA_EFFECTGEN = {

Scenes/CfgLoader.gd

+20-12
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ func start():
3030
# Step 3: load data from local config files (map00001.objects.cfg)
3131
print('Loaded things from cfg files: ' + str(OS.get_ticks_msec() - CODETIME_LOADCFG_START) + 'ms')
3232

33-
34-
3533
func load_objects_data():
3634
for section in objects_cfg:
3735
if section.begins_with("object"):
@@ -51,28 +49,38 @@ func load_creatures_data():
5149
for id_number in creature_cfg["common"]["Creatures"].size():
5250
if Things.DATA_CREATURE.has(id_number+1) == false:
5351
var newName = creature_cfg["common"]["Creatures"][id_number]
54-
var newSprite = get_sprite(newName)
52+
var newSprite = get_sprite(newName, null)
5553
Things.DATA_CREATURE[id_number + 1] = [newName, newSprite, Things.TAB_CREATURE]
5654

5755

5856
func load_trapdoor_data():
5957
for section in trapdoor_cfg:
6058
var id = int(section)
61-
if id == 0: continue
59+
var trapOrDoor = -1
60+
if section.begins_with("door"): trapOrDoor = Things.TYPE.DOOR
61+
if section.begins_with("trap"): trapOrDoor = Things.TYPE.TRAP
62+
if id == 0 or trapOrDoor == -1: continue
63+
6264
var data = trapdoor_cfg[section]
6365
var newName = data.get("Name", null)
64-
var newSprite = get_sprite(newName)
65-
if section.begins_with("door"):
66+
var newSprite = get_sprite(newName, null)
67+
var crateName = data.get("Crate", null)
68+
if trapOrDoor == Things.TYPE.DOOR:
6669
Things.DATA_DOOR[id] = [newName, newSprite, Things.TAB_MISC]
67-
elif section.begins_with("trap"):
70+
elif trapOrDoor == Things.TYPE.TRAP:
6871
Things.DATA_TRAP[id] = [newName, newSprite, Things.TAB_TRAP]
72+
73+
var crate_id_number = Things.find_subtype_by_name(Things.TYPE.OBJECT, crateName)
74+
Things.LIST_OF_BOXES[crate_id_number] = [
75+
trapOrDoor,
76+
id,
77+
]
78+
#print(Things.LIST_OF_BOXES)
6979

7080

71-
func get_sprite(id, fallback = null):
72-
if Graphics.sprite_id.has(id):
73-
return id
74-
elif fallback and Graphics.sprite_id.has(fallback):
75-
return fallback
81+
func get_sprite(first_priority, second_priority):
82+
if Graphics.sprite_id.has(first_priority): return first_priority
83+
if Graphics.sprite_id.has(second_priority): return second_priority
7684
return null
7785

7886

Scenes/Main.gd

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func _ready():
1111
Settings.initialize_settings()
1212
initialize_window_settings()
1313
$TextureCache.start() # Needs to be run after Settings initialized so that the GAME_DIRECTORY is correctly set
14+
Graphics.load_extra_images_from_harddrive()
1415
$OpenMap.start()
1516

1617
# Auto switch to 3D while devving

Scenes/Main.tscn

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=191 format=2]
1+
[gd_scene load_steps=190 format=2]
22

33
[ext_resource path="res://Scenes/SlabStyle.gd" type="Script" id=1]
44
[ext_resource path="res://Scenes/SlabPlacement.gd" type="Script" id=2]
@@ -169,7 +169,6 @@
169169
[ext_resource path="res://Scenes/ThreadedSaveUndo.gd" type="Script" id=167]
170170
[ext_resource path="res://Scenes/Guidelines.gd" type="Script" id=168]
171171
[ext_resource path="res://Scenes/CfgLoader.gd" type="Script" id=169]
172-
[ext_resource path="res://Scenes/OldCfgCode.gd" type="Script" id=170]
173172

174173
[sub_resource type="CubeMesh" id=2]
175174
size = Vector3( 1, 1, 1 )
@@ -337,9 +336,6 @@ script = ExtResource( 30 )
337336
[node name="CfgLoader" type="Node" parent="."]
338337
script = ExtResource( 169 )
339338
340-
[node name="OLD_UNUSED_CfgCode" type="Node" parent="CfgLoader"]
341-
script = ExtResource( 170 )
342-
343339
[node name="TextureCache" type="Node" parent="."]
344340
script = ExtResource( 46 )
345341

0 commit comments

Comments
 (0)