-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_random_world_generation.py
135 lines (106 loc) · 4.89 KB
/
page_random_world_generation.py
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
"""
Part of the 7 Days to Die Wiki Content Generator
Copyright (C) 2017 Liam Brandt <brandt.liam@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from xml.etree import ElementTree
from wiki import name_to_wiki_name, convert_to_link, get_path_settings
class PrefabRule(object):
def __init__(self, name):
self.name = name
self.prefabs = []
#calculate probability for each prefab and set it as the prob attrib as a float
def calculate_and_set_probs(self):
total_prob = 0.0
for prefab in self.prefabs:
if "prob" in prefab.attrib:
total_prob += float(prefab.attrib["prob"])
else:
total_prob += 1.0
for prefab in self.prefabs:
if "prob" in prefab.attrib:
prefab.attrib["prob"] = float(prefab.attrib["prob"]) / total_prob
else:
prefab.attrib["prob"] = 1.0 / total_prob
def grab_nested_prefabs(self, top_level_prefab_rules):
grabbed = []
for prefab in self.prefabs:
if "rule" in prefab.attrib:
#go through top level prefabs to find one that matches this rule
for top_level_prefab_rule in top_level_prefab_rules:
if prefab.attrib["rule"] == top_level_prefab_rule.name:
grabbed += top_level_prefab_rule.grab_nested_prefabs(top_level_prefab_rules)
break
elif "name" in prefab.attrib:
grabbed.append(prefab.attrib["name"])
#return grabbed with no duplicate names
return sorted(set(grabbed))
def write(self, write_file):
write_file.write("*'''Prefab Rule - " + name_to_wiki_name(self.name) + "'''\n")
for prefab in self.prefabs:
prefab.write(write_file)
class Prefab(object):
def __init__(self):
self.attrib = {}
def write(self, write_file):
write_file.write("**")
if "name" in self.attrib:
write_file.write("Prefab - " + convert_to_link(self.attrib["name"], "prefabs", use_get_variant=True) + " - ")
elif "rule" in self.attrib:
write_file.write("Prefab Rule - " + name_to_wiki_name(self.attrib["rule"]) + " - ")
else:
write_file.write("ERROR - ")
write_file.write(str(('%.2f' % (self.attrib["prob"]*100.0)).rstrip('0').rstrip('.')) + "% chance")
write_file.write("\n")
def get_prefab_rules(path_settings):
prefab_rules = []
root = ElementTree.parse(path_settings["xml_rwgmixer"]).getroot()
for item in root:
print(item.tag)
if item.tag == "prefab_rules":
#go through each prefab rule
for prefab_rule in item:
if prefab_rule.attrib["name"] != "none":
new_rule_object = PrefabRule(prefab_rule.attrib["name"])
#go through each prefab in the prefab rule
for prefab in prefab_rule:
if prefab.tag == "prefab":
new_prefab_object = Prefab()
new_prefab_object.attrib = prefab.attrib
new_rule_object.prefabs.append(new_prefab_object)
new_rule_object.calculate_and_set_probs()
prefab_rules.append(new_rule_object)
return prefab_rules
def get_possible_prefabs(prefab_rules):
top_level_rules = ["default", "wildernessGroup"]
possible_prefabs = []
for prefab_rule in prefab_rules:
if prefab_rule.name in top_level_rules:
possible_prefabs += prefab_rule.grab_nested_prefabs(prefab_rules)
return possible_prefabs
def main():
path_settings = get_path_settings()
prefab_rules = get_prefab_rules(path_settings)
#write the random_world_generation.txt
write_file = open(path_settings["folder_wiki_pages"] + "page_random_world_generation.txt", "w")
write_file.truncate()
for prefab_rule in prefab_rules:
prefab_rule.write(write_file)
write_file.close()
possible_prefabs = get_possible_prefabs(prefab_rules)
all_prefabs = []
for prefab_rule in prefab_rules:
all_prefabs += prefab_rule.grab_nested_prefabs(prefab_rules)
impossible_prefabs = list(set(all_prefabs)-set(possible_prefabs))
for prefab_name in impossible_prefabs:
print("impossible prefab: " + prefab_name)
main()