-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
531 lines (418 loc) · 19.6 KB
/
test.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import direct.directbase.DirectStart
from panda3d.core import *
from pandac.PandaModules import *
from direct.gui.OnscreenText import OnscreenText
from direct.actor.Actor import Actor
from direct.showbase.DirectObject import * #DirectObject, globalClock
from joystick import *
import sys
import pickle
# import random, sys, os, math
#from pandac.PandaModules import Thread as PandaThread
import pyhnet.hnet as hnet
#import threading
import time
#from direct.stdpy import pandaThreads
#TODO fix networking handler
#TODO fix collision handler names (we have an event handler and a physics handler)
#TODO separate out controls code
def MyActor(*args, **kwargs):
x = ActorNode(*args, **kwargs)
x.setTag("actor", pickle.dumps(None))
return x
class PlayerIntent:
def __init__(self, playerId, fireOn = False, thrusters = Vec3(0.0, 0.0, 0.0), rotThrusters = Vec3(0.0, 0.0, 0.0)):
self.playerId = playerId
self.fireOn = fireOn
self.thrusters = Vec3(thrusters)
self.rotThrusters = Vec3(rotThrusters)
def __eq__(self, other):
return self.playerId == other.playerId and self.fireOn == other.fireOn and self.thrusters == other.thrusters and self.rotThrusters == other.rotThrusters
def __add__(self, other):
return PlayerIntent( self.playerId
, self.fireOn or other.fireOn
, Vec3(tuple([max((-1.0, min((1.0, x)))) for x in self.thrusters + other.thrusters]))
, Vec3(tuple([max((-1.0, min((1.0, x)))) for x in self.rotThrusters + other.rotThrusters]))
)
#these functions are helper functions
# for event handling
def setFireOn(self, a): self.fireOn = a
def setForwardThruster(self, x): self.thrusters[1] = -x
def setLeftThruster(self, x): self.thrusters[0] = x
def setUpThruster(self, x): self.thrusters[2] = x
def setHeadingThruster(self, x): self.rotThrusters[0] = x #aka Yaw
def setPitchThruster(self, x): self.rotThrusters[1] = x
def setRollThruster(self, x): self.rotThrusters[2] = x
def clampVector(v, maxLength):
if v.length() > maxLength:
v.normalize()
return v * maxLength
else:
return v
def neg(x):
return -x
def compose(f, g):
def fg(x):
return f(g(x))
return fg
#TODO we need some for of error handling here
class NetworkingHandler(hnet.HNetHandler):
def onInit(self):
self.incoming = hnet.Queue()
self.outgoing = hnet.Queue()
self.playerId = None
self.gameJoined = hnet.Event()
self.gameName = "MyGame"
self.obj = None
def runConnection(self):
reply = self.sendAndWait('Hello')
self.obj = reply.proxy()
self.playerId = self.obj.joinGame(self.gameName, "Job")
self.gameJoined.set()
self.done.wait()
def sendGamePacket(self, stuff, dstPlayerId = None):
self.obj.sendGamePacket(self.gameName, stuff, dstPlayerId)
def onRecv(self, packet):
self.incoming.put(packet.msg())
def onError(self, exc_type, value, traceback):
raise
#TODO do something more usefull/gracefull here
class StateSaver:
accessors = []
nested = {}
def __init__(self, obj = None):
self.state = {}
if obj != None:
self.save(obj)
def save(self, obj):
for name, C in self.nested.items():
self.state['#' + name] = C(getattr(obj, name)())
for name in self.accessors:
self.state[name] = getattr(obj, "get" + name)()
return self
def restore(self, obj):
for name, C in self.nested.items():
self.state['#' + name].restore(getattr(obj, name)())
for name in self.accessors:
getattr(obj, "set" + name)(self.state[name])
class PhysicsState(StateSaver):
accessors = [ 'Name'
, 'Mass'
, 'Active'
, 'LastPosition'
, 'Position'
, 'Velocity'
, 'TerminalVelocity'
, 'Oriented'
, 'Orientation'
, 'Rotation'
]
class ActorState(StateSaver):
accessors = [ 'ContactVector' ]
nested = {'getPhysicsObject' : PhysicsState }
class Player:
def __init__(self, playerId, world, name, pos):
self.playerId = playerId
self.laserCool = 0.0
self.name = name
self.populateScene(playerId, world, pos)
#TODO move this to the addPlayer function
def populateScene(self, playerId, world, pos):
nodePath = NodePath(PandaNode("player-node"))
nodePath.setTag("player", str(playerId))
nodePath.reparentTo(world.root)
actorNode = MyActor("player-actor")
actorNode.getPhysicsObject().setMass(2000.0) # two metric tons
#TODO handle this more neatly somehow
world.physicsMgr.attachPhysicalNode(actorNode)
actorNodePath = nodePath.attachNewNode(actorNode)
actorNodePath.setPos(pos)
modelNode = loader.loadModel("media/models/Fighter")
modelNode.reparentTo(actorNodePath)
modelNode.setScale(0.1)
modelNode.setHpr(180.0, 0.0, 0.0)
boundingSphere = CollisionNode('player-bounds')
boundingSphere.addSolid(CollisionSphere(0, 0, 0, 2))
collideNode = actorNodePath.attachNewNode(boundingSphere)
collideNode.setTag("collides", "player")
world.cHandler.addCollider(collideNode, actorNodePath)
world.cTrav.addCollider(collideNode, world.cHandler)
nodePath.setTag("hasPythonObj", "")
nodePath.setPythonTag("pythonObj", self)
return nodePath
#TODO add a filter for multiple intent packets for a single player
def processIntent(self, world, intent, actorNodePath):
if intent.fireOn:
if world.clock.getFrameCount() >= self.laserCool:
world.createLaserProjectile(self, actorNodePath)
self.laserCool = world.clock.getFrameCount() + world.time2frames(0.4)
currentVelocity = actorNodePath.node().getPhysicsObject().getVelocity()
desiredVelocity = Vec3(actorNodePath.getMat().xformVec(intent.thrusters)) * 80.0
deltaVel = desiredVelocity - currentVelocity
#TODO, there are a lot of arbitrary numbers here,
# we should group them mostly in one place and label their units
maxAccel = 10.0
thrust = clampVector(deltaVel * 0.1, maxAccel)
actorNodePath.node().getPhysicsObject().addImpulse(thrust)
h, p, r = intent.rotThrusters * 1.5
actorNodePath.node().getPhysicsObject().addLocalTorque(LRotationf(h, p, r)*0.1)
handler = None
class World(DirectObject):
def handleLaserHitLevel(self, entry):
#TODO check to see if this is high enough
laser = entry.getFromNodePath()
laser.removeNode()
self.laserHitWall.play()
#print "hit level"
def handleLaserHitShip(self, entry):
laser = entry.getFromNodePath()
laser.removeNode()
self.laserHitPlayer.play()
#print "hit ship"
def time2frames(self, t):
return int(round(t/self.clock.getDt()))
#TODO remove these and the input events
def doSaves(self, a):
#asdf = ActorNode(PandaNode("test"))
if self.lastSave == None:
self.lastSave = self.save()
def doRestores(self, a):
if self.lastSave != None:
self.restore(self.lastSave)
self.lastSave = None
def save(self):
for actor in self.root.findAllMatches("**/=actor"):
actor.setTag("actor", pickle.dumps(ActorState(actor.node())))
for hasPythonObj in self.root.findAllMatches("**/=hasPythonObj"):
hasPythonObj.setTag("hasPythonObj", pickle.dumps(hasPythonObj.getPythonTag("pythonObj")))
return pickle.dumps(self.root)
def restore(self, data):
self.root.removeNode()
self.root = pickle.loads(data)
self.root.reparentTo(self.render)
#restore embedded python objects
for hasPythonObj in self.root.findAllMatches("**/=hasPythonObj"):
hasPythonObj.setPythonTag("pythonObj", pickle.loads(hasPythonObj.getTag("hasPythonObj")))
hasPythonObj.setTag("hasPythonObj", "")
#restore ActorNodes
for actor in self.root.findAllMatches("**/=actor"):
actorState = pickle.loads(actor.getTag("actor"))
newActor = MyActor(actor.getName())
newActor.replaceNode(actor.node())
actorState.restore(newActor)
self.checkDynamicObjects()
#this method makes sure various entities are in sync with their dynamic components
# it chesk that the camera is set correctly,
# the lights,
# colliders,
# etc..
# this is generally a safe call to make
def checkDynamicObjects(self):
#clear colliders
self.cHandler.clearColliders()
self.cTrav.clearColliders()
self.physicsMgr = PhysicsManager()
#base.enableParticles()
self.physicsMgr.attachLinearIntegrator(LinearEulerIntegrator())
self.physicsMgr.attachAngularIntegrator(AngularEulerIntegrator())
#self.physicsMgr.clearPhysicals()
#self.physicsMgr.clearAngularForces()
#self.physicsMgr.clearLinearForces()
#clear any old lights
for light in self.root.findAllMatches("**/*Light"):
self.root.clearLight(light)
#restore camera
playerShipActorNodePath = self.root.find("**/=player=%i/player-actor" % (self.playerId))
base.camera.reparentTo(playerShipActorNodePath)
base.camera.setPos(Vec3(0,20,5))
base.camera.lookAt(playerShipActorNodePath)
#restore lights
#TODO use a better search key here
for light in self.root.findAllMatches("**/*Light"):
self.root.setLight(light)
#restore collision traversers
for collider in self.root.findAllMatches("**/=collides"):
self.physicsMgr.attachPhysicalNode(collider.getParent().node())
if collider.getTag("collides") == "player":
self.cHandler.addCollider(collider, collider.getParent())
self.cTrav.addCollider(collider, self.cHandler)
elif collider.getTag("collides") == "projectile":
self.cTrav.addCollider(collider, self.pHandler)
def addPlayer(self, playerId, name):
Player(playerId, self, name, Vec3(0,-40,0))
def createLaserProjectile(self, player, playerActorNodePath):
self.laserSound.play()
nodePath = playerActorNodePath.attachNewNode(PandaNode("projectile-laser-node"))
actorNode = MyActor("projectile-laser-actor")
actorNode.getPhysicsObject().setVelocity(Vec3(0.0, -120.0, 0.0))
self.physicsMgr.attachPhysicalNode(actorNode)
actorNodePath = nodePath.attachNewNode(actorNode)
actorNodePath.setPos(0.0, -3.0, 0.0)
boundingObject = CollisionNode('projectile-laser-bounds')
boundingObject.addSolid(CollisionSphere(0.0, 0.0, 0.0, 0.1))
boundingObject.setIntoCollideMask(0)
collideNode = actorNodePath.attachNewNode(boundingObject)
collideNode.show()
collideNode.setTag("collides", "projectile")
#self.cHandler.addCollider(collideNode, actorNodePath)
#self.cTrav.addCollider(collideNode, self.cHandler)
self.cTrav.addCollider(collideNode, self.pHandler)
nodePath.wrtReparentTo(self.root)
def __init__(self, render):
global handler
self.render = render
self.root = render.attachNewNode(PandaNode("root"))
self.keyMap = {"left":0, "right":0, "forward":0, "cam-left":0, "cam-right":0}
self.axisData = Vec3(0.0,0.0,0.0)
base.win.setClearColor(Vec4(0,0,0,1))
self.multiplayer = len(sys.argv) > 1
self.frameQueue = []
self.frames = {}
self.lastSave = None
if self.multiplayer:
#TODO, this is kinda a lame setup, no error handling, etc...
self.networkHandler = NetworkingHandler(hnet.connectTCP(sys.argv[1], 6112))
self.networkHandler.run()
self.networkHandler.gameJoined.wait(1.0)
handler = self.networkHandler
self.playerId = self.networkHandler.playerId
else:
self.playerId = 0
if(self.playerId == 0):
self.running = True
else:
self.running = False
base.disableMouse()
self.physicsMgr = PhysicsManager()
#base.enableParticles()
self.physicsMgr.attachLinearIntegrator(LinearEulerIntegrator())
self.physicsMgr.attachAngularIntegrator(AngularEulerIntegrator()) # add angular integrator to the physics manager (for rotational physics)
self.environ = loader.loadModel("levels/minerva")
self.environ.reparentTo(self.root)
self.cHandler = PhysicsCollisionHandler()
self.cTrav = CollisionTraverser()
self.cTrav.setRespectPrevTransform( True )
#self.cTrav.showCollisions(render)
self.pHandler = CollisionHandlerEvent()
self.pHandler.addInPattern('%fn-into-%in')
self.accept('projectile-laser-bounds-into-player-bounds', self.handleLaserHitShip)
self.accept('projectile-laser-bounds-into-level', self.handleLaserHitLevel)
self.laserSound = base.loader.loadSfx("media/sound/laser02.wav")
self.laserHitWall = base.loader.loadSfx("media/sound/explode1.wav")
self.laserHitPlayer = base.loader.loadSfx("media/sound/shit01.wav")
if not self.multiplayer:
self.addPlayer(self.playerId, "player1")
self.joystickIntent = PlayerIntent(self.playerId)
self.keyboardIntent = PlayerIntent(self.playerId)
self.joy = JoystickHandler()
def addControlKey(keyName, f):
self.accept(keyName, f, [True])
self.accept(keyName + "-up", f, [False])
def addKeyAxis(keyA, keyB, f):
def magic(a, b, x = [0.0, 0.0]): # this function keeps the state of x around
if a != None: x[0] = a
if b != None: x[1] = b
f(sum(x))
self.accept(keyA, magic, [1.0, None])
self.accept(keyA + "-up", magic, [0.0, None])
self.accept(keyB, magic, [None, -1.0])
self.accept(keyB + "-up", magic, [None, 0.0])
def addHatAxis(hatName, setLeftRight, setUpDown, f = lambda x: x, g = lambda x: x):
def h((leftRight, upDown)):
setLeftRight(f(leftRight))
setUpDown(g(upDown))
self.accept(hatName, h)
def addAxis(joyAxisName, f, g = lambda x: x):
self.accept(joyAxisName, compose(f, g))
self.accept("escape", sys.exit)
addKeyAxis("a", "d", self.keyboardIntent.setLeftThruster)
addKeyAxis("w", "s", self.keyboardIntent.setUpThruster)
addKeyAxis("t", "enter", self.keyboardIntent.setForwardThruster)
addKeyAxis("4", "6", self.keyboardIntent.setHeadingThruster)
addKeyAxis("8", "5", self.keyboardIntent.setPitchThruster)
addKeyAxis("7", "9", self.keyboardIntent.setRollThruster)
addControlKey("space", self.keyboardIntent.setFireOn)
addControlKey("j", self.doSaves)
addControlKey("k", self.doRestores)
# Hard coded joystick controls (Ron's config)
#addKeyAxis("joystick0-button7", "joystick0-button6", self.keyboardIntent.setForwardThruster)
#addKeyAxis("joystick0-button2", "joystick0-button1", self.keyboardIntent.setHeadingThruster)
#addKeyAxis("joystick0-button3", "joystick0-button0", self.keyboardIntent.setPitchThruster)
addAxis("joystick0-axis0", self.joystickIntent.setHeadingThruster, neg)
addAxis("joystick0-axis1", self.joystickIntent.setPitchThruster, neg)
addAxis("joystick0-axis2", self.joystickIntent.setForwardThruster, neg)
addHatAxis("joystick0-hat0", self.joystickIntent.setLeftThruster, self.joystickIntent.setUpThruster, neg)
addControlKey("joystick0-button0", self.joystickIntent.setFireOn)
taskMgr.add(self.doPhys, "physFrameTask", sort = 30)
if self.multiplayer:
taskMgr.add(self.doNetworking, "networking")
print taskMgr
# Create some lighting
ambientLight = AmbientLight("ambientLight")
ambientLight.setColor(Vec4(.3, .3, .3, 1))
directionalLight = DirectionalLight("directionalLight")
directionalLight.setDirection(Vec3(-5, -5, -5))
directionalLight.setColor(Vec4(1, 1, 1, 1))
directionalLight.setSpecularColor(Vec4(1, 1, 1, 1))
#self.root.setLight(self.root.attachNewNode(ambientLight))
#self.root.setLight(self.root.attachNewNode(directionalLight))
#base.cTrav = self.cTrav
self.clock = ClockObject()
self.clock.setMode(self.clock.MNonRealTime)
self.clock.setFrameRate(60.0)
base.setFrameRateMeter(True)
#TODO, I think the only usefull thing this is doing here is setting the camera,
# perhaps pull that out do it in it's own function
self.checkDynamicObjects()
def applyIntent(self, intent):
playerActorNodePath = self.root.find("**/=player=%i/player-actor" % (intent.playerId))
playerActorNodePath.getParent().getPythonTag("pythonObj").processIntent(self, intent, playerActorNodePath)
def doNetworking(self, task):
while not self.networkHandler.incoming.empty():
playerId, frame, (tag, data) = self.networkHandler.incoming.get_nowait()
if tag == "PlayerUpdate":
intent = pickle.loads(data)
intent.playerId = playerId # just to make sure the other client isn't lieing to us
if frame not in self.frames:
self.frames[frame] = []
self.frames[frame].append(lambda:self.applyIntent(intent))
elif tag == "PlayerJoined":
self.addPlayer(playerId, data)
#TODO Switch the host to another player if host leaves
if (self.playerId == 0):
gameState = self.save()
self.networkHandler.sendGamePacket(("LevelSync", gameState), playerId)
elif tag == "LevelSync":
self.restore(data)
self.running = True
elif tag == "NewFrame":
lastFrame = frame -1
if lastFrame in self.frames:
self.frameQueue.append(self.frames[lastFrame])
del self.frames[lastFrame]
else:
self.frameQueue.append([])
return task.cont
def doPhys(self, task):
currentIntent = self.keyboardIntent + self.joystickIntent
if self.multiplayer:
self.networkHandler.sendGamePacket(("PlayerUpdate", pickle.dumps(currentIntent)))
else:
self.frameQueue.append([lambda:self.applyIntent(currentIntent)])
#TODO add something here so that we leave an extra frame of delay that we can use
# when a frame is delayed from the server (actually the way I'm doing the frames like this is kinda bad)
while len(self.frameQueue) > 0 and self.running:
self.processFrame(self.frameQueue.pop(0))
return task.cont
def processFrame(self, frameEvents):
for event in frameEvents:
event()
self.physicsMgr.doPhysics(self.clock.getDt())
self.cTrav.traverse(self.render)
self.clock.tick()
try:
w = World(render)
run()
finally:
if handler:
handler.close()