Author Topic: Attack success probability  (Read 3708 times)

A�rendyll (formerly Yurina)

  • Back from the dead.
Attack success probability
« on: 04 Apr 2006, 21:15 »
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
« Last Edit: 24 Apr 2006, 12:51 by strazer »
Yuna: Give me a Y!
Rikku: Give me an R!
Paine: Give me a break...
~Final Fantasy X-2

I've been

DoorKnobHandle

  • Mittens Serf
    • DoorKnobHandle worked on a game that was nominated for an AGS Award!
      DoorKnobHandle worked on a game that won an AGS Award!
You just need to use the Random function for this...

Example code:

Code: Adventure Game Studio
  1. bool attack_success; // true if it was a success, false if it was a miss
  2.  
  3. if ( Random ( 19 ) == 19 )
  4. // 5 percent chance
  5. {
  6.    attack_success = false;
  7. }
  8. else
  9. // 95 percent rest chance
  10. {
  11.    attack_success = true;
  12. }
  13.  

This code is absolutely untested, but it should definately work. Ask if you have questions!

Radiant

  • AGS Baker
  • Mittens Knight
  • Return once more to the Two Kingdoms!
    • I can help with publishing
    •  
    • I can help with story design
    •  
    • Radiant worked on a game that was nominated for an AGS Award!
      Radiant worked on a game that won an AGS Award!
Or, simply,

Code: Adventure Game Studio
  1. bool attack_success = (Random (19) == 0);
  2.  

RickJ

  • fix'n one thing and break'n two ...
    • I can help with scripting
    •  
    • I can help with story design
    •  
    • RickJ worked on a game that was nominated for an AGS Award!
Or better yet ...
Code: Adventure Game Studio
  1. bool attack_success = (Random (99) <5);
  2.  

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




Khris

    • Lifetime Achievement Award Winner
    •  
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
    • Khris worked on a game that was nominated for an AGS Award!
I 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: Adventure Game Studio
  1. function attack_success(int chance_of_hit) {
  2.   if (Random(99)<chance_of_hit) return true;
  3.   else return false;
  4. }
  5.  
  6. ...
  7.  
  8. if (attack_success(95)) {
  9.   // reduce foe's HP, etc...
  10. }
  11. else
  12.   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

  • I am slowly making progress...
    • I can help with translating
    •  
    • fovmester worked on a game that was nominated for an AGS Award!
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: Adventure Game Studio
  1. function attack_success(int chance_of_hit) {
  2.   return Random(99)<chance_of_hit;
  3. }
  4.  

Khris

    • Lifetime Achievement Award Winner
    •  
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
    • Khris worked on a game that was nominated for an AGS Award!
Yes, true, but considering that Yurina has posted this thread only recently, I wanted to keep things as readable and simple as possible.

RickJ

  • fix'n one thing and break'n two ...
    • I can help with scripting
    •  
    • I can help with story design
    •  
    • RickJ worked on a game that was nominated for an AGS Award!
I like fovmester's idea best and would add have it return a bool if AGS allows it? 
Code: Adventure Game Studio
  1. bool attack_success(int chance_of_hit) {
  2.   return Random(99)<chance_of_hit;
  3. }
  4.  



monkey0506

  • AGS Project Tracker Admins
  • Tasting the banhammer. Strangely, tastes like ham.
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).
User was banned for this post.

A�rendyll (formerly Yurina)

  • Back from the dead.
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! :=).

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

  • I am slowly making progress...
    • I can help with translating
    •  
    • fovmester worked on a game that was nominated for an AGS Award!
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

  • hello.
    • I can help with play testing
    •  
    • I can help with voice acting
    •  
Moderator - Please delete this  (my computer went mad and posted it when i wasnt finished, I didnt realise it actualy got through), sorry

« Last Edit: 13 Apr 2006, 20:01 by Nickenstien »

A�rendyll (formerly Yurina)

  • Back from the dead.
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

  • hello.
    • I can help with play testing
    •  
    • I can help with voice acting
    •  
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)

  • Back from the dead.
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

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.