Adventure Game Studio | Forums

AGS Support => Modules, Plugins & Tools => Topic started by: A�rendyll (formerly Yurina) on Tue 04/04/2006 21:15:37

Title: Attack success probability
Post by: A�rendyll (formerly Yurina) on Tue 04/04/2006 21:15:37
Hello,

For my RPG, I'm also working with accuracy, so your attack could miss. In general I thought of 95% chance to get hit, 5% chance to deal no damage.

I know this can be done with bools, but how do I make it depend on percents what the outcome is?

~Yurina
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: DoorKnobHandle on Tue 04/04/2006 21:18:16
You just need to use the Random function for this...

Example code:


bool attack_success; // true if it was a success, false if it was a miss

if ( Random ( 19 ) == 19 )
// 5 percent chance
{
   attack_success = false;
}
else
// 95 percent rest chance
{
   attack_success = true;
}


This code is absolutely untested, but it should definately work. Ask if you have questions!
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: Radiant on Tue 04/04/2006 23:17:27
Or, simply,


bool attack_success = (Random (19) == 0);

Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: RickJ on Tue 04/04/2006 23:21:34
Or better yet ...

bool attack_success = (Random (99) <5);


IMHO, this makes it clearer what the %success is and easier to adjust, just replace 5 with the desired success rate.



Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: Khris on Wed 05/04/2006 00:31:59
Quote from: Yurina on Tue 04/04/2006 21:15:37I know this can be done with bools, but how do I make it depend on percents what the outcome is?
Not exactly.
A bool is the most simple kind of variable, as it holds either true oder false.

But you don't need a bool to work with chances (at least not the way I guess you're thinking of).

Especially because in an RPG the outcome won't stay the same, it doesn't make sense to use a variable at all.

You'll want something like this:
function attack_success(int chance_of_hit) {
Ã,  if (Random(99)<chance_of_hit) return true;
Ã,  else return false;
}

...

if (attack_success(95)) {
Ã,  // reduce foe's HP, etc...
}
else
Ã,  Display("You missed!");


Of course it's possible (and elegant) to write the outcome of a condition right into a bool, but Yurina is a variable beginner, so let's not trick her into thinking that writing a single line of code will provide her with a constantly changing attack_success bool ;)
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: fovmester on Wed 05/04/2006 07:12:38
Quote
function attack_success(int chance_of_hit) {
  if (Random(99)<chance_of_hit) return true;
  else return false;
}

Seems like you're doing the same thing twice here.
Better:


function attack_success(int chance_of_hit) {
  return Random(99)<chance_of_hit;
}

Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: Khris on Wed 05/04/2006 10:26:31
Yes, true, but considering that Yurina has posted this thread (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=25834.0) only recently, I wanted to keep things as readable and simple as possible.
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: RickJ on Wed 05/04/2006 15:02:10
I like fovmester's idea best and would add have it return a bool if AGS allows it? 

bool attack_success(int chance_of_hit) {
  return Random(99)<chance_of_hit;
}



Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: monkey0506 on Wed 05/04/2006 17:12:14
IMO it's better to show them how to code properly instead of just giving them the simple, easy to understand answer.  If you take the time to explain it, she'll understand it, and be capable of programming things (better) by herself.

And AGS can return any of the types built into it (except managed types (it can return pointers to these types)), and it is logically best to do so instead of using the word function (for the same reasons as the above).  Furthermore, returning old-style strings isn't technically "supported", although there are several documented cases of it working (returning new-style Strings is supported).
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: A�rendyll (formerly Yurina) on Thu 06/04/2006 20:59:13
I'll try this out when I have the time and recources to do so. I always ask questions ahead so I don't get frustrated because I have to wait too long for an answer (I'm not impatient, and not in denial! :=).

Quote from: KhrisMUC on Wed 05/04/2006 00:31:59
Yurina is a variable beginner
You're right about that... :P
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: fovmester on Wed 12/04/2006 14:48:54
Quote from: monkey_05_06 on Wed 05/04/2006 17:12:14
And AGS can return any of the types built into it (except managed types (it can return pointers to these types)), and it is logically best to do so instead of using the word function (for the same reasons as the above).Ã,  Furthermore, returning old-style strings isn't technically "supported", although there are several documented cases of it working (returning new-style Strings is supported).

So you don't have to specify that it's a function in AGS? Just the return value like in Java/C? Didn't know that...
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: Nickenstien on Thu 13/04/2006 19:56:00
Moderator - Please delete this  (my computer went mad and posted it when i wasnt finished, I didnt realise it actualy got through), sorry

Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: A�rendyll (formerly Yurina) on Thu 13/04/2006 19:59:19
I'll try out both ways, but I still don't have a fight room or a GUI which makes fighting possible... I have to look into making one for testing... >.<

~Yurina
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: Nickenstien on Thu 13/04/2006 20:00:05
there realy is no need to use a function here, what is the point of a function that performs one line of code ?

surely the easiest thing to do  is: -


if (!(Random(19)))
{

// do stuff for a sucsesfull hit

}
else
{

// do stuff for an unsucsesfull hit
}


using  (!(Random(19))   will mean the following code block will be exicuted if the result of the expression is FALSE  (IE Zero) so its 5%.

using 9, would be a 10% percent chance and using 99 would be a 1% chance etc, realy no need for a function here.   :)
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: A�rendyll (formerly Yurina) on Fri 14/04/2006 18:34:13
Thanks, Nickenstein. This saves a lot of trouble for me.

~Yurina
Title: Re: ...% chance for a bool to be true, ...% chance to be false?
Post by: Wretched on Fri 14/04/2006 19:51:53
2 Reasons for using a function.
1) The code becomes 'readable' so it is clear exactly what is happening.
2) Suppose at a future date you want to change all the attack percentages to make the game easier or harder, with a function you can do this by changing just one line of code.