Author Topic: chance events  (Read 835 times)

Offline Sagefire135

  • Level 14
  • *
  • Posts: 107
  • Reputation: +2/-0
    • View Profile
chance events
« on: July 19, 2009, 11:18:54 PM »
No, this isnt about a lottery, this is about events happening in a battle.

Say i want to have a X% chance for an attack to be a critical hit. what is the best way to have the code decide whether or not the hit will be critical?

i was thinking of making sure X was between 1 and 100 then choose a random number from 1-100 if the number is less than X its a crit, if its more than its not, in each case a variable is made to be used later depending on wether it was crit or not. This would certainly work, but something tells me that its a very sloppy way to go about making it happen...

Adding a function to go with it, would make it less messy in the actual code, but the function itself still looks like it could be improved

function critical($percent)  {
  $numb1 = rand(1, 100);
  IF ($numb1 <= $percent) {
  $crit = 1
  echo 'critical hit';
  } ELSE {
  $crit = 0
  echo 'not critical';
  }
}

Offline karnedge

  • Level 17
  • *
  • Posts: 170
  • Reputation: +4/-0
  • ctrlHack provides the server, you bring the skill.
    • View Profile
    • ctrl://Hack.game
Re: chance events
« Reply #1 on: July 19, 2009, 11:32:52 PM »
Usually critical percentage is from a player's stat or based off of a stat.
Example:
crit% = Luck * 0.2
bonus damage = crit + 5

In battle, for each attack, you have to have some base dmg that the player normal does:
dmg = atk - opponent's def

So, while you do that... you would do something like:
Code: [Select]
if (rand(1,100) <= [b]crit[/b]){
  [b]total[/b] dmg = ([b]dmg[/b] * [b]bonus[/b]) + [b]dmg[/b]
}

Something of that sort. I just wouldn't suggest such randomness as you are proposing. If you don't want to use a set stat then I would suggest some global set number and do a rand(1,n) that the number has to be below.

Or make it really rare occurance and have the rand() number have to match the damage done (then they get a bonus).
ctrlHack - Hacking simulation RPG in development.
Latest blog: Back on Track
bbgFramework v0.1.3

Offline Sagefire135

  • Level 14
  • *
  • Posts: 107
  • Reputation: +2/-0
    • View Profile
Re: chance events
« Reply #2 on: July 19, 2009, 11:47:11 PM »
Ya, obviously there would be other things going on that im leaving out right now lol. Im just looking at how to go about using chance. lets say the chance for a crit is 9% so i do a rand(1,100) anything from 1 to 9 would give a crit and 10-100 would not.

Having a block of 9 "good" numbers all at the beginning works, but wouldn't it be better having the 9 good numbers spread throughout all 100? so for example getting a 10, 20, 30, 40, 50, 60, 70, 80, or 90 would give the crit. does that make sense?

i just feel like a block of numbers is less random

EDIT: because i can...made a script that used blocks and counted how many crits and no crits in 100,000 rolls with 50 as the crit chance.
50007crits and 49992notcrits

i guess its random enough lol
« Last Edit: July 19, 2009, 11:56:13 PM by Sagefire135 »

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: chance events
« Reply #3 on: July 20, 2009, 12:25:11 AM »
Nope, there's no difference between having a block of 'good' numbers and having them all spread out :) Overall chance is still the same.

You could improve your function by making it more general-purpose. Right now it can only be used for critical hits, but what if you wanted to use it for other chance events?

function chance($percent)
{
    return (
rand(1,100) <= $percent);
}

if (
chance($critical))
{
    echo 
'critical hit!';
}
else
{
    echo 
'no critical hit';
}
« Last Edit: July 20, 2009, 12:26:53 AM by Zeggy »

Offline Sagefire135

  • Level 14
  • *
  • Posts: 107
  • Reputation: +2/-0
    • View Profile
Re: chance events
« Reply #4 on: July 20, 2009, 12:58:53 AM »
when the function says return (rand(1,100) <= $percent) what is that actually saying code wise in the following IF statement?

Also can return be used pull a variable out from a function? (obviously not a realistic example but...the idea is the same)

function var()
{
   $var = 1
  // return used somehow here
}
var()
echo $var

so this would echo '1'

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: chance events
« Reply #5 on: July 20, 2009, 01:41:19 AM »
you'd have to use return $var inside the function, and $var = var(); outside the function.

return (rand(1,100) <= $percent) means exactly what it says.

Evaluate this part: (rand(1,100) <= $percent) in your head. It's a boolean expression which will return true or false, and the return statement means it will return the result of this expression.

Offline Sagefire135

  • Level 14
  • *
  • Posts: 107
  • Reputation: +2/-0
    • View Profile
Re: chance events
« Reply #6 on: July 20, 2009, 02:06:58 AM »
Ya, i managed to find what i wanted with the getting a value out of a function part yay. works exactly as it should.

now that see rand(1,100) <= $percent as "true" or "false" that makes much more sense lol thanks!

Offline karnedge

  • Level 17
  • *
  • Posts: 170
  • Reputation: +4/-0
  • ctrlHack provides the server, you bring the skill.
    • View Profile
    • ctrl://Hack.game
Re: chance events
« Reply #7 on: July 20, 2009, 11:03:31 AM »
Doing a return with a conditional in () will return a boolean (true or false) so for the chance function example if the rand(1,100) comes out below the crit percent you provide then it returns true. So in turn using that function in a conditional will allow true or false results... eg; crit bonus or not.
ctrlHack - Hacking simulation RPG in development.
Latest blog: Back on Track
bbgFramework v0.1.3

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal