# emacs-mode: -*- python-*-
from Plasma import *
from PlasmaTypes import *
import string
actTrigger = ptAttribActivator(1, 'Activator')
respBoolTrue = ptAttribResponder(2, 'Run if bool true')
respBoolFalse = ptAttribResponder(3, 'Run if bool false')
respPlayerFalse = ptAttribResponder(4, 'Run if Player is fail')
stringVarName = 'AirVentOpen'
stringInfo = 'AirVentOpen'
stringInfoFail = "No! You're doing it the WRONG way!"
boolCurrentValue = false
AgeStartedIn = None
ActingAvatarKey = None

class relltooAirVentLogic(ptResponder,):
    __module__ = __name__
    __module__ = __name__


    def __init__(self):
        ptResponder.__init__(self)
        self.id = 103350001
        self.version = 1
        self.elevatorUpTimerId = 1
        self.elevatorDownTimerId = 2
        self.flapsUpTimerId = 3
        self.ReEnableButtonsTimerId = 4



    def OnFirstUpdate(self):
        global AgeStartedIn
        AgeStartedIn = PtGetAgeName()



    def OnServerInitComplete(self):
        global boolCurrentValue
        if (AgeStartedIn == PtGetAgeName()):
            ageSDL = PtGetAgeSDL()
            if (stringVarName != ''):
                ageSDL.setNotify(self.key, stringVarName, 0.0)
                ageSDL.setFlags(stringVarName, 1, 1)
                ageSDL.sendToClients(stringVarName)
                self.SetIrisCollider()
                self.SetButton(0)
                try:
                    boolCurrentValue = ageSDL[stringVarName][0]
                except:
                    PtDebugPrint('ERROR: rellTooAirVentLogic.OnServerInitComplete():\tERROR reading age SDL')
                PtDebugPrint(('DEBUG: rellTooAirVentLogic.OnServerInitComplete():\t%s = %d' % (stringVarName,
                 boolCurrentValue)))
                if boolCurrentValue:
                    PtDebugPrint(('DEBUG: rellTooAirVentLogic.OnServerInitComplete():\tRunning true responder on %s, fastforward=1' % self.sceneobject.getName()))
                    respBoolTrue.run(self.key, fastforward=1)
                else:
                    PtDebugPrint(('DEBUG: rellTooAirVentLogic.OnServerInitComplete():\tRunning false responder on %s, fastforward=1' % self.sceneobject.getName()))
                    respBoolFalse.run(self.key, fastforward=1)
                self.SetButton(0)
            else:
                PtDebugPrint('ERROR: rellTooAirVentLogic.OnServerInitComplete():\tERROR: missing SDL var name')



    def OnNotify(self, state, id, events):
        global boolCurrentValue
        if ((not state) or (id != actTrigger.id)):
            return 
        if (not PtWasLocallyNotified(self.key)):
            return 
        elif ((type(actTrigger.value) == type([])) and (len(actTrigger.value) > 0)):
            PtDebugPrint(('DEBUG: rellTooAirVentLogic.OnNotify():\t local player requesting %s change via %s' % (stringVarName,
             actTrigger.value[0].getName())))
        if (stringVarName == ''):
            PtDebugPrint('ERROR: rellTooAirVentLogic.OnNotify():\tERROR: missing SDL var name')
            return 
        if (AgeStartedIn == PtGetAgeName()):
            ageSDL = PtGetAgeSDL()
            avvieYpos = ptPoint3.getY(ptSceneobject.position(PtGetLocalAvatar()))
            print '\t\tYour Y position is',
            print avvieYpos
            print '\t\tand you are trying to push the crank the',
            if (avvieYpos > 1.3999999999999999):
                print 'right way. Clever girl.'
                if boolCurrentValue:
                    boolCurrentValue = false
                    if ageSDL['ElevatorAtTop'][0]:
                        PtAtTimeCallback(self.key, 6, self.elevatorDownTimerId)
                        PtAtTimeCallback(self.key, 6, self.flapsUpTimerId)
                else:
                    boolCurrentValue = true
                    if (ageSDL['FlapsDown'][0] and (not ageSDL['ElevatorAtTop'][0])):
                        PtAtTimeCallback(self.key, 6, self.elevatorUpTimerId)
                ageSDL.setTagString(stringVarName, stringInfo)
                ageSDL.setIndex(stringVarName, 0, boolCurrentValue)
                PtDebugPrint(('DEBUG: rellTooAirVentLogic.OnNotify():\tset age SDL var %s to %d' % (stringVarName,
                 boolCurrentValue)))
            else:
                print 'wrong way, you fool!'
                boolCurrentValue = (1 - boolCurrentValue)
                ageSDL.setTagString(stringVarName, stringInfoFail)
                ageSDL.setIndex(stringVarName, 0, boolCurrentValue)



    def OnSDLNotify(self, VARname, SDLname, playerID, tag):
        global ActingAvatarKey
        global boolCurrentValue
        if (AgeStartedIn == PtGetAgeName()):
            ageSDL = PtGetAgeSDL()
            if (VARname == stringVarName):
                PtDebugPrint(('DEBUG: rellTooAirVentLogic.OnSDLNotify():\t VARname:%s, SDLname:%s, tag:%s, value:%d' % (VARname,
                 SDLname,
                 tag,
                 ageSDL[stringVarName][0])))
                boolCurrentValue = ageSDL[stringVarName][0]
                self.SetIrisCollider()
                if playerID:
                    ActingAvatarKey = PtGetAvatarKeyFromClientID(playerID)
                    objAvatar = ptSceneobject(ActingAvatarKey, self.key)
                else:
                    ActingAvatarKey = None
                    objAvatar = None
                if (tag == 'Ignore'):
                    PtDebugPrint('DEBUG: rellTooAirVentLogic.OnSDLNotify():\tIgnoring notification')
                else:
                    self.DoCutCam(1)
                    self.SetButton(1)
                    if (tag == stringInfoFail):
                        PtDebugPrint(('DEBUG: rellTooAirVentLogic.OnSDLNotify():\tRunning PlayerFalse responder on %s' % self.sceneobject.getName()))
                        respPlayerFalse.run(self.key, avatar=objAvatar, fastforward=0)
                        PtAtTimeCallback(self.key, 4, self.ReEnableButtonsTimerId)
                        boolCurrentValue = (1 - boolCurrentValue)
                        ageSDL.setTagString(stringVarName, 'Ignore')
                        ageSDL.setIndex(stringVarName, 0, boolCurrentValue)
                    else:
                        if boolCurrentValue:
                            PtDebugPrint(('DEBUG: rellTooAirVentLogic.OnSDLNotify():\tRunning true responder on %s' % self.sceneobject.getName()))
                            respBoolTrue.run(self.key, avatar=objAvatar, fastforward=0)
                        else:
                            PtDebugPrint(('DEBUG: rellTooAirVentLogic.OnSDLNotify():\tRunning false responder on %s' % self.sceneobject.getName()))
                            respBoolFalse.run(self.key, avatar=objAvatar, fastforward=0)
                        PtAtTimeCallback(self.key, 14, self.ReEnableButtonsTimerId)



    def SetIrisCollider(self):
        ageSDL = PtGetAgeSDL()
        tempBool = ageSDL['AirVentOpen'][0]
        floorObj = PtFindSceneobject('iris_coll', AgeStartedIn)
        if tempBool:
            floorObj.physics.suppress(true)
        else:
            floorObj.physics.suppress(false)



    def SetButton(self, actstate):
        ButnObj = PtFindSceneobject('T1_shuttercrank_trig', AgeStartedIn)
        if actstate:
            print '\t\tDisabling button.'
            ButnObj.physics.suppress(true)
        else:
            print '\t\tEnabling button.'
            ButnObj.physics.suppress(false)



    def DoCutCam(self, actstate):
        CamObj = PtFindSceneobject('T1_shuttercrank_cam', AgeStartedIn)
        if ActingAvatarKey:
            if actstate:
                print '\t\tSwitching to camera:',
                print CamObj,
                print 'on avatar:',
                print ActingAvatarKey
                CamObj.pushCamera(ActingAvatarKey)
            else:
                print '\t\tPopping camera',
                print CamObj,
                print 'on avatar:',
                print ActingAvatarKey
                CamObj.popCamera(ActingAvatarKey)



    def OnTimer(self, id):
        ageSDL = PtGetAgeSDL()
        if (id == self.elevatorUpTimerId):
            ageSDL.setIndex('ElevatorAtTop', 0, true)
        elif (id == self.elevatorDownTimerId):
            ageSDL.setIndex('ElevatorAtTop', 0, false)
        elif (id == self.flapsUpTimerId):
            ageSDL.setIndex('FlapsDown', 0, false)
        elif (id == self.ReEnableButtonsTimerId):
            self.SetButton(0)
            self.DoCutCam(0)



