Skip to content

Commit a7c6be5

Browse files
committed
first draft of a lua graph connection script. the base is a json that provides the start location as key and all its exits in a list
1 parent 9f8d77d commit a7c6be5

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

pythonProject/base_structure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ def create_base_structure(path: str, game_name: str, game_dict: dict, test_state
662662
)
663663
if not os.path.exists(path + "/manifest.json"):
664664
game_name_lua = game_name.lower().replace(' ', '_')
665-
for char in ("@", "#", "$", "%", "&", "(", ")", ".", "+", "–", "*", "?", "[", "^", "~"):
665+
for char in ("@", "#", "$", "%", "&", "(", ")", ".", "+", "–", "*", "?", "[", "^", "~", ":", "-", "\\", "/"):
666666
game_name_lua = game_name_lua.replace(char, "_")
667667
game_name_lua = re.sub(r'(_)\1+', r'\1', game_name_lua)
668668
with open(path + "/manifest.json", "w", encoding="utf-8") as manifest:
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import json
2+
import os
3+
import tkinter as tk
4+
from tkinter import filedialog
5+
import argparse
6+
import re
7+
8+
forbidden_chars = ["@", "#", "$", "%", "&", "(", ")", ".", "+", "–", "*", "?", "[", "^", "~", ":", "-", "'", "`",
9+
"/", "\\"]
10+
11+
def create_lua_entrances(json_file_path, base_path):
12+
with open(fr"{base_path}/manifest.json") as manifest_file:
13+
manifest = json.load(manifest_file)
14+
game_name = manifest["game_name"]
15+
game_name_lua = game_name.lower().replace(' ', '_')
16+
for char in forbidden_chars:
17+
game_name_lua = game_name_lua.replace(char, "_")
18+
game_name_lua = re.sub(r'(_)\1+', r'\1', game_name_lua)
19+
assert isinstance(game_name, str)
20+
21+
with open(json_file_path) as json_file:
22+
entrance_data = json.load(json_file)
23+
24+
if not os.path.exists(fr"{base_path}/scripts/logic/graph_logic/er_connections.lua"):
25+
with open(fr"{base_path}/scripts/logic/graph_logic/er_connections.lua", "w", encoding="utf-8") as er_lua:
26+
er_lua.write("a")
27+
with open(fr"{base_path}/scripts/logic/graph_logic/er_connections.lua", "w") as er_connection_lua:
28+
nodes = []
29+
for region in entrance_data.keys():
30+
region_lua = region.lower().replace(' ', '_')
31+
for char in forbidden_chars:
32+
region_lua = region_lua.replace(char, "_")
33+
region_lua = re.sub(r'(_)\1+', r'\1', region_lua)
34+
nodes.append(region_lua)
35+
for exit in entrance_data[region]:
36+
exit_lua = exit.lower().replace(' ', '_')
37+
for char in forbidden_chars:
38+
exit_lua = exit_lua.replace(char, "_")
39+
exit_lua = re.sub(r'(_)\1+', r'\1', exit_lua)
40+
nodes.append(exit_lua)
41+
unique_nodes = list(set(nodes))
42+
er_connection_lua.write("-- these are the nodes for the graph is build from. \n"
43+
"-- Add more if you like\n")
44+
for node in unique_nodes:
45+
er_connection_lua.write(f'local {node} = {game_name_lua}_location.new("{node}")\n')
46+
47+
er_connection_lua.write("-- these are the connections betweens the nodes defined above.\n"
48+
"-- those are all 2-way connections. please go ahead an make them one-way if needed.\n"
49+
"-- the rules are what is needed to get from point A to B for this specific "
50+
"connection.\n"
51+
"-- only add whats actually needed. replace the 'return true' to 'return <whatever "
52+
"you use to build your rules>'\n")
53+
for region in entrance_data:
54+
region_lua = region.lower().replace(' ', '_')
55+
for char in forbidden_chars:
56+
region_lua = region_lua.replace(char, "_")
57+
region_lua = re.sub(r'(_)\1+', r'\1', region_lua)
58+
for exit in entrance_data[region]:
59+
exit_lua = exit.lower().replace(' ', '_')
60+
for char in forbidden_chars:
61+
exit_lua = exit_lua.replace(char, "_")
62+
exit_lua = re.sub(r'(_)\1+', r'\1', exit_lua)
63+
er_connection_lua.write(f'{region_lua}:connect_two_ways({exit_lua}, function() return true end)\n')
64+
65+
66+
67+
if __name__ == "__main__":
68+
root = tk.Tk()
69+
root.withdraw()
70+
71+
print("Select the base-folder of the pack:")
72+
base_path = tk.filedialog.askdirectory()
73+
print("Path to base-folder of the pack: ", base_path)
74+
75+
print("Select the ER.json file path:")
76+
er_json_file_path = tk.filedialog.askopenfilename()
77+
print("Path to ER.json file: ", er_json_file_path)
78+
79+
create_lua_entrances(er_json_file_path, base_path)

0 commit comments

Comments
 (0)