Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button Puzzle Scripts #726

Merged
merged 2 commits into from May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
226 changes: 226 additions & 0 deletions Scripts/Python/xAgeSDLBoolActivatorComboSet.py
@@ -0,0 +1,226 @@
# -*- coding: utf-8 -*-
""" *==LICENSE==*

CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Additional permissions under GNU GPL version 3 section 7

If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.

You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021

*==LICENSE==* """

from Plasma import *
from PlasmaTypes import *

solvedVariableName = ptAttribString(1, "SDL: Boolean Solved Variable")
butsInUseVariableName = ptAttribString(2, "SDL: Button Lock Variable")
numCorrectVariableName = ptAttribString(3, "SDL: Number of correct entries")
combination = ptAttribString(4, "Combination")
resetOnEmpty = ptAttribBoolean(5, "Reset when the Age shuts down", default=False)
disableOnSolve = ptAttribBoolean(6, "Disable activators on solve", default=True)
actButtons = ptAttribActivatorList(7, "Act: Buttons")
respButtonPush = ptAttribResponder(8, "Resp: Button Push")

class xAgeSDLBoolActivatorComboSet(ptResponder):
def __init__(self):
ptResponder.__init__(self)
self.version = 1
self.id = 719

# Defer setting up the handlers until PFM is initialized by the engine
self._NotifyHandlers = {}
self._HitActId = None

def OnInit(self):
self._ValidateCombination()
if respButtonPush.value is not None:
self._NotifyHandlers[actButtons.id] = self._RunPushResp
else:
self._NotifyHandlers[actButtons.id] = self._PickButton
self._NotifyHandlers[respButtonPush.id] = self._RespPushComplete

def OnServerInitComplete(self):
ageSDL = PtGetAgeSDL()
ageSDL.setFlags(solvedVariableName.value, True, True)
ageSDL.sendToClients(solvedVariableName.value)
ageSDL.setNotify(self.key, solvedVariableName.value, 0.0)
ageSDL.setFlags(butsInUseVariableName.value, True, True)
ageSDL.sendToClients(butsInUseVariableName.value)
ageSDL.setNotify(self.key, butsInUseVariableName.value, 0.0)
ageSDL.setFlags(numCorrectVariableName.value, True, True)
ageSDL.sendToClients(numCorrectVariableName.value)

if not PtGetPlayerList():
self._butsInUse = False
if resetOnEmpty.value:
self._solved = (False, "fastforward")
self._numCorrect = (0, "fastforward")

if self._butsInUse:
actButtons.disable()

# ======================================================================

def OnSDLNotify(self, VARname, SDLname, playerID, tag):
if VARname == solvedVariableName.value:
if not self._solved:
self._numCorrect = 0
elif VARname == butsInUseVariableName.value:
if self._butsInUse:
actButtons.disable()
else:
actButtons.enable()

def OnNotify(self, state, id, events):
if not state or not PtWasLocallyNotified(self.key):
return
handler = self._NotifyHandlers.get(id, None)
if handler is None:
PtDebugPrint(f"xAgeSDLBoolActComboSet.OnNotify():\tUnhandled notify {id=} {events=}", level=kDebugDumpLevel)
else:
handler(events)

# ======================================================================

def _GetAgeSDL(self, attr):
return PtGetAgeSDL()[attr.value][0]
def _SetAgeSDL(self, attr, value):
ageSDL = PtGetAgeSDL()
if isinstance(value, tuple):
state, hint = value
else:
state, hint = value, ""
ageSDL.setTagString(attr.value, hint)
ageSDL.setIndexNow(attr.value, 0, state)

@property
def _butsInUse(self):
return self._GetAgeSDL(butsInUseVariableName)

@_butsInUse.setter
def _butsInUse(self, value):
self._SetAgeSDL(butsInUseVariableName, value)

@property
def _solved(self):
return self._GetAgeSDL(solvedVariableName)

@_solved.setter
def _solved(self, value):
self._SetAgeSDL(solvedVariableName, value)

