PDA

View Full Version : Random Duration Invisibility:


So_1337
10-05-2009, 01:35 PM
Greetings. I've recently started trying to learn my way around the source and to see if I can become more of an asset around here. I've spent some time recently trying to make the TZVZ code for random duration invisibility work, and have come up short.

There are a few things that are holding me back, really, and I was hoping a more experienced coder could help me understand them. Let me start with a code block to use for reference (in client.cpp, this is inserted at line 3242):

void Client::DoInvisUpkeep() { //Null: Invis Fading Function
int Invis_Skill = GetSkill(DIVINATION);
int Invis_MaxSkill = MaxSkill(DIVINATION);
int break_Chance = 0.95*(Invis_Skill/Invis_MaxSkill); //Null: 5% chance to break with max skill.
int rngNum = 0; //This is the first point I will reference
Message(15, "Skill: %i Max Skill: %i Break Chance: %i", Invis_Skill, Invis_MaxSkill, break_Chance); //Debugging purposes
if(invisible == true) {
switch(Invis_fade) {
case 2:
BuffFadeByEffect(SE_Invisibility);
Invis_fade = -1;
invisible = false;
break;
case -1:
rngNum = MakeRandomInt(0, 100);
Message(13, "RNG: %i", rngNum); //Debugging, show the chance that was rolled
if (rngNum > (break_Chance * 100)) {
Invis_fade++;
Message(13, "You feel yourself starting to appear.");
}
break;
case 1:
case 0:
Invis_fade++;
break;
default:
break;
}
}

if(invisible_animals == true) {
switch(IVA_fade) {
case 2:
BuffFadeByEffect(SE_Invisibility);
IVA_fade = -1;
invisible_animals = false;
break;
case -1:
rngNum = MakeRandomInt(0, 100);
if (rngNum > (break_Chance * 100)) {
IVA_fade++;
Message(13, "You feel yourself starting to appear.");
}
break;
case 1:
case 0:
IVA_fade++;
break;
default:
break;
}
}

if(invisible_undead == true) {
switch(IVU_fade) {
case 2:
BuffFadeByEffect(SE_Invisibility);
IVU_fade = -1;
invisible_undead = false;
break;
case -1:
rngNum = MakeRandomInt(0, 100);
if (rngNum > (break_Chance * 100)) {
IVU_fade++;
Message(13, "You feel yourself becoming more visable.");
}
break;
case 1:
case 0:
IVU_fade++;
break;
default:
break;
}
}
}
The errors I'm receiving have to do with undeclared identifiers, and I know it's because Invis_fade, IVA_fade, and IVU_fade weren't declared with a data type. However, I can't figure out the proper way to do so. Inserting each of them with just "int Invis_fade = -1" at the reference point I commented in the code above would reset the values to -1 every time the code is called, making it worthless. However, just declaring them and leaving them would leave them null, so there'd be no way to trigger the cases in the first place.

What I have currently is a mix of Null's original code and what I've changed to get it closer to working (such as making the "Invis_Skill" and "Invis_MaxSkill" checks actually check Divination; they were returning wildly absurd values previously, because they didn't point to anything.)

I know that I'm probably way out of my depth here, but this was code that I sure wanted to see make it into the source after TheLieka released it to the public, and I'm using it to learn as I go.

I've gotten it to compile a few times just by declaring the variables at the beginning of the function, but as I said, that really had no effect, as the cases were never triggered. (I didn't paste the small snippets of code from client.h and client_process.cpp that call this function in the first place, but the code was definitely being called after invis was applied; I just can't get the check and fade working yet).

Thanks for any pointers that can be offered. I wouldn't be offended if someone took this and ran with (in fact, I'd feel very good knowing that it was coded properly by someone with experience), but I'm trying to pitch in around here, so a reply to this post doesn't mean I'll expect you to write the code for me. No obligations here! Thanks for reading.

Pend
10-05-2009, 02:19 PM
Haven't looked in any detail at what you've done, but a quick answer to your query about where to declare your variables is this: If you want them to be persistent, the most likely place you'll want to declare them is as member variables of the parent object--in this case in "class Client {" in client.h. But don't forget to initialize them to some known valid value in the constructor!

But without looking into it in more detail, I'm not convinced that Client is where you want this to be (there might be a Spell or SpellEffect class that persists as long as the spell is in effect, or a Player class that contains a list of currently active effects--if either exists, those would be more appropriate place to have this data retained).

So_1337
10-05-2009, 02:32 PM
Actually, I had the same thoughts when I first went sifting through the VZTZ source for the code. Spells.cpp and spell_effects.cpp seemed the most likely candidates, but I didn't find any of the code there. Since I'm still new to this, all I know is what I can learn to do by example, really, and I'm only building off of what was already in place. It's daunting enough stepping into this sandbox where I know there are coders who are very concerned about structuring everything properly, I guess I picked a bad example to start from, then =X

I'll see if I can figure out what you mean about putting them in the parent object. I noticed a lot of the other code was structured that same way, but I was a little too afraid to jump in and try it that way. Thanks for the tips!

