I'm attempting to implement a script that is akin to Spartan Rage from God of War. I have a counter that is supposed to increment each time you take damage, then once it reaches 100, it ticks down until it reaches 0 again. Whenever I get hit, the counter just ticks all the way up to 99 and then freezes there.
import "std.zh"
global script Active
{
void run()
{
bool rage = false;
int lasthp = Link->HP;
while (true)
{
moblinRage(rage, lasthp);
Waitframe();
}
}
}
void moblinRage(bool rage, int lasthp){
if(Link->HP < lasthp)
{
Game->Counter[CR_SCRIPT1] += (lasthp - Link->HP)/2;
lasthp = Link->HP;
}
if(Game->Counter[CR_SCRIPT1] > 100)
{
Game->Counter[CR_SCRIPT1] = 100;
}
if(Game->Counter[CR_SCRIPT1] < 0)
{
Game->Counter[CR_SCRIPT1] = 0;
}
if(Game->Counter[CR_SCRIPT1] == 0)
{
rage = false;
}
if(Game->Counter[CR_SCRIPT1] == 100)
{
rage = true;
}
if(rage == true)
{
Game->Counter[CR_SCRIPT1]--;
}
}
The other option here, is to use a script-local array. The array remains accessible as long as the script is active, so you can pass its pointer by reference and use it, thus:
const int RAGE_ACTIVE = 0;
const int RAGE_LASTHP = 1;
global script Active
{
void run()
{
int rageinfo[2] = {0,(Link->HP*0.5)}; //active, lasthp
while (true)
{
moblinRage(rageinfo);
Waitframe();
}
}
}
bool moblinRage(int ptr)
{
if(Link->HP < ptr[RAGE_LASTHP] )
{
if ( !ptr[RAGE_ACTIVE] ) //might want this, too
//Game->Counter[CR_SCRIPT1] += (ptr[RAGE_LASTHP] - Link->HP)/2; //might want to rethink this calc
Game->Counter[CR_SCRIPT1] = (Link->HP*0.5) * 60; //Half of his HP in seconds
}
if(Game->Counter[CR_SCRIPT1] > 99 )
{
if (!ptr[RAGE_ACTIVE])
{
ptr[RAGE_ACTIVE] = 1;
return true;
}
else
{
Game->Counter[CR_SCRIPT1] = 99;
}
}
if ( ptr[RAGE_ACTIVE] )
{
if ( Game->Counter[CR_SCRIPT1] ) //reduce if > 0
{
--Game->Counter[CR_SCRIPT1];
}
else
{
ptr[RAGE_ACTIVE] = 0; //ran out of rage bonus, so clear
ptr[RAGE_LASTHP] = (Link->HP*0.5); //reset min HP
}
}
return (ptr[RAGE_ACTIVE]);
}
I also condensed and optimised your if chain, but I see a possible flaw logic. See my code comments.