rendered paste body#!/usr/bin/pythonMAXTRANS = 5000SV_INITS2C = 0SV_MAPCHANGE = 11SV_ITEMLIST = 21DMF = 16DVELF = 1 #import weakrefimport enetimport atexitimport sdbimport worldimport handleCommandimport timefrom threading import Threadclass monsterThread(Thread): __stop = False def stop(self): self.__stop = True def run(self): while self.__stop == False: for m in world.monsters: m.think() time.sleep(.5)monster_runner = monsterThread()monster_runner.setDaemon(True)def spill(obj): for a in dir(obj): print a, repr(getattr(obj, a))connections = {}# send packet to everybody, usually you want msend insteaddef send(packet): for a in connections: enet.enet_peer_send(connections[a].peer, 0, packet);# send packet to everyone on same map as entdef msend(ent, packet): for a in connections: if connections[a].map == ent.map: enet.enet_peer_send(connections[a].peer, 0, packet); if(packet.referenceCount==0): enet.enet_packet_destroy(packet);# sent packet to everyone def osend(player, packet): for a in connections: if connections[a] != player and connections[a].map == player.map: enet.enet_peer_send(connections[a].peer, 0, packet); if(packet.referenceCount==0): enet.enet_packet_destroy(packet);# send packet to a specific playerdef asend(player, packet): enet.enet_peer_send(player.peer, 0, packet); if(packet.referenceCount==0): enet.enet_packet_destroy(packet);def handleInput(player, packet): pw = packetWrangler.packetWrangler() #for a in enet.IntVector(enet.parsepacket(packet)): print "VECT: %d" % a #print "----- FEEDING ---" pw.feed(enet.IntVector(enet.parsepacket(packet))) while pw.hasData(): # SV_POS, position & movement info type = pw.getInt() if type == 2: #print "POS packet pw.getInt() # client num #print "xyz yaw" player.o[0] = pw.getUint() / DMF # x player.o[1] = pw.getUint() / DMF # y player.o[2] = pw.getUint() / DMF # z player.yaw = pw.getUint() # yaw player.pitch = pw.getInt() # pitch player.roll = pw.getInt() # roll player.v[0] = pw.getInt()/DVELF # velocity x player.v[1] = pw.getInt()/DVELF # velocity y player.v[2] = pw.getInt()/DVELF # velocity z #print "State Info" tmp = pw.getInt() if tmp & 0x10: # physstate pack player.g[0] = pw.getInt()/DVELF # gravity x player.g[1] = pw.getInt()/DVELF # gravity y player.g[2] = pw.getInt()/DVELF # gravity z pw.getInt() # state pack player.onPositionUpdate() player.sendUpdate() elif type == 1: # player init info player.name = pw.getStr() pw.getStr() player.lifesequence = pw.getInt() pw.getInt() pw.getInt() osend(player, player.infoPacket()) elif type == 3: # text from player, command or chat text = pw.getStr().strip() if text[0] == "@": handleCommand.handleCommand(player, text[1:len(text)]) else: osend(player, enet.mint_messagePacket(player.name + ": " + text)); elif type == 7: world.ents[pw.getInt()].onMeleeAttack(player) pw.getInt() # damage pw.getInt() # lifesequence of victim pw.getInt() #x pw.getInt() #y pw.getInt() #z elif type == 10: # map name pw.getStr() name = pw.getInt() elif type == 14: # ping pw.getInt() elif type == 21: # server message, needs to be here to avoid crashing on forged packet becasue its a fixed length message pw.getStr() elif type == 22: #item list while pw.getInt() != -1: continue elif type == 29 or type == 31: # copy from edit mode x1 = pw.getInt() # x1 y1 = pw.getInt() # y1 z1 = pw.getInt() # z1 x2 = pw.getInt() # x2 y2 = pw.getInt() # y2 z2 = pw.getInt() # z2 gridsize = pw.getInt() #grid pw.getInt() #grid orientation pw.getInt() #cx pw.getInt() #cxs pw.getInt() #cy pw.getInt() #cys pw.getInt() #corner if(type == 31): pw.getInt() # tex pw.getInt() # newtex pw.getInt() # orient # shove selection coordinates into a structure so we can organize them properly tselect = [x1,y1,z1,x1 + (x2 * gridsize),y1 + (y2 * gridsize),z1 + (z2 * gridsize)] for i in [0,1,2]: if tselect[i] < tselect[i+3]: player.selectionA[i] = tselect[i] player.selectionB[i] = tselect[i+3] else: player.selectionB[i] = tselect[i+3] player.selectionA[i] = tselect[i] player.msg("Area selected.") elif type == 38: # ctf bases while pw.getInt() != -1: getInt(p) getInt(p) elif type == 39: # ctf base info pw.getInt() pw.getStr() pw.getStr() pw.getInt() elif type == 40: # team score pw.getStr() pw.getInt() else: print "Unknown packet type %d." % type x = enet.msgsize(type) - 1 if(x > 0 & pw.hasData()): if(type < 1): print "We should never get a packet type %d!" % type raise SystemExit for a in range(x): pw.getInt() #print "Recovered, skipped %d integer" % x else: print "Non-fixed packet size, thus non-recoverable." #while pw.hasData(): print pw.getUint() raise SystemExitdef server_shutdown(): print "shut down started" enet.enet_deinitialize() monster_runner.stop() monster_runner.join() print "shutdown complete"# last things first!atexit.register(server_shutdown)#load rooms from databaseworld.loadRooms()world.loadMonsters()monster_runner.start()if(enet.enet_initialize()<0): print("Unable to initialise network module");serverAddress = enet.ENetAddress()serverAddress.host = 0 #ENET_HOST_ANYserverAddress.port = 28785 #SAUERBRATEN_SERVER_PORTserver = enet.enet_host_create(serverAddress, 32, 0, 0)event = enet.ENetEvent()# run the loop forever, atexit will take care of calling shutdown handlerwhile(1): enet.enet_host_service(server, event, 1000) if event.type == 1: new_player = world.player() new_player.peer = event.peer connections[event.peer.incomingPeerID] = new_player packet = enet.mint_welcomePacket(new_player.entnum) asend(new_player, packet); new_player.setMap(new_player.map) elif event.type == 2: print "Disconnect" leaving_player = connections[event.peer.incomingPeerID] packet = enet.mint_removePacket(leaving_player.entnum) osend(leaving_player, packet) del world.ents[connections[event.peer.incomingPeerID].entnum] del connections[event.peer.incomingPeerID] enet.enet_peer_disconnect(event.peer, 0); elif event.type == 3: handleInput(world.ents[connections[event.peer.incomingPeerID].entnum], event.packet) time.sleep(.01)