Skip to content

Instantly share code, notes, and snippets.

@Kronos11
Created January 24, 2012 00:35
Show Gist options
  • Save Kronos11/1666945 to your computer and use it in GitHub Desktop.
Save Kronos11/1666945 to your computer and use it in GitHub Desktop.
Initial Python for PSM
#-------------------------------------------------------------------------------
# Name: ActionStateData
# Purpose:
#
# Author: Kyle Craviotto
#
# Created: 23/01/2012
# Copyright: (c) SWGANH 2012
# Licence: GPL2 SWGANH
#-------------------------------------------------------------------------------
# ACTION LAYER
# BITMASKS
clear = 0
cover = 1
combat = 2
peace = 4
aiming = 8
berserk = 16
feign_death = 32
attitude_evasive = 64
attitude_normal = 128
attitude_aggresive = 256
tumbling = 512
rallied = 1024
stunned = 2048
blinded = 5096
'etc -- https://github.com/Kronos11/mmoserver/blob/develop/src/ZoneServer/CreatureEnums.h'
#-------------------------------------------------------------------------------
# Name: ActorStateManager
# Purpose: Implements 3 parallel state machines
#
# Author: Kyle Craviotto
#
# Created: 23/01/2012
# Copyright: (c) SWGANH 2012
# Licence: GPL2 SWGANH
#-------------------------------------------------------------------------------
import StateData as sd
class ActorStateManager():
def __init__(self):
"""
Initialize the three state machines to the default states, set
up the dictionary to track blocked states
"""
self.__stateMachineDict = {}
self.__stateMachineDict[sd.POSTURE] = sd._upright
self.__stateMachineDict[sd.LOCOMOTION] = sd._standing
self.__stateMachineDict[sd.ACTION] = sd._clear
self.__blockedStateDict = {}
def _Transition(self, newState):
"""
Transitions to the specified newState by first exiting the current
state on the state machine that newState belongs to. Make newState the
current state and call its setup method
"""
assert self.__stateMachineDict.has_key(newState.GetStateTypeId()), "Error: Unknown state type"
self.__stateMachineDict[newState.GetStateTypeId()].OnExitState(self)
self.__stateMachineDict[newState.GetStateTypeId()] = newState
self.__stateMachineDict[newState.GetStateTypeId()].OnEnterState(self)
def RequestTransition(self, state):
"""
Transitions to the given state, while honoring any blocks in place.
Returns 1 if sucessfully transtioned, 0 otherwise
"""
if not self.CanTransitionTo(state):
return 0
self._Transition(state)
return 1
def TransitionTo(self, state):
"""
Force transtion into the specified state, ignoring any blocks. Use
with care.
"""
self._Transition(state)
def GetAllStateIds(self):
"""
Returns the ids of the current states in each layer as a tuple
"""
return (self.GetPostureState().GetStateId(),
self.GetLocomotionState().GetStateId(),
self.GetActionState().GetStateId())
def SetAllStateIds(self, stateIdList):
"""
Sets each of the states based on the list of ids in stateIdList
"""
for stateId in stateIdList:
state = shared._GetStateModuleById(stateId)
self.TransitionTo(state)
def ForceDefaultStates(self):
"""
Forces the actor back to the starting states when created,
where they are doing "nothing"
"""
self.TransitionTo(_standing)
self.TransitionTo(_stopped)
self.TransitionTo(_idle)
def CanTransitionTo(self, state):
"""
Returns whether a transition to the specified state is blocked or not.
"""
return self.IsBlocked(state) == 0
def _GetCurrentState(self, stateTypeId):
"""
Return the current state for the specified stateTypeId (layer)
"""
assert self.__stateMachineDict.has_key(stateTypeId), "Error: Unknown state type"
return self.__stateMachineDict[stateTypeId]
def GetPostureState(self):
"""
Returns current posture state
"""
return self.__stateMachineDict[_characterstatedata.POSTURE]
def GetLocomotionState(self):
"""
Returns the current locomotion state
"""
return self.__stateMachineDict[_characterstatedata.LOCOMOTION]
def GetActionState(self):
"""
Returns the current action state
"""
return self.__stateMachineDict[_characterstatedata.ACTION]
def BlockState(self, state):
"""
Blocks transitions to the specified state
"""
self.__blockedStateDict[state] = self.__blockedStateDict.get(state, 0) + 1
def UnblockState(self, state):
"""
removes a block on the specified state
"""
self.__blockedStateDict[state] = self.__blockedStateDict[state] - 1
assert self.__blockedStateDict[state] >= 0, "Tried to unblock a state that was not previously blocked"
def IsBlocked(self, state):
"""
Returns whether the specified state is blocked
"""
return self.__blockedStateDict.get(state, 0) > 0
def GetBlockedStateDict(self):
"""
Returns the blocked state dictionary
"""
return self.__blockedStateDict
def SetBlockedStateDict(self, blockedDict):
"""
Sets the blocked state dictonary, usually used
in cases where a correction/sync up nees to happen
"""
self.__blockedStateDict = blockedDict
def BlockCharacterInput(self):
"""
Helper method to block the actor from doing anything
"""
self.BlockLocomotion()
self.BlockPosture()
self.BlockAction()
def UnblockCharacterInput(self):
"""
Helper method to undo effects of previous call to
BlockCharacterInput
"""
self.UnblockLocomotion()
self.UnblockPosture()
self.UnblockAction()
def BlockLocomotion(self):
"""
Helper method to block all transitions in the locomotion
state machine
"""
self.BlockState(_stopped)
self.BlockState(_movebackward)
self.BlockState(_moveforward)
self.BlockState(_moveleft)
self.BlockState(_moveright)
def UnblockLocomotion(self):
"""
Helper method to undo effects of previous call to
BlockCharacterLocomotion
"""
self.UnblockState(_stopped)
self.UnblockState(_movebackward)
self.UnblockState(_moveforward)
self.UnblockState(_moveleft)
self.UnblockState(_moveright)
def BlockPosture(self):
"""
Helper method to block transitions in the posture
state machine
"""
self.BlockState(_standing)
self.BlockState(_sitting)
self.BlockState(_layingdown)
self.BlockState(_dead)
def UnblockPosture(self):
"""
Helper method undo effects of previous call to
BlockCharacterPosture
"""
self.UnblockState(_standing)
self.UnblockState(_sitting)
self.UnblockState(_layingdown)
self.UnblockState(_dead)
def BlockAction(self):
"""
Helper method to block transitions in the action
state machine. Idle not blocked because actor
must be idle to try any other action
"""
self.BlockState(_casting)
self.BlockState(_fighting)
self.BlockState(_gesturing)
self.BlockState(_crafting)
def UnblockAction(self):
"""
Helper method to undo effects of previous call to
BlockCharacterAction
"""
self.UnblockState(_casting)
self.UnblockState(_fighting)
self.UnblockState(_gesturing)
self.UnblockState(_crafting)
#-------------------------------------------------------------------------------
# Name: LocomotionStateData
# Purpose:
#
# Author: Kyle Craviotto
#
# Created: 23/01/2012
# Copyright: (c) SWGANH 2012
# Licence: GPL2 SWGANH
#-------------------------------------------------------------------------------
#!/usr/bin/env python
# LOCOMOTION LAYER
invalid = -1
standing = 0
sneaking = 1
walking = 2
running = 3
kneeling = 4
crouch_sneaking = 5
crouch_walking = 6
prone = 7
crawling = 8
climbing_stationary = 9
climbing = 10
hovering = 11
lying_down = 12
sitting = 13
skil_animating = 14
driving_vehicle = 15
riding_creature = 16
knocked_down = 17
incapacitated = 18
dead = 19
blocking = 20
'"" etc '
#-------------------------------------------------------------------------------
# Name: PostureStateData
# Purpose:
#
# Author: Kyle Craviotto
#
# Created: 23/01/2012
# Copyright: (c) SWGANH 2012
# Licence: GPL2 SWGANH
#-------------------------------------------------------------------------------
#!/usr/bin/env python
# STATE IDs BY LAYER
# POSTURE LAYER
invalid = -1
upright = 0
crouched = 1
prone = 2
sneaking = 3
blocking = 4
climbing = 5
flying = 6
lying_down = 7
sitting = 8
skill_animating = 9
driving_vehicle = 10
riding_creature = 11
knocked_down = 12
incapacitated = 13
dead = 14
'"" etc '
#-------------------------------------------------------------------------------
# Name: StateData
# Purpose: Implements 3 parallel state machines
#
# Author: Kyle Craviotto
#
# Created: 23/01/2012
# Copyright: (c) SWGANH 2012
# Licence: GPL2 SWGANH
#-------------------------------------------------------------------------------
#!/usr/bin/env python
# ACTION STATE
import ActionStateData as ASD
import ASD.clear as _clear
import ASD.cover as _cover
import ASD.combat as _combat
import ASD.peace as _peace
# LOCOMOTION STATE
import LocomotionStateData as LSD
import LSD.standing as _standing
# POSTURE STATE
import PostureStateData as PSD
import PSD.upright as _upright
# LAYER IDs
POSTURE = 1
LOCOMOTION = 2
ACTION = 3
# STATE LOOKUP DICTIONARY - built by the RegisterState utility function
stateLookup = {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment