Skip to content

Commit

Permalink
apoogioflagStay
Browse files Browse the repository at this point in the history
  • Loading branch information
atupone committed Mar 28, 2024
1 parent f9acb58 commit 9261f20
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
3 changes: 2 additions & 1 deletion python/bzEvents.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ def __init__(self, flagType):
self.flagType = flagType

class bz_FlagGrabbedEventData_V1 (bz_EventData):
def __init__(self, flagType):
def __init__(self, flagType, pos):
super().__init__(bz_eFlagGrabbedEvent)
self.flagType = flagType
self.pos = pos

class bz_FlagDroppedEventData_V1 (bz_EventData):
def __init__(self, flagType):
Expand Down
2 changes: 1 addition & 1 deletion python/bzfsItf.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def handleDefaultOptions(self, data):
'WARNING: The "BBOX" attribute has been deprecated.'\
' Please use the `position` and `size` '\
'attributes instead:')
bz_debugMessage(0, ' position {f} {f} {f}'
bz_debugMessage(0, ' position {} {} {}'
.format(self.cX, self.cY, self.cZ))
bz_debugMessage(0, ' size {} {} {}'
.format(self.hw, self.hh, self.height))
Expand Down
91 changes: 91 additions & 0 deletions python/flagStay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from bzfsItf import *

class FlagStayZone(bz_CustomZoneObject):
message = ''
flagList = []

def checkFlag (self, flag):
return flag in self.flagList

zoneList = []

class FlagStay (bz_Plugin, bz_CustomMapObjectHandler):

def Name (self):
return 'Flag Stay Zones'
def __init__ (self, _):
self.playerIDToZoneMap = {}

bz_registerCustomMapObject('FLAGSTAYZONE', self)

self.Register(bz_eFlagGrabbedEvent)
self.Register(bz_ePlayerUpdateEvent)

def Cleanup (self):
self.Flush()

bz_removeCustomMapObject('FLAGSTAYZONE')

def Event (self, eventData):
if eventData.eventType == bz_eFlagGrabbedEvent:
flagGrabData = eventData

for zone in zoneList:
if not zone.pointInZone(flagGrabData.pos):
continue
if not zone.checkFlag(flagGrabData.flagType):
continue
self.playerIDToZoneMap[flagGrabData.playerID] = zone
break
elif eventData.eventType == bz_ePlayerUpdateEvent:
updateData = eventData
playerID = updateData.playerID

if playerID not in self.playerIDToZoneMap:
return

pos = updateData.state.pos

flagAbrev = bz_getPlayerFlag(playerID)

if not flagAbrev:
del self.playerIDToZoneMap[playerID]
else:
zone = self.playerIDToZoneMap[playerID]

if not zone.pointInZone(pos):
bz_removePlayerFlag(playerID)
del self.playerIDToZoneMap[playerID]

if zone.message.size():
bz_sendTextMessage(playerID, zone.message)

def MapObject (self, obj, data):
if obj != 'FLAGSTAYZONE':
return False
if not data:
return False

newZone = FlagStayZone()
newZone.handleDefaultOptions(data)

# parse all the chunks
for line in data:
nubs = bz_tokenize(line)

if len(nubs) <= 1:
continue

key = nubs[0].upper()

if key == 'FLAG':
flag = nubs[1].upper()
newZone.flagList.append(flag)
elif key == 'MESSAGE' or key == 'MSG':
newZone.message = nubs[1]

zoneList.append(newZone)

return True

plugin = FlagStay('')
19 changes: 13 additions & 6 deletions src/bzfs/bzfsPython.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1084,14 +1084,21 @@ static PyObject *genEvent (bz_FlagTransferredEventData_V1 &data)

static PyObject *genEvent (bz_FlagGrabbedEventData_V1 &data)
{
PyObject *pArg = PyUnicode_FromString(data.flagType);
if (!pArg)
PyObject *pArgs = PyTuple_New(2);
if (!pArgs)
return nullptr;

PyObject *pEvent = PyObject_CallFunctionObjArgs(flagGrabbedEventData_V1,
pArg,
NULL);
Py_DECREF(pArg);
PyObject *pPos = PyTuple_New(3);
if (!pPos)
return nullptr;
for (int i = 0; i < 3; i++)
PyTuple_SetItem(pPos, i, PyFloat_FromDouble(data.pos[i]));

PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(data.flagType));
PyTuple_SetItem(pArgs, 1, pPos);

PyObject *pEvent = PyObject_CallObject(flagGrabbedEventData_V1, pArgs);
Py_DECREF(pArgs);
return pEvent;
}

Expand Down

0 comments on commit 9261f20

Please sign in to comment.