Attack success probability

Started by A�rendyll (formerly Yurina), Tue 04/04/2006 21:15:37

Previous topic - Next topic

A�rendyll (formerly Yurina)

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
Yuna: Give me a Y!
Rikku: Give me an R!
Paine: Give me a break...
~Final Fantasy X-2

I've been

DoorKnobHandle

You just need to use the Random function for this...

Example code:

Code: ags

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!

Radiant

Or, simply,

Code: ags

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


RickJ

Or better yet ...
Code: ags

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.




Khris

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:
Code: ags
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 ;)

fovmester

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:

Code: ags

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


Khris

Yes, true, but considering that Yurina has posted this thread only recently, I wanted to keep things as readable and simple as possible.

RickJ

I like fovmester's idea best and would add have it return a bool if AGS allows it? 
Code: ags

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




monkey0506

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).

A�rendyll (formerly Yurina)

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
Yuna: Give me a Y!
Rikku: Give me an R!
Paine: Give me a break...
~Final Fantasy X-2

I've been

fovmester

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...

Nickenstien

Moderator - Please delete this  (my computer went mad and posted it when i wasnt finished, I didnt realise it actualy got through), sorry


A�rendyll (formerly Yurina)

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
Yuna: Give me a Y!
Rikku: Give me an R!
Paine: Give me a break...
~Final Fantasy X-2

I've been

Nickenstien

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.   :)

A�rendyll (formerly Yurina)

Thanks, Nickenstein. This saves a lot of trouble for me.

~Yurina
Yuna: Give me a Y!
Rikku: Give me an R!
Paine: Give me a break...
~Final Fantasy X-2

I've been

Wretched

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.

SMF spam blocked by CleanTalk