1 (edited by Browed 2019-04-26 00:35:21)

Topic: [Code] Potions and how best to implement them

I have a question, I want to make all sorts of potions, both ordinary without functions and with them

Plans:
- Without Enums
- Without Enums name
- Any effect writing it, in GiveEffect
- Counting the number of effects is easy

Example GiveEffect("Dance", 10); and player got effect dance


Now i use, but I don't know if it's safe
Need advice on how to optimize this, and how to protect it
I am use std::map

There are no problems with the operation of this code.

player.cpp in tick

        if(Server()->Tick() % Server()->TickSpeed() == 0)
        {
            for(auto const& x : m_Effect)
            {
                if(m_Effect.find(x.first) != m_Effect.end())
                {
                    if(!strcmp(x.first, "poison"))
                    {
                        // effect poison
                    }
                    // idk
                    m_Effect[x.first]--;
                    if(m_Effect[x.first] <= 0)
                        m_Effect.erase(m_Effect.find(x.first));
                }
            }
        }

player.h

Private:
    std::map < const char * , int > m_Effect;
Public:
    void GiveEffect(const char* Effect, int Sec);

function:

void CPlayer::GiveEffect(const char* Effect, int Sec)
{
    m_Effect[Effect] = Sec;
}

example
http://priscree.ru/img/8158a8256105c9.png

2

Re: [Code] Potions and how best to implement them

This level of optimization is completely insignificant on the scale of Teeworlds.
Make code that works and is easy to read.

Not Luck, Just Magic.

3

Re: [Code] Potions and how best to implement them

Yes, it seems to work, and safety and leakage memory, I will see with time. Multi Buffs.
But I didn’t cope with const char * in key std::map

https://www.youtube.com/watch?v=vyXBP8KaQVU&t=55s

4 (edited by Browed 2019-06-02 14:59:44)

Re: [Code] Potions and how best to implement them

Why using std::map or std::list I see the correct operation, and when using "base/array.h", I see a shift in memory? I saw that the array always holds, but the list is empty. Maybe I'm using it wrong.

Is this the right use?

array<int> m_HidenMenu; or array <class , struct > template

If I perform operations

for(int i = 0; i < m_HidenMenu.size(); i++)
{
    if(m_HidenMenu{i} > NUMHIDEMENU && m_HidenMenu{i} != MenuID)
        m_HidenMenu.remove_index(i);
}
if(m_HidenMenu.finditem(MenuID))
    m_HidenMenu.remove(MenuID);
else
    m_HidenMenu.add(MenuID);

It all works "base/array.h" as it should, but the program begins to behave not as intended, lost pointers example i see const char *pName[] = { lalala }, how empty

Where at the other end of the code as soon as I interact with "base/array.h", later .add(item);
Debug and breakpoints i see in enter const char *pName equal as expected, when an interaction with it, it returns null

But in other cases, it will work correctly, example static

// Works, I missed another