-
Notifications
You must be signed in to change notification settings - Fork 0
/
citygen.py
154 lines (103 loc) · 6.37 KB
/
citygen.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
import bge, json
from bge.logic import globalDict
from bge.render import setMipmapping
from random import choice, random, seed
from pprint import pprint
from ast import literal_eval as litev
# Initialize globalDict
if not 'map' in globalDict.keys():
globalDict['map'] = {}
def gen_city(cont):
""" Generates a random city, using a pre made set of rules. """
own = cont.owner
scene = own.scene
# Sensors
sensor = cont.sensors[0].positive
# Properties
max_x = own['max_x'] * 100
max_y = own['max_y'] * 100
streets = ('DL', 'DLR', 'DR', 'LR', 'UD', 'UDL', 'UDLR', 'UDR', 'UL', 'ULR', 'UR', 'N')
#### INITIALIZE ####
if sensor:
setMipmapping(False)
scene.active_camera.worldPosition = (max_x/2, -max_y/2, 200)
if own['seed']:
seed(own['seed'])
### Generates the given number of streets ###
for x in range(0, max_x, 100):
### Add streets at Y axis of current X street ###
for y in range(0, max_y, 100):
cur_street = {'position' : (x, -y, 0), 'type' : 'UD'}
if cur_street['position'][1] == 0:
if cur_street['position'][0] == 0:
cur_street['type'] = choice([i for i in streets if 'D' in i or 'R' in i and not 'N' in i])
own['last_added'] = str(cur_street)
cur_street['position'] = (x, 0, 0)
own['current_top'] = str(cur_street)
elif cur_street['position'][0] > 0:
cur_left = globalDict['map'][str((x-100, 0, 0))]
possible_streets = []
if 'R' in cur_left['type']:
possible_streets = [i for i in streets if 'L' in i]
elif not 'R' in cur_left['type']:
possible_streets = [i for i in streets if not 'L' in i]
cur_street['position'] = (x, 0, 0)
cur_street['type'] = choice(possible_streets)
own['current_top'] = str(cur_street)
own['current_left'] = str(cur_left)
own['last_added'] = str(cur_street)
elif cur_street['position'][1] < 0:
if cur_street['position'][0] == 0:
possible_streets = []
last_added = litev(own['last_added'])
if 'D' in last_added['type']:
possible_streets = [i for i in streets if 'U' in i and not 'N' in i]
if not 'D' in last_added['type']:
possible_streets = [i for i in streets if not 'U' in i]
cur_street['type'] = choice(possible_streets)
own['last_added'] = str(cur_street)
if cur_street['position'][0] > 0:
cur_left = globalDict['map'][str((x-100, -y, 0))]
possible_streets = []
last_added = litev(own['last_added'])
if 'D' in last_added['type'] and not 'R' in cur_left['type']:
possible_streets = [i for i in streets if 'U' in i and not 'L' in i and not 'N' in i]
if not 'D' in last_added['type'] and 'R' in cur_left['type']:
possible_streets = [i for i in streets if not 'U' in i and 'L' in i and not 'N' in i]
if 'D' in last_added['type'] and 'R' in cur_left['type']:
possible_streets = [i for i in streets if 'U' in i and 'L' in i and not 'N' in i]
if not 'D' in last_added['type'] and not 'R' in cur_left['type']:
possible_streets = [i for i in streets if not 'U' in i and not 'L' in i]
cur_street['type'] = choice(possible_streets)
own['last_added'] = str(cur_street)
else:
cur_street['type'] = 'N'
### Add current street to globalDict and continue iteration
globalDict['map'][str(cur_street['position'])] = cur_street
### Add streets from globalDict to scene ###
for obj in globalDict['map'].values():
added = scene.addObject(obj['type'] + '_group')
for i in added.groupMembers:
i.setParent(added)
added.worldPosition = obj['position']
pass
def spawn_building(cont):
""" Spawn buildings at specific spots. """
own = cont.owner
scene = own.scene
# Sensors
sensor = cont.sensors[0].positive
# Properties
buildings = ('building_01', 'building_02', 'building_03', 'building_04', 'building_05', 'house_01', 'house_02', 'house_03', 'house_04', 'house_05', 'house_06', 'house_07', 'house_08', 'house_09', 'house_10', 'house_11', 'house_12', 'house_13')
fences = ('fence_01', 'fence_02', 'fence_03', 'fence_04')
# Objects
street = own.groupObject.groupMembers[own.groupObject.name.replace('_group', '')]
#### INITIALIZE ####
if sensor:
# Add random building at spawner position
building = scene.addObject(choice(buildings), own)
fence = scene.addObject(choice(fences), own)
# Set building to random color
building.color[0] = random()
# Warning message
print('Added', building.name, 'at', tuple(building.worldPosition), 'with hue', building.color[0].__round__(2))