Help with item stats calculation

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Nero
Veteran
Posts: 242
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Help with item stats calculation

#1 Post by Nero » Fri Dec 02, 2016 8:51 am

Hey guys!

So I made items that gives health/armor bonuses when equipped now I'm little bit lost here I made armor and necklace with defined stats problem is how do I make them to act independently? For example I have (100 HP) now I equip armor(50) and now I have (150 HP) but when I equip the necklace(20) my armor stats get lost and I have (120HP) instead of (170) . I understand that it happens because EXTRA_HP = item value but how do I make that it stack itself?

Here is my current code for this note that I removed unnecessary variables from Player class:

Code: Select all

init -1 python:
    class Player(renpy.store.object):
        def __init__(self,NAME, MAX_HP, CURRENT_HP,  IMAGE = "NO IMAGE", ARMOR= None, EXTRA_HP=0, WEAPON=None, PLAYER_MAX_DAMAGE=0, PLAYER_MIN_DAMAGE=0, EXTRA_MAX_HP=0, 
            MAX_ARMOR_PROTECTION = 0,MIN_ARMOR_PROTECTION=0, NECKLACE=None, MAX_NECKLACE_PROTECTION=0, MIN_NECKLACE_PROTECTION=0):       

            self.NAME = NAME ### Player name
            self.MAX_HP = MAX_HP ### Players max hp
            self.CURRENT_HP = CURRENT_HP ### Players current hp
            self.ARMOR = ARMOR ### Name of armor
            self.EXTRA_HP = EXTRA_HP ### Used for the sum calculation with CURRENT_HP
            self.EXTRA_MAX_HP = EXTRA_MAX_HP ### Used for the sum calculation with MAX_HP
            self.MAX_ARMOR_PROTECTION = MAX_ARMOR_PROTECTION ### Highest roll on protection
            self.MIN_ARMOR_PROTECTION = MIN_ARMOR_PROTECTION ### Lowest roll on protection
            self.NECKLACE = NECKLACE ### Name of necklace
            self.MAX_NECKLACE_PROTECTION= MAX_NECKLACE_PROTECTION ### Highest roll on protection
            self.MIN_NECKLACE_PROTECTION = MIN_NECKLACE_PROTECTION ### Lowest roll on protection   
    
        @property
        def TOTAL_MAX_HP(self): ### Used in battle engine just to display the max health
            return self.MAX_HP + self.EXTRA_MAX_HP

        @property
        def TOTAL_CURRENT_HP(self): ### Used in battle engine when taking damage
            return self.CURRENT_HP + self.EXTRA_HP

        @TOTAL_CURRENT_HP.setter
        def TOTAL_CURRENT_HP(self, value):            
             self.CURRENT_HP and self.EXTRA_HP - 0    
        
        def equip(self, item): 
            if  item.PLAYER_EQ == "Bob" and item.EQUIPMENT_TYPE == "ARMOR":
                self.ARMOR = item.NAME               
                self.EXTRA_HP = item.ARMOR_HP_BONUS
                self.EXTRA_MAX_HP = item.ARMOR_HP_BONUS
                bob.MAX_ARMOR_PROTECTION = item.MAX_ARMOR_PROTECTION
                bob.MIN_ARMOR_PROTECTION = item.MIN_ARMOR_PROTECTION
                renpy.show_screen("inventory_popup", message="You equiped [bob.ARMOR].")            
                
            elif item.PLAYER_EQ == "Bob" and item.EQUIPMENT_TYPE == "NECKLACE":
                self.NECKLACE = item.NAME                
                self.EXTRA_HP = item.NECKLACE_HP_BONUS
                self.EXTRA_MAX_HP = item.NECKLACE_HP_BONUS
                bob.MAX_NECKLACE_PROTECTION = item.MAX_NECKLACE_PROTECTION
                bob.MIN_NECKLACE_PROTECTION = item.MIN_NECKLACE_PROTECTION
                renpy.show_screen("inventory_popup", message="You equiped [bob.NECKLACE].") 
                
            else:
                renpy.show_screen("inventory_popup", message="You can't use this item!")     
     
    class Item(store.object):
        def __init__(self, NAME, COST=0, KG=0, MAX_DAMAGE=0, MIN_DAMAGE=0, AMOUNT=0, DESC="No Desc", PICTURE = "", WEAPON_EQUIP="", PLAYER_EQ=None, ARMOR_EQUIP="", 
            ARMOR_HP_BONUS=0, TYPE=None,EQUIPMENT_TYPE=None, MAX_ARMOR_PROTECTION=0,MIN_ARMOR_PROTECTION=0, MAX_NECKLACE_PROTECTION=0, MIN_NECKLACE_PROTECTION=0, NECKLACE_HP_BONUS=0):
            self.NAME = NAME ### Item name
            self.COST = COST ### Item cost
            self.KG = KG ### How heavy is item inside inventory?
            self.MAX_DAMAGE = MAX_DAMAGE ### Highest damage roll
            self.MIN_DAMAGE = MIN_DAMAGE ### Lowest damage roll
            self.AMOUNT = AMOUNT ### Item amount
            self.DESC = DESC ### Item description
            self.PICTURE = PICTURE ### Item picture
            self.PLAYER_EQ = PLAYER_EQ ### Which players can carry this tiem?
            self.ARMOR_HP_BONUS = ARMOR_HP_BONUS ### How much extra HP does armor give when equipped?
            self.ARMOR_NAME = TYPE ### Name of equippment that will be displayed on the screen
            self.EQUIPMENT_TYPE = EQUIPMENT_TYPE ### Is the item weapon,armor or accessory?
            self.MAX_ARMOR_PROTECTION = MAX_ARMOR_PROTECTION ### Highest armor prot roll
            self.MIN_ARMOR_PROTECTION = MIN_ARMOR_PROTECTION ### Lowest armor prot roll
            self.MAX_NECKLACE_PROTECTION= MAX_NECKLACE_PROTECTION ### Highest necklace prot roll
            self.MIN_NECKLACE_PROTECTION = MIN_NECKLACE_PROTECTION ### Lowest necklace prot roll
            self.NECKLACE_HP_BONUS = NECKLACE_HP_BONUS ### How much hp does necklace give? 