glue_cl = None
glue_inst = None
glue_params = None
glue_paramKeys = None
try:
    x = glue_verbose
except NameError:
    glue_verbose = 0

def glue_getClass():
    global glue_cl
    if (glue_cl == None):
        try:
            cl = eval(glue_name)
            if issubclass(cl, ptModifier):
                glue_cl = cl
            elif glue_verbose:
                print ('Class %s is not derived from modifier' % cl.__name__)
        except NameError:
            if glue_verbose:
                try:
                    print ('Could not find class %s' % glue_name)
                except NameError:
                    print 'Filename/classname not set!'
    return glue_cl



def glue_getInst():
    global glue_inst
    if (type(glue_inst) == type(None)):
        cl = glue_getClass()
        if (cl != None):
            glue_inst = cl()
    return glue_inst



def glue_delInst():
    global glue_inst
    global glue_cl
    global glue_paramKeys
    global glue_params
    if (type(glue_inst) != type(None)):
        del glue_inst
    glue_cl = None
    glue_params = None
    glue_paramKeys = None



def glue_getVersion():
    inst = glue_getInst()
    ver = inst.version
    glue_delInst()
    return ver



def glue_findAndAddAttribs(obj, glue_params):
    if isinstance(obj, ptAttribute):
        if glue_params.has_key(obj.id):
            if glue_verbose:
                print 'WARNING: Duplicate attribute ids!'
                print ('%s has id %d which is already defined in %s' % (obj.name,
                 obj.id,
                 glue_params[obj.id].name))
        else:
            glue_params[obj.id] = obj
    elif (type(obj) == type([])):
        for o in obj:
            glue_findAndAddAttribs(o, glue_params)

    elif (type(obj) == type({})):
        for o in obj.values():
            glue_findAndAddAttribs(o, glue_params)

    elif (type(obj) == type(())):
        for o in obj:
            glue_findAndAddAttribs(o, glue_params)




