-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapreset.py
202 lines (169 loc) · 7.86 KB
/
mapreset.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
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
#!/usr/bin/python3
"""
Provides a simplified interface to reset sections of a Zomboid Map
Requires config.json for all configurations including your paths
WRITTEN AND TESTED ON A LINUX DEDICATED SERVER.
ADJUST LINE 1 IF YOU ARE USING A DIFFERENT BINARY OR WINDOWS
"""
# ----------------------------------------------------------------------
# COMMON LIBRARIES -----------------------------------------------------
# ----------------------------------------------------------------------
from json import load as jsonload
from math import floor
from os import listdir, remove
from os.path import split as path_split, abspath, join, isfile
from time import time
# ----------------------------------------------------------------------
# CUSTOM LIBRARIES -----------------------------------------------------
# ----------------------------------------------------------------------
from common import logs
# ----------------------------------------------------------------------
# CONFIGURATION --------------------------------------------------------
# ----------------------------------------------------------------------
GLOBAL_CONFIG = join(path_split(abspath(__file__))[0], "config.json")
# ----------------------------------------------------------------------
# FUNCTIONS ------------------------------------------------------------
# ----------------------------------------------------------------------
def in_boundary(X, Y, startX, startY, endX, endY):
"""Determines if a co-ordinate is within a boundary
Args:
X (int): The X co-ordinate to test
Y (int): The Y co-ordinate to test
startX (int): The starting boundary (top-left) X co-ordinate
startY (int): The starting boundary Y co-ordinate
endX (int): The stopping boundary (bottom-right) X co-ordinate
endY (int): The stopping boundary Y co-ordinate
Returns:
boolean: Is the defined X,Y within the boundaries of Start/Stop
"""
return (int(X) >= int(startX)) and (int(X) <= int(endX)) and \
(int(Y) >= int(startY)) and (int(Y) <= int(endY))
def load_configuration(file):
"""Loads the JSON Configuration document as defined
Args:
file (str): The full path and filename to the JSON Configuration
Raises:
ValueError: Improperly formatted configuration
FileNotFoundError: File or Folder not found
Returns:
array: A JSON decoded array of values
"""
if isfile(file):
try:
with open(file) as file_object:
return jsonload(file_object)
except Exception as error:
raise ValueError("Improperly formatted configuration") from error
else:
raise FileNotFoundError(f"File not found: {file}")
def resetmap(log, path,
startX, startY, startChunkX, startChunkY,
endX, endY, endChunkX, endChunkY):
"""Deletes a series of files that are within the boundaries.
Args:
log (obj): The logfile object to record actions to
path (str): Path to the Zomboid Save File Folder
startX (int): The starting X co-ordinate (top left)
startY (int): The starting Y co-ordinate
startChunkX (int): The starting Chunk/Cell X co-ordinate
startChunkY (int): The starting Chunk/Cell Y co-ordinate
endX (int): The stopping X co-ordinate (bottom right)
endY (int): The stopping Y co-ordinate
endChunkX (int): The stopping Chunk/Cell X co-ordinate
endChunkY (int): The stopping Chunk/Cell Y co-ordinate
"""
files = listdir(path)
counter_map = 0
counter_meta = 0
timer_start = time()
# Co-ordinates are sent in in absolute tiles however the map stores them
# in 10 tiles to a line, so we take the co-ordinates, divide by 10,
# floor it and then we can get a filename boundary check
startX = floor(startX/10)
startY = floor(startY/10)
endX = floor(endX/10)
endY = floor(endY/10)
# Loop through the map files and delete any that are within our boundareies
for file in files:
current_item = file[:-4].split('_')
# Clear out Map files that are within our co-ordinates
if len(current_item) == 3 and current_item[0] == 'map':
if in_boundary(current_item[1], current_item[2],
startX, startY, endX, endY):
counter_map += 1
log.add(f"> Deleting {file}")
remove(join(path, file))
# If not a map file, but actually meta data then if it's in chunk data
# OLD METHOD: Trash chunkdata AND zombie population
#elif len(current_item) == 3 and (current_item[0] == 'chunkdata' or current_item[0] == 'zpop'):
# NEW METHOD: Don't delete zombie population to avoid multiplayer lag fest on regen
elif len(current_item) == 3 and current_item[0] == 'chunkdata':
if in_boundary(current_item[1], current_item[2],
startChunkX, startChunkY, endChunkX, endChunkY):
counter_meta += 1
log.add(f"> Deleting {file}")
remove(join(path, file))
log.add_header("Summary")
log.add(f"> Completed in {(time() - timer_start):.2f} seconds")
log.add(f"> Removed {counter_map:,d} map files")
log.add(f"> Removed {counter_meta:,d} meta files")
# ----------------------------------------------------------------------
# MAIN -----------------------------------------------------------------
# ----------------------------------------------------------------------
def main(user_config):
"""Main application routine
Args:
user_config (str): The Path & Filename of the configuration document
"""
# Load Configuration settings
config = load_configuration(user_config)
# Setup Logfile settings
log = logs.NewLog(65, config['logs']['save'], config['logs']['path'])
log.enable_treeview()
# Prompt the user for which region they want to be reset
counter = 0
print("Which region would you like to reset?")
print("")
for region in config['regions']:
print(f" {counter}\t{region['name']}")
counter += 1
print("")
selection = input("Reset region #")
try:
selection = int(selection)
except ValueError:
print("ERROR: Incorrect region selection")
exit(1)
if int(selection) < 0 or (int(selection) >= int(counter)):
print("ERROR: Region out of bounds")
exit(2)
# Display some meta information about your selection
log.timestamp()
log.add_header(f"{config['regions'][selection]['name']}")
log.add(f"> Region: {selection}")
log.add(f"> Start: {config['regions'][selection]['start'][0]} x "
f"{config['regions'][selection]['start'][1]} "
f" Chunk ({config['regions'][selection]['start'][2]} x "
f"{config['regions'][selection]['start'][3]})")
log.add(f"> Stop: {config['regions'][selection]['stop'][0]} x "
f"{config['regions'][selection]['stop'][1]} "
f" Chunk ({config['regions'][selection]['stop'][2]} x "
f"{config['regions'][selection]['stop'][3]})")
log.add_header("Reset Log")
# Reset the selected boundaries
resetmap(log, config['path'],
config['regions'][selection]['start'][0],
config['regions'][selection]['start'][1],
config['regions'][selection]['start'][2],
config['regions'][selection]['start'][3],
config['regions'][selection]['stop'][0],
config['regions'][selection]['stop'][1],
config['regions'][selection]['stop'][2],
config['regions'][selection]['stop'][3])
# Done
log.timestamp()
# ----------------------------------------------------------------------
# ROUTINE --------------------------------------------------------------
# ----------------------------------------------------------------------
if __name__ == "__main__":
main(GLOBAL_CONFIG)