-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructureBase.py
279 lines (234 loc) · 9.71 KB
/
StructureBase.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from __future__ import annotations
import functools
import string
from typing import TYPE_CHECKING, Any
import vectorTools
if TYPE_CHECKING:
from StructureFolder import StructureFolder
from StructureFile import StructureFile
from Node import Node
from glm import ivec3, ivec2
import numpy as np
import globals
import worldTools
from Connector import Connector
import nbtTools
from gdpc.gdpc.interface import placeStructure
from gdpc.gdpc.vector_tools import Box, Rect, loop3D
from gdpc.gdpc.lookup import INVENTORY_BLOCKS, CONTAINER_BLOCK_TO_INVENTORY_SIZE
from gdpc.gdpc.block_state_tools import rotateFacing
from gdpc.gdpc.minecraft_tools import bookData
class Structure:
connectors: list[Connector]
decorationStructureFiles: dict[str, StructureFile]
transitionStructureFiles: dict[str, StructureFile]
structureFile: StructureFile
settlementType: str | None
customProperties: dict[str, Any]
preProcessingSteps: list[worldTools.PlacementInstruction]
_position: ivec3
_facing: int
def __init__(
self,
structureFolder: StructureFolder,
position: ivec3,
facing: int = 0,
settlementType: str = None,
):
self.structureFile = structureFolder.structureFile
self.transitionStructureFiles = structureFolder.transitionStructureFiles
self.decorationStructureFiles = structureFolder.decorationStructureFiles
self.settlementType = settlementType
self.customProperties = dict()
self.preProcessingSteps = []
self.position = position
self.facing = facing
@property
def connectors(self) -> list[Connector]:
return []
@property
def position(self) -> ivec3:
return self._position
@position.setter
def position(self, value: ivec3):
self._position = value
@property
def position2D(self) -> ivec2:
return ivec2(self.position.x, self.position.z)
@property
def position2DCentered(self) -> ivec2:
return self.rectInWorldSpace.center
@property
def facing(self) -> int:
return self._facing
@facing.setter
def facing(self, value: int):
self._facing = value % 4
@functools.cached_property
def box(self) -> Box:
# noinspection PyTypeChecker
return Box(
size=ivec3(
self.structureFile.sizeX,
self.structureFile.sizeY,
self.structureFile.sizeZ
)
)
@property
def boxInWorldSpace(self) -> Box:
# noinspection PyTypeChecker
return Box(
offset=self.position,
size=ivec3(
self.structureFile.sizeX,
self.structureFile.sizeY,
self.structureFile.sizeZ
)
)
@functools.cached_property
def rect(self) -> Rect:
return self.box.toRect()
@property
def rectInWorldSpace(self) -> Rect:
return self.boxInWorldSpace.toRect()
def isIntersection(self, otherStructure: Structure = None) -> bool:
if otherStructure is None:
return False
otherStructureBox = otherStructure.boxInWorldSpace
otherStructureBox.erode()
currentBox = self.boxInWorldSpace
return currentBox.collides(otherStructureBox)
def isSameType(self, otherStructure: Structure = None) -> bool:
if otherStructure:
return otherStructure.structureFile == self.structureFile
return False
@property
def rearFacingConnector(self) -> Connector:
for connector in self.connectors:
if connector.facing == 2:
return connector
return Connector(facing=2)
@property
def inventoryTable(self) -> dict[str, (float, int)]:
return {
'paper': (0.5, 6),
}
def setInventoryBlocks(
self,
rng: np.random.Generator = np.random.default_rng()
) -> list[worldTools.PlacementInstruction]:
newInventoryBlockPlacements: list[worldTools.PlacementInstruction] = []
for pos in loop3D(self.box.size):
block = nbtTools.getBlockAt(self.structureFile.nbt, pos)
if block is None:
continue
if block.id not in INVENTORY_BLOCKS:
continue
inventoryDimensions: ivec2 = CONTAINER_BLOCK_TO_INVENTORY_SIZE[block.id]
newInventory = []
for inventorySlots in range(inventoryDimensions.x * inventoryDimensions.y):
itemType = rng.choice(list(self.inventoryTable.keys()))
itemRarity, itemMaxQuantity = self.inventoryTable.get(itemType)
if rng.random() > itemRarity:
continue
newItemDict = {
'x': rng.integers(inventoryDimensions.x),
'y': rng.integers(inventoryDimensions.y),
'material': itemType,
'amount': rng.integers(1, itemMaxQuantity + 1),
}
if itemType == 'written_book':
randomCodes = ''.join(rng.choice(list(string.hexdigits), size=10))
randomCodesSampleName = ''.join(rng.choice(list(string.hexdigits), size=8))
author = 'The members of the {} research team'.format(rng.choice([
'exogeology',
'exobiology',
'exobiotanica',
'exo-ecology',
'exo-seismology',
'exo-planetary',
'exo macro-biology'
'exo archaeology'
]))
bookText = \
'SAMPLE REPORT §4{}§r\n'.format(randomCodes) + \
'date: §dSOL{}/EPOC{}:T+{}{}§r\n'.format(
rng.integers(1, 9),
rng.integers(1, 11),
rng.integers(20, 28),
rng.integers(0, 61),
) + \
author + '§2 PART OF EXPEDITION {}§r\n'.format(
''.join(rng.choice(list(string.hexdigits), size=3))) + \
'SUBJECT: §a SAMPLE OF {}§r\n'.format(randomCodesSampleName) + \
'§rINTRODUCTION\n' + \
'§k' + ''.join(
rng.choice(list(string.hexdigits), size=rng.integers(100, 1000))) + ' §r\f' + \
'§rRESULTS and DISCUSSION\n' + \
'§k' + ''.join(
rng.choice(list(string.hexdigits), size=rng.integers(100, 1000))) + ' §r\f' + \
'§rCONCLUSION\n' + \
'§k' + ''.join(
rng.choice(list(string.hexdigits), size=rng.integers(100, 1000))) + '\f'
newItemDict['tag'] = bookData(
text=bookText,
title='$4 SAMPLE REPORT {}'.format(randomCodes),
author=author,
description=f'Subject: {randomCodesSampleName}',
)
newInventory.append(newItemDict)
block = nbtTools.setInventoryContents(block, newInventory)
transformedPosition = vectorTools.rotatePointAroundOrigin3D(
origin=self.boxInWorldSpace.middle,
point=pos + self.boxInWorldSpace.offset,
rotation=self.facing,
)
if block.states.get('facing'):
block.states['facing'] = rotateFacing(block.states.get('facing'), self.facing)
newInventoryBlockPlacements.append(worldTools.PlacementInstruction(
position=transformedPosition,
block=block,
))
return newInventoryBlockPlacements
def evaluateStructure(self) -> float:
cost = 1.0
cost += worldTools.calculateTreeCuttingCost(self.rectInWorldSpace) * 0.2
return cost
def doPreProcessingSteps(self, node: Node = None):
self.preProcessingSteps.extend(worldTools.getTreeCuttingInstructions(self.rectInWorldSpace))
for preProcessingStep in self.preProcessingSteps:
globals.editor.placeBlockGlobal(
position=preProcessingStep.position,
block=preProcessingStep.block,
)
def place(self):
# noinspection PyTypeChecker
placeStructure(
self.structureFile.file,
position=self.position, rotate=self.facing, mirror=None,
pivot=self.structureFile.centerPivot
)
print(f'Placed {self}')
def doPostProcessingSteps(self, node: Node = None):
for connector in node.connectorSlots:
if connector.transitionStructure is None:
continue
# noinspection PyTypeChecker
placeStructure(
connector.transitionStructure.file,
position=self.position, rotate=(connector.facing + self.facing) % 4, mirror=None,
pivot=connector.transitionStructure.centerPivot,
)
postProcessingSteps: list[worldTools.PlacementInstruction] = []
postProcessingSteps.extend(self.setInventoryBlocks(node.rng))
for postProcessingStep in postProcessingSteps:
globals.editor.placeBlockGlobal(
position=postProcessingStep.position,
block=postProcessingStep.block,
)
def __eq__(self, other):
return hash(self) == hash(other)
def __hash__(self):
return hash((self.structureFile, self.position, self.facing))
def __repr__(self):
return f'{self.structureFile}; pos: {self.position.x},{self.position.y},{self.position.z}; f: {self.facing}'