nilbog
10-05-2009, 04:06 PM
All the credit for this comes from Ladoth, from project 1999, not myself :o
Here is how we have it set up. Still more work to be done when we hit kunark.. because there are full duration invisibilities, but I hope this helps you out!


around line 100 in spdat.cpp

///////////////////////////////////////////////////////////////////////////////
// spell property testing functions

// @LADOTH 07182009 Is invis type spell
bool IsInvisibilitySpell(int16 spell_id)
{
bool bResult = false;

if( IsValidSpell(spell_id) &&
(IsEffectInSpell(spell_id, SE_Invisibility) ||
IsEffectInSpell(spell_id, SE_Invisibility2) ||
IsEffectInSpell(spell_id, SE_InvisVsAnimals) ||
IsEffectInSpell(spell_id, SE_InvisVsUndead) ||
IsEffectInSpell(spell_id, SE_InvisVsUndead2))
)
{
bResult = true;
}

return bResult;
}

around line 610 of spdat.h

#endif
// @LADOTH 07182009 Is invis type spell
bool IsInvisibilitySpell(int16 spell_id);
bool IsTargetableAESpell(int16 spell_id);

around line 500 in spell_effects.cpp

#ifdef SPELL_EFFECT_SPAM
snprintf(effect_desc, _EDLEN, "Invisibility");
#endif
// solar: TODO already invis message and spell kill from SpellOnTarget
SetInvisible(true);
// @LADOTH 07252009 randomize time remaining for invis type spell
float randscale = MakeRandomInt(1, 100) / 100.0f;
buffs[buffslot].ticsremaining *= randscale;

around line 520 in spell_effects.cpp

#ifdef SPELL_EFFECT_SPAM
snprintf(effect_desc, _EDLEN, "Invisibility to Animals");
#endif

// @LADOTH 07252009 randomize time remaining for invis type spell
float randscale = MakeRandomInt(1, 100) / 100.0f;
buffs[buffslot].ticsremaining *= randscale;

around line 540 in spell_effects.cpp

#ifdef SPELL_EFFECT_SPAM
snprintf(effect_desc, _EDLEN, "Invisibility to Undead");
#endif
// @LADOTH 07252009 randomize time remaining for invis type spell
float randscale = MakeRandomInt(1, 100) / 100.0f;
buffs[buffslot].ticsremaining *= randscale;

KLS
10-05-2009, 04:19 PM
Honestly you were overthinking it just a bit.

Easiest and most accurate implementation:

on tic if tics > 2 then a % chance to reduce tics to 2 and update buffduration client side.

So_1337
10-05-2009, 04:30 PM
Is that reply to Ladoth's code, or to Null's variation that I've been working on, KLS? If in regards to Null's, I actually liked that casting skill level (comparative to the skill cap) played a role in his version, and he also included the various "You feel yourself starting to appear" messages for the different invisibility types and gave two tics from that point before it faded. The only part I disagreed with was that he'd had them yellow originally (message type 15), and I seem to remember them being red.

However, this was all commented out of the TZVZ code as "needs work", and Ladoth seems to have a working version active on Project 1999. Working beats non-working any day =)

Either way, thanks for looking over this, KLS, and thanks for the alternate idea, backwards goblin. I do intend to keep hammering away at this, and all of these ideas really help.

KLS
10-05-2009, 05:12 PM
The first mostly, the 2nd implementation has it's own problems as well.

KLS
10-06-2009, 02:39 AM
Hopefully you don't mind that I sorta hijacked this too much. Should be implemented tonight though.

So_1337
10-06-2009, 08:17 AM
Haha, not at all. I know you're very skilled and spread too thin, so if anything, thank you for taking time away from your other projects to check this out. I'll pull the latest code and give it a look, I'm sure I can learn a lot from it.

Besides, you have pretty high standards for what goes in (as well you should, same as anyone else who contributes). I'd much rather save my first true submission for when I can give a diff for something better than a hack job =P And don't take that as any insult to Null's code, it's self-directed, haha.

ChaosSlayerZ
10-08-2009, 03:40 PM
how does new code distinguish between random and static duration invis?
If I am to make a new custom invis spell, how do I ensure to get the properties of random or static duration - what governs it?

cavedude
10-08-2009, 05:37 PM
All standard invisible spells have a random chance to break. Improved spells are static. If you want a spell to be static, use effectid 314 for invis, or 315 for invis vs the undead. effectid 12, 28, or 29 (invis, ivu, iva) are all dynamic length.

KLS
10-08-2009, 06:54 PM
Static is SE_Invisibility2 or SE_InvisVsUndead2, look in spdat.h for the exact numbers on those.

cavedude
10-08-2009, 07:17 PM
314 and 315! :P

ChaosSlayerZ
10-08-2009, 07:50 PM
very well, thank you guys.
does Camo and Improved Camo also taken care of in similar way?

BTW I have noticed that there never was Improved invis vs Animals? I also noticed that in npc_types table there is no such property as "see invis vs animals", in case someone would want to make an animal in a similar manner you make undead