def glue_getParamDict():
    global glue_paramKeys
    global glue_params
    if (type(glue_params) == type(None)):
        glue_params = {}
        gd = globals()
        for obj in gd.values():
            glue_findAndAddAttribs(obj, glue_params)

        glue_paramKeys = glue_params.keys()
        glue_paramKeys.sort()
        glue_paramKeys.reverse()
    return glue_params



def glue_getClassName():
    cl = glue_getClass()
    if (cl != None):
        return cl.__name__
    if glue_verbose:
        print ('Class not found in %s.py' % glue_name)
    return None



def glue_getBlockID():
    inst = glue_getInst()
    if (inst != None):
        return inst.id
    if glue_verbose:
        print ('Instance could not be created in %s.py' % glue_name)
    return None



def glue_getNumParams():
    pd = glue_getParamDict()
    if (pd != None):
        return len(pd)
    if glue_verbose:
        print ('No attributes found in %s.py' % glue_name)
    return 0



def glue_getParam(number):
    pd = glue_getParamDict()
    if (pd != None):
        if (type(glue_paramKeys) == type([])):
            if ((number >= 0) and (number < len(glue_paramKeys))):
                return pd[glue_paramKeys[number]].getdef()
            else:
                print ('glue_getParam: Error! %d out of range of attribute list' % number)
        else:
            pl = pd.values()
            if ((number >= 0) and (number < len(pl))):
                return pl[number].getdef()
            elif glue_verbose:
                print ('glue_getParam: Error! %d out of range of attribute list' % number)
    if glue_verbose:
        print 'GLUE: Attribute list error'
    return None



def glue_setParam(id, value):
    pd = glue_getParamDict()
    if (pd != None):
        if pd.has_key(id):
            try:
                pd[id].__setvalue__(value)
            except AttributeError:
                if isinstance(pd[id], ptAttributeList):
                    try:
                        if (type(pd[id].value) != type([])):
                            pd[id].value = []
                    except AttributeError:
                        pd[id].value = []
                    pd[id].value.append(value)
                else:
                    pd[id].value = value
        elif glue_verbose:
            print "setParam: can't find id=",
            print id
    else:
        print 'setParma: Something terribly has gone wrong. Head for the cover.'



def glue_isNamedAttribute(id):
    pd = glue_getParamDict()
    if (pd != None):
        try:
            if isinstance(pd[id], ptAttribNamedActivator):
                return 1
            if isinstance(pd[id], ptAttribNamedResponder):
                return 2
        except KeyError:
            if glue_verbose:
                print ('Could not find id=%d attribute' % id)
    return 0



def glue_isMultiModifier():
    inst = glue_getInst()
    if isinstance(inst, ptMultiModifier):
        return 1
    return 0



def glue_getVisInfo(number):
    pd = glue_getParamDict()
    if (pd != None):
        if (type(glue_paramKeys) == type([])):
            if ((number >= 0) and (number < len(glue_paramKeys))):
                return pd[glue_paramKeys[number]].getVisInfo()
            else:
                print ('glue_getVisInfo: Error! %d out of range of attribute list' % number)
        else:
            pl = pd.values()
            if ((number >= 0) and (number < len(pl))):
                return pl[number].getVisInfo()
            elif glue_verbose:
                print ('glue_getVisInfo: Error! %d out of range of attribute list' % number)
    if glue_verbose:
        print 'GLUE: Attribute list error'
    return None



# local variables:
# tab-width: 4