@property
def _numCorrect(self):
return self._GetAgeSDL(numCorrectVariableName)

@_numCorrect.setter
def _numCorrect(self, value):
self._SetAgeSDL(numCorrectVariableName, value)

# ======================================================================

def _GetPickedActID(self, events):
try:
pickedSOKey = next((i[3].getKey() for i in events if i[0] == kPickedEvent))
return next((i for i, actKey in enumerate(actButtons.value) if actKey.getParentKey() == pickedSOKey))
except:
PtDebugPrint("xAgeSDLBoolActivatorComboSet._GetPickedActID():\tUnable to determine picked activator ID")
raise

def _PickButton(self, events):
# Run if there is no push responder
actId = self._GetPickedActID(events)
PtDebugPrint(f"xAgeSDLBoolActivatorComboSet._PickedButton():\tPicked button {actId}", level=kWarningLevel)
self._TriggerButton(actId)

def _RunPushResp(self, events):
self._HitActId = self._GetPickedActID(events)
PtDebugPrint(f"xAgeSDLBoolActivatorComboSet._RunPushResp():\tPushing button {self._HitActId}", level=kWarningLevel)
self._butsInUse = True

# Responders can't be fired by integer index, so we'll have to do this the hard way...
notify = ptNotify(self.key)
if isinstance(respButtonPush.value, (list, tuple)):
for i in respButtonPush.value:
notify.addReceiver(i)
else:
notify.addReceiver(respButtonPush.value)
notify.netPropagate(True)
notify.netForce(True)
avKey = PtGetLocalAvatar().getKey()
notify.addCollisionEvent(1, avKey, avKey)
notify.setActivate(1.0)
notify.addResponderState(self._HitActId)
# Whoosh... off it goes...
notify.send()

def _RespPushComplete(self, events):
self._butsInUse = False

# Proxy over to the "wait, there is no responder" method
self._TriggerButton(self._HitActId)
self._HitActId = None

def _TriggerButton(self, actId):
if self._solved:
PtDebugPrint("xAgeSDLBoolActComboSet._TriggerButton():\tYou just unsolved it, moron.", level=kWarningLevel)
self._solved = False
return

try:
desiredActId = self._combination[self._numCorrect]
except IndexError:
PtDebugPrint("xAgeSDLBoolActComboSet._TriggerButton():\tWoah, an index error... Let's pretend this is a bad input...")
desiredActId = -1

if desiredActId == actId:
PtDebugPrint("xAgeSDLBoolActComboSet._TriggerButton():\tThat's right!", level=kWarningLevel)
self._numCorrect += 1
if self._numCorrect == len(self._combination):
PtDebugPrint("xAgeSDLBoolActComboSet._TriggerButton():\tWoohoo! You have solved the puzzle!", level=kWarningLevel)
self._solved = True
if disableOnSolve.value:
self._butsInUse = True
else:
PtDebugPrint("xAgeSDLBoolActComboSet._TriggerButton():\tWRONG! THAT'S ***WRONG***!", level=kWarningLevel)
self._solved = False
self._numCorrect = 0

def _ValidateCombination(self):
if not combination.value:
PtDebugPrint("xAgeSDLBoolActComboSet._ValidateCombination():\tCombination is unset")
if ',' not in combination.value:
PtDebugPrint("xAgeSDLBoolActComboSet._ValidateCombination():\tCombination does not have separation delimiters")
self._combination = [int(i) for i in combination.value.split(',')]
2 changes: 1 addition & 1 deletion Scripts/Python/xAgeSDLBoolRespond.py
Expand Up @@ -84,7 +84,7 @@ def OnSDLNotify(self, VARname, SDLname, playerID, tag):
avatar = PtGetAvatarKeyFromClientID(playerID).getSceneObject()
except:
avatar = None
ff = False
ff = tag.lower() == "fastforward"
else:
avatar = None
ff = vmFastFwd.value
Expand Down