-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.py
190 lines (180 loc) · 5.29 KB
/
gen.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
import os
import json
from server_main import format_level, read_file, write_file
import random
import typing
import math
level_datas: list[dict[str, typing.Any]] = []
for i in os.listdir("levels/published/"):
level_datas.append(json.loads(read_file("levels/published/" + i)))
for i in os.listdir("levels/user/"):
level_datas.append(json.loads(read_file("levels/user/" + i)))
class Rect:
def __init__(self, x: int, y: int, width: int, height: int):
self.x = x
self.y = y
self.width = width
self.height = height
def colliderect(self, other: "Rect") -> bool:
return (self.x < other.x + other.width and
self.x + self.width > other.x and
self.y < other.y + other.height and
self.y + self.height > other.y)
def copy(self):
return Rect(self.x, self.y, self.width, self.height)
T = typing.TypeVar('T')
R = typing.TypeVar('R')
class Optional(typing.Generic[T]):
def __init__(self, value: T | None = None):
self._value = value
def ifPresent(self, func: typing.Callable[[ T ], typing.Any]):
if self._value != None:
func(self._value)
def getOr(self, replacement: T) -> T:
if self._value == None:
return replacement
else:
return self._value
def transform(self, func: typing.Callable[[ T ], R]) -> "Optional[R]":
if self._value == None:
return Optional()
else:
return Optional(func(self._value))
class LevelObject:
def __init__(self, data: dict[str, typing.Any]):
self.data = data
def getData(self, name: str) -> Optional[float]:
if name in self.data["data"].keys():
d = self.data["data"][name]
try:
r = int(d)
return Optional(r)
except: pass
return Optional()
def setData(self, name: str, data: Optional[float]):
data.ifPresent(lambda x: self.setRawData(name, x))
def setRawData(self, name: str, data: float):
self.data["data"][name] = data
def moveBy(self, x: float, y: float):
self.setData("x", self.getData("x").transform(lambda v: v + x))
self.setData("y", self.getData("y").transform(lambda v: v + y))
def copy(self):
return LevelObject(dict(self.data))
class Level:
def __init__(self, name: str, objects: list[LevelObject]):
self.name = name
self.objects = objects
def copy(self):
return Level(self.name, [o.copy() for o in self.objects])
def rotate(self):
for o in self.objects:
oldX = o.getData("x")
oldY = o.getData("y")
oldR = o.getData("rotation")
oldY.ifPresent(lambda y: o.setData("x", oldX.transform(lambda x: y)))
oldX.ifPresent(lambda x: o.setData("y", oldY.transform(lambda y: -x)))
# o.setData("rotation", oldR.transform(lambda r: (180 - r) % 360))
# that was for flipping whoops
o.setData("rotation", oldR.transform(lambda r: (r + 90) % 360))
self.align()
def getXs(self):
x: list[float] = []
for o in self.objects:
o.getData("x").ifPresent(x.append)
return x
def getYs(self):
y: list[float] = []
for o in self.objects:
o.getData("y").ifPresent(y.append)
return y
def align(self):
x = self.getXs()
y = self.getYs()
if len(x) == 0 or len(y) == 0: return
minX = min(x)
minY = min(y)
for o in self.objects:
o.moveBy(-minX, -minY)
def getRect(self) -> Rect:
self.align()
x = self.getXs()
y = self.getYs()
if len(x) == 0 or len(y) == 0: return Rect(0, 0, 0, 0)
maxX = max(x)
maxY = max(y)
return Rect(0, 0, math.ceil(maxX), math.ceil(maxY))
def doesCollide(self, other: "Level", pos: tuple[float, float]):
thisXs = self.getXs()
thisYs = self.getYs()
otherXs = other.getXs()
otherYs = other.getYs()
for i in range(len(thisXs)):
for n in range(len(otherXs)):
if thisXs[i] == otherXs[n] + pos[0]:
if thisYs[i] == otherYs[n] + pos[1]:
return True
return False
def blit(self, other: "Level", pos: tuple[float, float]):
oc = other.copy()
for o in oc.objects:
o.moveBy(pos[0], pos[1])
self.objects.append(o)
def explode(self):
for o in [*self.objects]:
chance = 0.3
if o.data["type"].split(".")[0] == "death":
chance = 0.9
if random.random() < chance:
self.objects.remove(o)
def save(self):
coins: list[bool] = []
for o in self.objects:
if o.data["type"] == "special.coin":
coins.append(False)
formatted = format_level({
"name": "Combination",
"description": self.name,
"settings": {
"colorbg": [0, 125, 255],
"colorstage": [0, 125, 255],
"gamemode": "cube"
},
"objects": [x.data for x in self.objects],
"completion": {
"percentage": 0,
"coins": coins
},
"deleted": False
})
write_file(f"levels/user/gen_{random.randint(1, 100000)}.json", formatted)
@staticmethod
def fromDict(name: str, data: list[dict[str, typing.Any]]):
return Level(name, [LevelObject(x) for x in data])
levels: list[Level] = [Level.fromDict(x["name"], x["objects"]) for x in level_datas]
combined: Level = Level("Combination of everything", [])
levelsLeft: list[Level] = []
for l in levels:
levelsLeft.append(l.copy())
l.rotate()
levelsLeft.append(l.copy())
l.rotate()
levelsLeft.append(l.copy())
l.rotate()
levelsLeft.append(l.copy())
random.shuffle(levelsLeft)
levelsLeft = levelsLeft[:15]
positions: list[tuple[int, int]] = []
for l in levelsLeft:
l.explode()
pos = (
random.randint(-10, 10),
random.randint(-10, 10)
)
positions.append(pos)
for i in range(len(levelsLeft)):
l = levelsLeft[i]
pos = positions[i]
if not combined.doesCollide(l, pos):
combined.blit(l, pos)
combined.align()
combined.save()