User avatar
Pomeranian
Regular
Posts: 33
Joined: Wed Oct 12, 2016 6:52 pm
Contact:

Re: Help with item stats calculation

#2 Post by Pomeranian » Fri Dec 02, 2016 10:35 am

I believe you need to make it so that extra_hp is equal to item.armor_hp_bonus + item.necklace_hp_bonus (with two seperate items, obvs)

Personally I would completely rewrite it to account for multiple equipment slots - the way I wrote my code was originally intended for just the one type of dress so when you try to use my code as a base there has to be a lot of repeated code to account for the changes, but I hope this helps a little.

Nero
Veteran
Posts: 242
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: Help with item stats calculation

#3 Post by Nero » Fri Dec 02, 2016 12:47 pm

Was thinking about this but it kind of seems like a really noob way of doing it and the code would be even more messy when I add new item types to it. I will try to do it the other more practical way then.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Help with item stats calculation

#4 Post by xela » Fri Dec 02, 2016 10:58 pm

Code: Select all

@TOTAL_CURRENT_HP.setter
        def TOTAL_CURRENT_HP(self, value):           
             self.CURRENT_HP and self.EXTRA_HP - 0
this doesn't do anything meaningful... what happens when you do:

Code: Select all

player.TOTAL_CURRENT_HP -= 10
is that value in the function is the sum of self.CURRENT_HP, self.EXTRA_HP and the -10. First a check if the value is smalleror equals to 0 in in order, cause the player is dead if it is.

If player is alive you prolly want to get to "-10" first, something like:

Code: Select all

value = value - self.CURRENT_HP - self.EXTRA_HP
Than if this new value is greater than 0 (like a magical buff/bonus to total health), do something like splitting it between the two and if it's smaller (damage/penalty), drain the self.EXTRA_HP first and self.CURRENT_HP second. Or maybe drain them proportionally to their own values, I am not sure what your plan here is.

Stacking is really simple, you just:

Code: Select all

self.EXTRA_HP += item.ARMOR_HP_BONUS
instead of:

Code: Select all

self.EXTRA_HP = item.ARMOR_HP_BONUS
but it's a weird and confusing setup. Usually defense got nothing to do with health, this kind of code is annoying to write cause it's confusing and we all should focus on doing easy things instead of hard things :)
Like what we're doing? Support us at:
Image

Nero
Veteran
Posts: 242
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: Help with item stats calculation

#5 Post by Nero » Sat Dec 03, 2016 9:45 am

@TOTAL_CURRENT_HP.setter
def TOTAL_CURRENT_HP(self, value):
self.CURRENT_HP and self.EXTRA_HP - 0


this doesn't do anything meaningful... what happens when you do:
But for some reason it must be there because my battle engine code gives me error "Wrong attribute(something like that)" ... Those setters and @property are little bit confusing they are new to me.

Yes I tested that but thing is I have a screen textbutton that uses this function and if I use this code

Code: Select all

self.EXTRA_HP += item.ARMOR_HP_BONUS
each time I press the button it stacks infinitely because it just keeps adding same value over and over. Is there any way to block stacking after one mouse click on the button something like "cheat" :D? I can think of one way doing it with 2 text buttons equip and de-equip that appear and disappear dependent if item is equipped or not. But I would like to know other ways too.

My ides and imagination keeps growing larger and larger every morning :lol: . I think about that stuff and I have the need of making it even if its pain to make. So far everything works as I imagined its slow progress but the feeling when your program works is just too great 8) I'm so inspired by those 2 famous VN like "Loren the amazon princess" and "Sunrider" with all expansions. Its impressive what can be done.

Ocasota
Newbie
Posts: 10
Joined: Sat Nov 26, 2016 11:44 pm
Contact:

Re: Help with item stats calculation

#6 Post by Ocasota » Wed Dec 07, 2016 1:43 am

more than you ever wanted to know about @property and getter/setter functions in Python:

https://www.programiz.com/python-programming/property

I'd agree with Pomeranian, add a capability for extra equip slots.

that would also prevent the stacking cheat problem because you can only equip the armor once. don't mod the stat when you put it on, calculate it on the fly each time it's needed.

when getting your current hp or max_hp, loop through all your equipped items, sum up any HP bonuses, add to current base value hp and return that. hit damage reduces only your base value hp.

is is why getters/setters are cool, you can hide complex code like this behind what looks like a simple variable reference to "player.hp"

Post Reply

Who is online

Users browsing this forum: No registered users