forked from wezu/Avolition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
naive_editor.py
357 lines (320 loc) · 14.4 KB
/
naive_editor.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Nameless A.I. Version E editor
from panda3d.core import *
from direct.showbase.DirectObject import DirectObject
import direct.directbase.DirectStart
from direct.gui.DirectGui import *
import json
from dijkstra2 import Graph
from vis_ninth import VisibilityPolygon
from direct.gui.OnscreenText import OnscreenText
import math
myFile="level4.egg"
# Function to put instructions on the screen.
def addInstructions(pos, msg):
return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
pos=(-1.3, pos), align=TextNode.ALeft, scale = .05)
class AI_level_editor(DirectObject):
def __init__(self):
addInstructions(0.80,"WSAD keys to move the camera")
addInstructions(0.75,"E R keys to rotate the camera")
addInstructions(0.70,"Z X keys to zoom the camera")
addInstructions(0.65,"ENTER saves the map")
addInstructions(0.60,"Mouse-1 to draw a wall or spawnpoint")
addInstructions(0.55,"Space to switch between walls and spawnpoints")
addInstructions(0.50,"Tab to switch wall mode")
addInstructions(0.45,"Esc to remove last wall/spawnpoint")
#pointer
self.pointer = loader.loadModel("models/pointer")
self.pointer.reparentTo(render)
self.pointer.setLightOff()
self.pointer.setScale(0.3)
#self.pointer.setScale(0.4)
#level
self.map=loader.loadModel("models/"+myFile)
self.map.reparentTo(render)
#camera
base.disableMouse()
self.cameraNode = NodePath(PandaNode("cameraNode"))
self.cameraNode.reparentTo(render)
self.cameraNode.setPos(0, 0, 32)
base.camera.setPos(0,-32, 32)
base.camera.lookAt(0, 0, 0)
base.camera.wrtReparentTo(self.cameraNode)
self.keyMap = { "w" : False,
"s" : False,
"a" : False,
"d" : False,
"q" : False,
"e" : False,
"z" : False,
"x" : False
}
for key in self.keyMap.keys():
self.accept(key, self.keyMap.__setitem__, [key, True])
self.accept(key+"-up", self.keyMap.__setitem__, [key, False])
self.camera_momentum=[1.0, 1.0, 1.0, 1.0]
#the plane will bu used to see where the mouse pointer is
self.plane = Plane(Vec3(0, 0, -0.1), Point3(0, 0, -0.1))
self.accept("mouse1", self.add_wall)
self.accept("escape", self.remove_wall)
self.accept("tab", self.waypoint_switch)
self.accept("space", self.edit_mode_switch)
self.accept("enter", self.generate_map, [myFile])
self.walls=[]
self.wall=loader.loadModel("models/wall_w_collision2")
self.wall.setScale(0.3)
self.wall.flattenLight()
self.new_wall=True
self.edit_mode="walls"
self.spawnpoints=[]
self.traverser = CollisionTraverser('myTraverser')
#self.traverser.showCollisions(render)
self.queue = CollisionHandlerQueue()
self.waypoints_connections=[]
self.waypoints=[]
self.waypoint= NodePath(PandaNode("WPnode"))
self.waypoint_model=loader.loadModel("models/waypoint")
self.waypoint_model.setScale(0.3)
self.waypoint_model.flattenLight()
coll=self.waypoint_model.attachNewNode(CollisionNode('collRayNode'))
coll.node().addSolid(CollisionRay(0, 0, 25, 0,0,-180))
coll.node().setIntoCollideMask(BitMask32.allOff())
#coll.show()
#self.traverser.addCollider(coll, self.queue)
self.waypoint_model.reparentTo(self.waypoint)
self.waypoint_model.setPos(-1, -1,0)
self.outside=True
#gui
self.widget = DirectFrame(frameSize=(-32, 0, 0, 32),
frameTexture='images/outside.png',
state=DGG.NORMAL,
parent=pixel2d)
self.widget.setPos(32,0,-32)
self.widget.setTransparency(TransparencyAttrib.MAlpha)
self.widget.bind(DGG.B1PRESS, self.waypoint_switch)
self.widget2 = DirectFrame(frameSize=(-32, 0, 0, 32),
text="0.0 / 0.0",
text_scale=20,
text_fg=(1,1,1,1),
textMayChange=1,
frameColor=(0, 0, 0, 0),
parent=pixel2d)
self.widget2.setPos(100,0,-22)
self.widget2.setTransparency(TransparencyAttrib.MAlpha)
self.polygons=[]
self.current_polygon=[]
self.undo=True
self.timer=0
taskMgr.add(self.__getMousePos, "mousePosTask")
taskMgr.add(self.camera_control, "cameraTask")
taskMgr.add(self.orient_wall, "orient_wall")
def edit_mode_switch(self):
if self.edit_mode=="walls":
self.edit_mode="spawnpoints"
else:
self.edit_mode="walls"
print("Editing ", self.edit_mode)
def build_polyset(self, points, position, tag=None, mask=1, z=0):
polyset=render.attachNewNode(CollisionNode('collisionPolyset'))
start_point=Point3(position[0], position[1], z)
final_points=[]
for point in points:
final_points.append(Point3(point[0], point[1], z))
#print point
max=len(final_points)-1
for index in range(max):
polyset.node().addSolid(CollisionPolygon(start_point, final_points[index],final_points[index+1]))
polyset.node().addSolid(CollisionPolygon(start_point, final_points[-1],final_points[0]))
if tag!=None: #tag could be 0, and we want a tag then
polyset.setTag("index", str(tag))
polyset.node().setIntoCollideMask(BitMask32.bit(mask))
return polyset
def run_collisions(self):
#print "collisions..."
self.traverser.traverse(render)
for entry in self.queue.getEntries():
i_from=entry.getFromNodePath().getNetTag("index")
i_into=entry.getIntoNodePath().getNetTag("index")
if i_into!=i_from:
self.waypoints_connections[int(i_from)].add(i_into)
#print "connection: ", i_from, i_into
def generate_map(self, name="temp"):
VP=VisibilityPolygon()
segments = VP.convertToSegments(self.polygons)
print("segments: ", segments)
export_node=NodePath(PandaNode("map"))
waypoints=[]
for wp in render.findAllMatches("**/collRayNode*"):
position = [wp.getX(render), wp.getY(render)]
print("pos:",position)
visibility = VP.compute(position, segments)
#print "visibility: ", visibility
tag=wp.getNetTag("index")
collision_poly=self.build_polyset(visibility, position, tag)
collision_poly.reparentTo(export_node)
self.traverser.addCollider(wp, self.queue)
waypoints.append(export_node.attachNewNode(PandaNode("WP"+tag)))
waypoints[-1].setPos(wp.getPos(render))
#collision_poly.show()
#export_node.writeBamFile('map2.bam')
export_node.reparentTo(render)
self.run_collisions()
#we can build the graph data only after collisons
graph_data={}
for i in range(len(waypoints)):
graph_data[str(i)]={}
for item in self.waypoints_connections[i]:
distance=waypoints[i].getDistance(waypoints[int(item)])
#print distance
graph_data[str(i)][item]=round(distance, 2)
graph = Graph(graph_data)
#we can build the paths only when we have the graph data
#so we run the loop AGAIN :|
for wp_index in range(len(waypoints)):
path_list=[]
for i in range(len(waypoints)):
path_list.append(round(graph.path_length(str(wp_index), str(i)), 2))
waypoints[wp_index].setTag("path", json.dumps(path_list))
#print "path_list", wp_index, path_list
#print graph_data
#adding wals
temp_wall=NodePath(PandaNode("walls"))
for wall in render.findAllMatches("**/collision"):
wall.wrtReparentTo(temp_wall)
wall.node().setIntoCollideMask(BitMask32.bit(2))
temp_wall.flattenStrong()
temp_wall.wrtReparentTo(export_node)
self.map.wrtReparentTo(export_node)
#spawnpoints
spawn=export_node.attachNewNode(PandaNode("spawnpoint_root"))
for spawnpoint in self.spawnpoints:
temp=spawn.attachNewNode(PandaNode("spawnpoint"))
temp.setPos(spawnpoint.getPos(render))
#write
export_node.writeBamFile('models/'+name+'.bam')
print("DONE")
def close_polygon(self, point):
x=point.getX()
y=point.getY()
if [x,y] in self.current_polygon:
self.polygons.append(self.current_polygon)
self.current_polygon=[]
self.undo=False
print("new polygon added")
print(self.polygons)
else:
self.add_wall()
def add_point(self, point):
x=point.getX()
y=point.getY()
self.current_polygon.append([x,y])
self.undo=True
#print self.polygons
#print self.current_polygon
def waypoint_switch(self, event=None):
if self.outside:
self.waypoint_model.setPos(-1,1,0)
self.outside=False
self.widget['frameTexture']='images/inside.png'
else:
self.waypoint_model.setPos(-1,-1,0)
self.outside=True
self.widget['frameTexture']='images/outside.png'
def remove_wall(self):
if self.edit_mode=="walls":
if self.walls and self.undo:
self.walls.pop().removeNode()
self.waypoints.pop().removeNode()
self.current_polygon.pop()
self.new_wall=True
else:
if self.spawnpoints:
self.spawnpoints.pop().removeNode()
def add_wall(self):
if self.edit_mode=="walls":
if self.new_wall:
self.walls.append(self.wall.copyTo(render))
self.walls[-1].setPos(render, self.pointer.getPos())
self.new_wall=False
self.waypoints.append(self.waypoint.copyTo(render))
self.waypoints[-1].setTag("index",str(len(self.waypoints)-1))
self.waypoints[-1].setPos(render, self.pointer.getPos())
self.add_point(self.pointer.getPos())
self.waypoints_connections.append(set())
#self.waypoints[-1].setPos(self.waypoints[-1],-1,1,0)
else:
self.new_wall=True
self.walls[-1].flattenLight()
#self.add_point(self.pointer.getPos())
self.close_polygon(self.pointer.getPos())
else:
self.spawnpoints.append(self.pointer.copyTo(render))
print(self.spawnpoints)
def orient_wall(self, task):
if not self.new_wall:
self.walls[-1].lookAt(self.pointer)
self.waypoints[-1].lookAt(self.pointer)
length=self.walls[-1].getDistance(self.pointer)
if length!=0:
self.walls[-1].setScale(1, length, 1)
return task.cont
def camera_control(self, task):
dt = globalClock.getDt()
for i in (0,1,2,3):
if self.camera_momentum[i]>10.0:
self.camera_momentum[i]=10.0
#move X
if self.keyMap["d"]:
self.cameraNode.setX(self.cameraNode, 25*dt* self.camera_momentum[0])
self.camera_momentum[0]+=dt*3
elif self.keyMap["a"]:
self.cameraNode.setX(self.cameraNode, -25*dt* self.camera_momentum[0])
self.camera_momentum[0]+=dt*3
else:
self.camera_momentum[0]=0
#move Y
if self.keyMap["w"]:
self.cameraNode.setY(self.cameraNode, 25*dt* self.camera_momentum[1])
self.camera_momentum[1]+=dt*3
elif self.keyMap["s"]:
self.cameraNode.setY(self.cameraNode, -25*dt* self.camera_momentum[1])
self.camera_momentum[1]+=dt*3
else:
self.camera_momentum[1]=0
#zoom
if self.keyMap["x"]:
base.camera.setY(base.camera, -60*dt* self.camera_momentum[2])
self.camera_momentum[2]+=dt*3
elif self.keyMap["z"]:
base.camera.setY(base.camera, 60*dt* self.camera_momentum[2])
self.camera_momentum[2]+=dt*3
else:
self.camera_momentum[2]=0
#rotate
if self.keyMap["q"]:
self.cameraNode.setH(self.cameraNode, -60*dt* self.camera_momentum[3])
self.camera_momentum[3]+=dt*3
elif self.keyMap["e"]:
self.cameraNode.setH(self.cameraNode, 60*dt* self.camera_momentum[3])
self.camera_momentum[3]+=dt*3
else:
self.camera_momentum[3]=0
return task.cont
def __getMousePos(self, task):
if base.mouseWatcherNode.hasMouse():
mpos = base.mouseWatcherNode.getMouse()
pos3d = Point3()
nearPoint = Point3()
farPoint = Point3()
base.camLens.extrude(mpos, nearPoint, farPoint)
if self.plane.intersectsLine(pos3d,
render.getRelativePoint(camera, nearPoint),
render.getRelativePoint(camera, farPoint)):
self.pointer.setPos(render, (math.ceil(pos3d.getX()*2)/2, math.ceil(pos3d.getY()*2)/2, 0))
self.timer+=1
if self.timer >=80:
self.widget2['text']="x={0} y={1}".format(round(self.pointer.getX(),1), round(self.pointer.getY(),1) )
self.timer=0
return task.again
w = AI_level_editor()
run()