Multiple buffs/debuffs with various times

Please post any questions about developing your plugin here. Please use the search function before posting!
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Multiple buffs/debuffs with various times

Postby arawra » Sun Jan 11, 2015 5:12 am

As a continuation of my other thread, I would like to create a timer for various effects for players. Effects include things like healing, poisons, slows, etc etc. I think I've found an effective way to accomplish this and would like input on my implementation and API.

Syntax: Select all

statusEffectsList = ['drug','regen','stun']
statusEffects = {}
for status in statusEffectsList:
statusEffects[status] = []

def heal(player, amount):
# player is a PlayerEntity/dndPlayer object
player.health += amount
if player.health > player.maxhp:
player.health = player.maxhp

def stun():
raise NotImplementedError('Not yet included')

def drug():
raise NotImplementedError('Not yet included')

def dndTick():
for players in PlayerGenerator():
myPlayer = dndPlayerDictionary[userid_from_playerinfo(players)]
if not myPlayer.isdead:
for item in myPlayer.statusEffects:
for instance in myPlayer.statusEffects[item]

if myPlayer.statusEffects[item][instance]['effect']:
if dndTimer.count >= myPlayer.statusEffects[item][instance]['duration'] and dndTimer.count % myPlayer.statusEffects[item][instance]['duration'] == 0:

if myPlayer.statusEffects[item][instance]['iteration'] != None
if myPlayer.statusEffects[item][instance]['iteration'] > 0:
myPlayer.statusEffects[item][instance]['iteration'] -= 1
globals()[item]()
else:
myPlayer.statusEffects[item][instance].pop
else:
globals()[item]()

@Event
def player_spawn(game_event):
dndTimer.reset()

spawnee_userid = game_event.get_int('userid')
spawnee = dndPlayerDictionary[spawnee_userid]

#This just sets all attributes of PlayerEntity() to their default value, i.e. max health is 100 and speed is 100%
#dndPlayer object also has an attribute of statusEffects and dndPlayer.reset sets all keys to an empty list
spawnee.reset()

if spawnee.someAttribute = True:

#Player should regenerate 1 health every 4 seconds
spawnee.statusEffects['regen'].append({'duration':3,'effect':5,'intervals':3})

#Player should regenerate 5 health every 3 seconds, but only for 3 intervals
spawnee.statusEffects['regen'].append({'duration':3,'effect':5,'intervals':3})

dndTimer = TickRepeat(dndTick)
dndTimer.start(1,0)
User avatar
L'In20Cible
Project Leader
Posts: 1536
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Sun Jan 11, 2015 7:28 am

I didn't check the entire code but something I noticed.

Syntax: Select all

def dndTick():
for players in PlayerGenerator():
myPlayer = dndPlayerDictionary[userid_from_playerinfo(players)]
if not myPlayer.isdead:
Why not using PlayerIter?

Syntax: Select all

from filters.players import PlayerIter

def dndTick():
for userid in PlayerIter('alive', return_types='userid'):
myPlayer = dndPlayerDictionary[userid]
User avatar
satoon101
Project Leader
Posts: 2703
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Jan 11, 2015 2:48 pm

Also, instead of making functions where you pass the player's instance, I would rather extend the player class to include the functions as methods. I will try to remember to provide an example when I get home tonight.
Image
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Sun Jan 11, 2015 8:25 pm

As satoon mentioned, you should extend PlayerEntity class for something like this.

Syntax: Select all

def heal(player, amount):
# player is a PlayerEntity/dndPlayer object
player.health += amount
if player.health > player.maxhp:
player.health = player.maxhp

Can be turned into:

Syntax: Select all

class DndPlayer(PlayerEntity):
"""DndPlayer extends PlayerEntity to implement bonus effects like healing."""

def heal(self, amount):
"""Heals player for 'amount' health."""
self.health += amount
if self.health > self.maxhp:
self.health = self.maxhp

And now instead of using PlayerEntity, you use DndPlayer and just say

Syntax: Select all

my_player = DndPlayer(player_index)
my_player.heal(15)


Now you can fill your dndPlayerDictionary with DndPlayers, or if it's already filled with some custom class that needs to work in a different way than PlayerEntity class does (f.e. sustain through deaths etc.), you can just add a 'get_entity' method, or similar, into the other class.
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Sun Jan 18, 2015 1:48 am

If I were to implement the methods as part of the class, could I call them using a variable as such?

Syntax: Select all

globals()[myPlayer.item]()
User avatar
satoon101
Project Leader
Posts: 2703
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Jan 18, 2015 2:22 am

More like:

Syntax: Select all

getattr(myPlayer, item)()
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 111 guests