Poodletooth-iLand/toontown/safezone/DistributedFindFourAI.py

637 lines
17 KiB
Python
Raw Normal View History

# File: D (Python 2.4)
2015-03-03 16:10:12 -06:00
from direct.distributed.DistributedNodeAI import DistributedNodeAI
from direct.distributed.ClockDelta import *
from direct.fsm import ClassicFSM, State
from direct.fsm import State
from direct.fsm import StateData
from direct.distributed.ClockDelta import *
from direct.interval.IntervalGlobal import *
2015-03-03 16:10:12 -06:00
class DistributedFindFourAI(DistributedNodeAI):
2015-07-05 18:59:25 -05:00
def __init__(self, air, parent, name, x, y, z, h, p, r):
DistributedNodeAI.__init__(self, air)
self.name = name
self.air = air
self.setPos(x, y, z)
self.setHpr(h, p, r)
self.myPos = (x, y, z)
self.myHpr = (h, p, r)
self.board = [
[
0,
0,
0,
0,
0,
0,
0],
[
0,
0,
0,
0,
0,
0,
0],
[
0,
0,
0,
0,
0,
0,
0],
[
0,
0,
0,
0,
0,
0,
0],
[
0,
0,
0,
0,
0,
0,
0],
[
0,
0,
0,
0,
0,
0,
0]]
self.parent = self.air.doId2do[parent]
self.parentDo = parent
self.wantStart = []
self.playersPlaying = []
self.playersSitting = 0
self.playersTurn = 1
self.movesMade = 0
self.playerNum = 1
self.winDirection = None
self.playersGamePos = [
None,
None]
self.wantTimer = True
self.timerEnd = 0
self.turnEnd = 0
self.playersObserving = []
self.winLaffPoints = 20
self.movesRequiredToWin = 10
self.zoneId = self.air.allocateZone()
self.generateOtpObject(air.districtId, self.zoneId, optionalFields = [
'setX',
'setY',
'setZ',
'setH',
'setP',
'setR'])
self.parent.setCheckersZoneId(self.zoneId)
self.timerStart = None
self.fsm = ClassicFSM.ClassicFSM('Checkers', [
State.State('waitingToBegin', self.enterWaitingToBegin, self.exitWaitingToBegin, [
'playing']),
State.State('playing', self.enterPlaying, self.exitPlaying, [
'gameOver']),
State.State('gameOver', self.enterGameOver, self.exitGameOver, [
'waitingToBegin'])], 'waitingToBegin', 'waitingToBegin')
self.fsm.enterInitialState()
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def announceGenerate(self):
self.parent.setGameDoId(self.doId)
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def getTableDoId(self):
return self.parentDo
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def delete(self):
self.fsm.requestFinalState()
self.parent = None
self.parentDo = None
del self.board
del self.fsm
DistributedNodeAI.delete(self)
2015-07-05 18:59:25 -05:00
def informGameOfPlayer(self):
self.playersSitting += 1
if self.playersSitting < 2:
self.timerEnd = 0
elif self.playersSitting == 2:
self.timerEnd = globalClock.getRealTime() + 20
self.parent.isAccepting = False
self.parent.sendUpdate('setIsPlaying', [
1])
elif self.playersSitting > 2:
pass
2015-07-05 18:59:25 -05:00
self.sendUpdate('setTimer', [
globalClockDelta.localToNetworkTime(self.timerEnd)])
2015-07-05 18:59:25 -05:00
def informGameOfPlayerLeave(self):
self.playersSitting -= 1
if self.playersSitting < 2 and self.fsm.getCurrentState().getName() == 'waitingToBegin':
self.timerEnd = 0
self.parent.isAccepting = True
self.parent.sendUpdate('setIsPlaying', [
0])
2015-07-05 18:59:25 -05:00
if self.playersSitting > 2 and self.fsm.getCurrentState().getName() == 'waitingToBegin':
pass
1
self.timerEnd = 0
if self.timerEnd != 0:
self.sendUpdate('setTimer', [
globalClockDelta.localToNetworkTime(self.timerEnd)])
else:
self.sendUpdate('setTimer', [
0])
2015-07-05 18:59:25 -05:00
def setGameCountdownTime(self):
self.timerEnd = globalClock.getRealTime() + 10
2015-07-05 18:59:25 -05:00
def setTurnCountdownTime(self):
self.turnEnd = globalClock.getRealTime() + 25
2015-07-05 18:59:25 -05:00
def getTimer(self):
if self.timerEnd != 0:
return 0
else:
return 0
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def getTurnTimer(self):
return globalClockDelta.localToNetworkTime(self.turnEnd)
2015-07-05 18:59:25 -05:00
2015-03-03 16:10:12 -06:00
def requestTimer(self):
avId = self.air.getAvatarIdFromSender()
self.sendUpdateToAvatarId(avId, 'setTimer', [
globalClockDelta.localToNetworkTime(self.timerEnd)])
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def handlePlayerExit(self, avId):
if avId in self.wantStart:
self.wantStart.remove(avId)
2015-07-05 18:59:25 -05:00
if self.fsm.getCurrentState().getName() == 'playing':
gamePos = self.playersGamePos.index(avId)
self.playersGamePos[gamePos] = None
self.fsm.request('gameOver')
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def handleEmptyGame(self):
self.movesMade = 0
self.playersPlaying = []
self.playersTurn = 1
self.playerNum = 1
self.fsm.request('waitingToBegin')
self.parent.isAccepting = True
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def requestWin(self, pieceNum):
avId = self.air.getAvatarIdFromSender()
playerNum = self.playersGamePos.index(avId) + 1
x = pieceNum[0]
y = pieceNum[1]
if self.checkWin(x, y, playerNum) == True:
self.sendUpdate('announceWinnerPosition', [
x,
y,
self.winDirection,
playerNum])
winnersSequence = Sequence(Wait(5.0), Func(self.fsm.request, 'gameOver'), Func(self.parent.announceWinner, 'Find Four', avId))
winnersSequence.start()
else:
self.sendUpdateToAvatarId(avId, 'illegalMove', [])
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def distributeLaffPoints(self):
for x in self.parent.seats:
if x != None:
av = self.air.doId2do.get(x)
av.toonUp(self.winLaffPoints)
continue
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def enterWaitingToBegin(self):
self.setGameCountdownTime()
self.parent.isAccepting = True
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def exitWaitingToBegin(self):
self.turnEnd = 0
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def enterPlaying(self):
self.parent.isAccepting = False
for x in self.playersGamePos:
if x != None:
self.playersTurn = self.playersGamePos.index(x)
self.d_sendTurn(self.playersTurn + 1)
break
continue
2015-07-05 18:59:25 -05:00
self.setTurnCountdownTime()
self.sendUpdate('setTurnTimer', [
globalClockDelta.localToNetworkTime(self.turnEnd)])
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def exitPlaying(self):
2015-03-03 16:10:12 -06:00
pass
2015-07-05 18:59:25 -05:00
def enterGameOver(self):
self.timerEnd = 0
isAccepting = True
self.parent.handleGameOver()
self.playersObserving = []
self.playersTurn = 1
self.playerNum = 1
self.playersPlaying = []
self.movesMade = 0
self.playersGamePos = [
None,
None]
self.parent.isAccepting = True
self.fsm.request('waitingToBegin')
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def exitGameOver(self):
2015-03-03 16:10:12 -06:00
pass
2015-07-05 18:59:25 -05:00
def requestBegin(self):
avId = self.air.getAvatarIdFromSender()
if avId not in self.wantStart:
self.wantStart.append(avId)
2015-07-05 18:59:25 -05:00
numPlayers = 0
for x in self.parent.seats:
if x != None:
numPlayers = numPlayers + 1
continue
2015-07-05 18:59:25 -05:00
if len(self.wantStart) == numPlayers and numPlayers >= 2:
self.d_gameStart(avId)
self.parent.sendIsPlaying()
2015-03-03 16:10:12 -06:00
2015-07-05 18:59:25 -05:00
def d_gameStart(self, avId):
for x in self.playersObserving:
self.sendUpdateToAvatarId(x, 'gameStart', [
255])
2015-07-05 18:59:25 -05:00
zz = 0
numPlayers = 0
for x in self.parent.seats:
if x != None:
numPlayers += 1
self.playersPlaying.append(x)
continue
2015-07-05 18:59:25 -05:00
if numPlayers == 2:
player1 = self.playersPlaying[0]
self.sendUpdateToAvatarId(player1, 'gameStart', [
1])
self.playersGamePos[0] = player1
player2 = self.playersPlaying[1]
self.sendUpdateToAvatarId(player2, 'gameStart', [
2])
self.playersGamePos[1] = player2
2015-07-05 18:59:25 -05:00
self.wantStart = []
self.fsm.request('playing')
self.parent.getTableState()
2015-07-05 18:59:25 -05:00
def d_sendTurn(self, playersTurn):
self.sendUpdate('sendTurn', [
playersTurn])
2015-07-05 18:59:25 -05:00
def advancePlayerTurn(self):
if self.playersTurn == 0:
self.playersTurn = 1
self.playerNum = 2
else:
self.playerNum = 1
self.playersTurn = 0
2015-07-05 18:59:25 -05:00
def requestMove(self, moveColumn):
avId = self.air.getAvatarIdFromSender()
turn = self.playersTurn
if avId in self.playersGamePos:
if self.playersGamePos.index(avId) != self.playersTurn:
pass
2015-07-05 18:59:25 -05:00
if self.board[0][moveColumn] != 0:
self.sendUpdateToAvatarId(avId, 'illegalMove', [])
2015-07-05 18:59:25 -05:00
2015-07-05 20:02:35 -05:00
for x in xrange(6):
if self.board[x][moveColumn] == 0:
movePos = x
continue
2015-07-05 18:59:25 -05:00
self.board[movePos][moveColumn] = self.playersTurn + 1
if self.checkForTie() == True:
self.sendUpdate('setGameState', [
self.board,
moveColumn,
movePos,
turn])
self.sendUpdate('tie', [])
winnersSequence = Sequence(Wait(8.0), Func(self.fsm.request, 'gameOver'))
winnersSequence.start()
return None
2015-07-05 18:59:25 -05:00
self.movesMade += 1
self.advancePlayerTurn()
self.setTurnCountdownTime()
self.sendUpdate('setTurnTimer', [
globalClockDelta.localToNetworkTime(self.turnEnd)])
self.d_sendTurn(self.playersTurn + 1)
self.sendUpdate('setGameState', [
self.board,
moveColumn,
movePos,
turn])
2015-07-05 18:59:25 -05:00
def checkForTie(self):
2015-07-05 20:02:35 -05:00
for x in xrange(7):
if self.board[0][x] == 0:
return False
continue
2015-07-05 18:59:25 -05:00
return True
2015-07-05 18:59:25 -05:00
def getState(self):
return self.fsm.getCurrentState().getName()
2015-07-05 18:59:25 -05:00
def getName(self):
return self.name
2015-07-05 18:59:25 -05:00
def getGameState(self):
return [
self.board,
0,
0,
0]
2015-07-05 18:59:25 -05:00
def clearBoard(self):
for x in self.board.squareList:
x.setState(0)
2015-07-05 18:59:25 -05:00
def getPosHpr(self):
return self.posHpr
2015-07-05 18:59:25 -05:00
def tempSetBoardState(self):
self.board = [
[
0,
0,
0,
0,
0,
0,
0],
[
1,
2,
1,
2,
2,
2,
1],
[
2,
2,
1,
2,
1,
2,
1],
[
2,
1,
1,
2,
2,
1,
2],
[
1,
2,
2,
1,
2,
1,
1],
[
1,
2,
1,
2,
1,
2,
1]]
self.sendUpdate('setGameState', [
self.board,
0,
0,
1])
2015-07-05 18:59:25 -05:00
def checkWin(self, rVal, cVal, playerNum):
if self.checkHorizontal(rVal, cVal, playerNum) == True:
self.winDirection = 0
return True
elif self.checkVertical(rVal, cVal, playerNum) == True:
self.winDirection = 1
return True
elif self.checkDiagonal(rVal, cVal, playerNum) == True:
self.winDirection = 2
return True
else:
self.winDirection = None
return False
2015-07-05 18:59:25 -05:00
def checkHorizontal(self, rVal, cVal, playerNum):
if cVal == 3:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal][cVal - x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal][cVal - x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal][cVal + x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal][cVal + x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
elif cVal == 2:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal][cVal + x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal][cVal + x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
elif cVal == 4:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal][cVal - x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal][cVal - x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
else:
return False
2015-07-05 18:59:25 -05:00
def checkVertical(self, rVal, cVal, playerNum):
if rVal == 2:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal + x][cVal] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal + x][cVal] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
elif rVal == 3:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal - x][cVal] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal - x][cVal] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
else:
return False
2015-07-05 18:59:25 -05:00
def checkDiagonal(self, rVal, cVal, playerNum):
if cVal <= 2:
if rVal == 2:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal + x][cVal + x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal + x][cVal + x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
elif rVal == 3:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal - x][cVal + x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal - x][cVal + x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
2015-07-05 18:59:25 -05:00
elif cVal >= 4:
if rVal == 2:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal + x][cVal - x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal + x][cVal - x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
elif rVal == 3:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal - x][cVal - x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal - x][cVal - x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
2015-07-05 18:59:25 -05:00
elif rVal == 3 and rVal == 4 or rVal == 5:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal - x][cVal - x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal - x][cVal - x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal + x][cVal - x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal + x][cVal - x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
elif rVal == 0 and rVal == 1 or rVal == 2:
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal + x][cVal - x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal + x][cVal - x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
2015-07-05 20:02:35 -05:00
for x in xrange(1, 4):
if self.board[rVal + x][cVal + x] != playerNum:
break
2015-07-05 18:59:25 -05:00
if self.board[rVal + x][cVal + x] == playerNum and x == 3:
return True
continue
2015-07-05 18:59:25 -05:00
return False
2015-07-05 18:59:25 -05:00
return False