Poodletooth-iLand/toontown/cogdominium/DistCogdoMazeGameAI.py

240 lines
8.6 KiB
Python
Raw Normal View History

2015-03-03 16:10:12 -06:00
from direct.directnotify import DirectNotifyGlobal
from toontown.cogdominium.DistCogdoGameAI import DistCogdoGameAI
2015-03-06 20:46:05 -06:00
import CogdoMazeGameGlobals
from direct.distributed.ClockDelta import *
from direct.task import Timer
from toontown.battle import BattleBase
from toontown.building.ElevatorConstants import *
2015-03-03 16:10:12 -06:00
2015-04-27 18:51:51 -05:00
ALL_ABOARD_LAG = 3.7
2015-03-03 16:10:12 -06:00
2015-03-06 20:46:05 -06:00
BASE_TOON_UP = 10
JOKE_TOON_UP = 5
2015-03-03 16:10:12 -06:00
2015-03-06 20:46:05 -06:00
class DistCogdoMazeGameAI(DistCogdoGameAI):
notify = DirectNotifyGlobal.directNotify.newCategory("DistCogdoMazeGameAI")
2015-04-28 02:07:50 -05:00
delayIntro = BattleBase.ELEVATOR_T + ElevatorData[ELEVATOR_FIELD]['openTime']
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def __init__(self, air):
DistCogdoGameAI.__init__(self, air)
self.numSuits = (0,0,0)
self.timer = Timer.Timer()
self.doorRevealed = False
self.toonsInDoor = []
self.bosses = {}
self.fastMinions = {}
self.slowMinions = {}
self.suitTypes = [self.bosses, self.fastMinions, self.slowMinions]
self.numJokes = {}
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def announceGenerate(self):
DistCogdoGameAI.announceGenerate(self)
self.setupSuitsAI()
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def setupSuitsAI(self):
bossHp = CogdoMazeGameGlobals.SuitData[0]['hp']
fastMiniHp = CogdoMazeGameGlobals.SuitData[1]['hp']
slowMiniHp = CogdoMazeGameGlobals.SuitData[2]['hp']
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
serialNum = 0
2015-07-05 20:02:35 -05:00
for i in xrange(self.numSuits[0]):
2015-03-06 20:46:05 -06:00
self.bosses[serialNum] = bossHp
serialNum += 1
2015-07-05 20:02:35 -05:00
for i in xrange(self.numSuits[1]):
2015-03-06 20:46:05 -06:00
self.fastMinions[serialNum] = fastMiniHp
serialNum += 1
2015-07-05 20:02:35 -05:00
for i in xrange(self.numSuits[2]):
2015-03-06 20:46:05 -06:00
self.slowMinions[serialNum] = slowMiniHp
serialNum += 1
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def setNumSuits(self, num):
self.numSuits = num
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def getNumSuits(self):
return self.numSuits
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def requestUseGag(self, x, y, h, timestamp):
avId = self.air.getAvatarIdFromSender()
self.sendUpdate('toonUsedGag', [avId, x, y, h, globalClockDelta.getRealNetworkTime()])
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def requestSuitHitByGag(self, suitType, suitNum):
hitAI = self.hitSuitAI(suitType, suitNum)
if not hitAI:
self.notify.warning('Cannot hit suit!')
return
avId = self.air.getAvatarIdFromSender()
self.sendUpdate('suitHitByGag', [avId, suitType, suitNum])
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def requestHitBySuit(self, suitType, suitNum, nettime):
avId = self.air.getAvatarIdFromSender()
av = self.air.doId2do.get(avId)
if av:
lostHp = CogdoMazeGameGlobals.SuitData[suitType]['toonDamage'] * self.getDifficulty() * 10
av.takeDamage(lostHp)
networkTime = globalClockDelta.getRealNetworkTime()
self.sendUpdate('toonHitBySuit', [avId, suitType, suitNum, networkTime])
if av.getHp() < 1:
self.toonWentSad(avId)
2015-04-27 18:51:51 -05:00
2015-03-03 16:10:12 -06:00
def requestHitByDrop(self):
2015-03-06 20:46:05 -06:00
avId = self.air.getAvatarIdFromSender()
av = self.air.doId2do.get(avId)
if av:
lostHp = CogdoMazeGameGlobals.DropDamage
av.takeDamage(lostHp)
self.sendUpdate('toonHitByDrop', [avId])
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def requestPickUp(self, pickupNum):
avId = self.air.getAvatarIdFromSender()
av = self.air.doId2do.get(avId)
if av:
now = globalClockDelta.getRealNetworkTime()
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
if avId in self.numJokes:
self.numJokes[avId] += 1
else:
self.numJokes[avId] = 1
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
self.sendUpdate('pickUp', [avId, pickupNum, now])
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def requestGag(self, coolerIndex):
avId = self.air.getAvatarIdFromSender()
self.sendUpdate('hasGag', [avId, globalClockDelta.getRealNetworkTime()])
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def hitSuitAI(self, suitType, suitNum):
cogKey = None
for cogNum in self.suitTypes[suitType].keys():
if cogNum == suitNum:
cogKey = cogNum
break
if cogKey == None:
return 0
cogHp = self.suitTypes[suitType][cogKey]
cogHp -= 1
self.suitTypes[suitType][cogKey] = cogHp
if cogHp <= 0:
del self.suitTypes[suitType][cogKey]
return 1
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def handleStart(self):
taskMgr.add(self.__checkGameDone, self.taskName('check-game-done'))
taskMgr.add(self.__checkPlayersTask, self.taskName('check-players-task'))
serverDelay = 1.0
self.timer.startCallback(CogdoMazeGameGlobals.SecondsUntilTimeout + serverDelay, self.__handleGameOver)
taskMgr.doMethodLater(serverDelay, self.clientCountdown, self.taskName('client_countdown'))
taskMgr.add(self.__timeWarningTask, self.taskName('time-warning-task'))
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def clientCountdown(self, task):
self.doAction(CogdoMazeGameGlobals.GameActions.Countdown, 0)
return task.done
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def __handleGameOver(self):
self.removeAll()
self.gameDone(failed=True)
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def __checkGameDone(self, task):
bossesLeft = self.bosses
if len(bossesLeft) == 0:
self.timer.stop()
self.doAction(CogdoMazeGameGlobals.GameActions.OpenDoor, 0)
self.__startTimeout()
return task.done
return task.again
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def __startTimeout(self):
self.timer.startCallback(CogdoMazeGameGlobals.SecondsUntilGameEnds, self.__handleTimeout)
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def __handleTimeout(self):
for toon in self.toons:
if not toon in self.toonsInDoor:
self.killToon(toon)
self.removeAll()
self.gameDone()
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def __timeWarningTask(self, task):
if self.timer.getT() <= CogdoMazeGameGlobals.SecondsForTimeAlert:
self.doAction(CogdoMazeGameGlobals.GameActions.TimeAlert, 0)
return task.done
return task.again
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def killToon(self, avId):
av = self.air.doId2do.get(avId)
if av:
if av.getHp() > 0:
av.takeDamage(av.getHp())
self.toonWentSad(avId)
self.__playerDisconnected(avId)
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def __checkPlayersTask(self, task):
for toonId in self.toons:
toon = self.air.doId2do.get(toonId)
if not toon:
self.__playerDisconnected(toonId)
return task.again
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def __playerDisconnected(self, avId):
self.sendUpdate('setToonDisconnect', [avId])
self.toons.pop(self.toons.index(avId))
if len(self.toons) == 0:
self.removeAll()
self.gameDone(failed=True)
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def doAction(self, action, data):
self.sendUpdate('doAction', [action, data, globalClockDelta.getRealNetworkTime()])
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def requestAction(self, action, data):
Globals = CogdoMazeGameGlobals
avId = self.air.getAvatarIdFromSender()
if action == Globals.GameActions.RevealDoor:
if not self.doorRevealed:
self.doAction(action, avId)
self.doorRevealed = True
else:
self.notify.warning('Toon tried to reveal door but it\'s already revealed! Ignoring.')
elif action == Globals.GameActions.EnterDoor:
if not avId in self.toonsInDoor:
self.doAction(action, avId)
self.toonsInDoor.append(avId)
self.toonUpToon(avId)
else:
self.notify.warning('Toon tried to enter into door but already entered! Ignoring.')
return
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
if len(self.toonsInDoor) >= len(self.toons):
self.__handleAllAboard()
else:
self.notify.warning('Client requested unknown action \'%s\'' %action)
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def __handleAllAboard(self):
if len(self.toonsInDoor) != len(self.toons):
self.notify.warning('__handleAllAboard expect all toons aboard!')
return
self.removeAll()
taskMgr.doMethodLater(ALL_ABOARD_LAG, lambda t: self.gameDone(), self.taskName('all-aboard-delay'))
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def toonUpToon(self, toonId):
if toonId in self.toonsInDoor:
toon = self.air.doId2do.get(toonId)
if toon:
val = min(BASE_TOON_UP + JOKE_TOON_UP * self.numJokes.get(toonId, 0), toon.getMaxHp())
toon.toonUp(val)
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def removeAll(self):
taskMgr.remove(self.taskName('check-game-done'))
taskMgr.remove(self.taskName('check-players-task'))
taskMgr.remove(self.taskName('time-warning-task'))
taskMgr.remove(self.taskName('all-aboard-delay'))
self.timer.stop()
2015-04-27 18:51:51 -05:00
2015-03-06 20:46:05 -06:00
def disable(self):
DistCogdoGameAI.disable(self)
self.removeAll()
from otp.ai.MagicWordGlobal import *
2015-03-26 21:48:23 -05:00
@magicWord(category=CATEGORY_PROGRAMMER)
2015-03-06 20:46:05 -06:00
def endMaze():
if hasattr(simbase.air, 'cogdoGame'):
maze = simbase.air.cogdoGame
maze.doAction(CogdoMazeGameGlobals.GameActions.OpenDoor, 0)
return 'Finished cogdo maze game!'