Home   Help Search Login Register  

Author Topic: random under certain number? [SOLVED]  (Read 1503 times)

0 Members and 1 Guest are viewing this topic.

Offline Krieg

  • Mission Maker
  • Members
  • *
  • Who dares wins.
random under certain number? [SOLVED]
« on: 31 Jan 2010, 08:25:42 »
I am making an artillery script in which combines onMapSingleClick with createVehicle.
Code: [Select]
_pos = _this select 0
createVehicle "shell125" getPos _pos + random
But random number could result in 1548415456 which would throw the shell waaay of the island.

My question is, is there a way to make random go under certain number (i.e. under 150)?

Thanks,
Krieg
« Last Edit: 31 Jan 2010, 17:39:55 by Krieg »
If you see a light at the end of the tunnel, then it's probably an enemy tank.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: random under certain number?
« Reply #1 on: 31 Jan 2010, 08:54:34 »
Quote
But random number could result in 1548415456
No it doesn't, unless you use random 1548415456.

And you can't even use it like that with a position since position is in three dimentions: [x,y,z]. It returns an array of values, such as [67543.1,8378.23,0.23476]. When you're dealing with arrays each element in it has to be dealt with individually. Element is an entry in the array separated by comma (or what ever , is called).
The first elements index number is 0, the second is 1, and so on.

The format of the random command is random <number>, so if you set for example random 5 it will return a random number anywhere on or between 0 and 5.

I'm assuming since it's a arty script you would want to have a random position around the given position, within a random diameter. For that you need some trigonometry.

So try something like this:
Code: [Select]
; random diamater between 0 and 150 meters
_randDia = random 150
; random direction
_randDir = random 360
; create shell within the random diameter between 50 and 100 meters into the air
_shell = "Shell125" createVehicle [(getPos _pos select 0)+_randDia*sin(_randDir),(getPos _pos select 1)+_randDia*cos(_randDir),50+(random 50)]

Note that I'm a complete math retard so the trig part maybe completely wrong  :D
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Krieg

  • Mission Maker
  • Members
  • *
  • Who dares wins.
Re: random under certain number?
« Reply #2 on: 31 Jan 2010, 10:42:14 »
Thanks h-!

To my understanding, complete script would look like this:
Code: [Select]
_shot = 0
_randDia = random 150
_randDir = random 360

#bigloop
?(_shot == 50) : goto "exit"
_shell = "Shell125" createVehicle [(getPos _pos select 0)+_randDia*sin(_randDir),(getPos _pos select 1)+_randDia*cos(_randDir),50+(random 50)]
_shot = _shot + 1
goto "bigloop"

#exit
exit

I'll test it right away, thanks!
If you see a light at the end of the tunnel, then it's probably an enemy tank.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: random under certain number?
« Reply #3 on: 31 Jan 2010, 10:44:12 »
Put the _ranDir and _ranDia inside the loop too.
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Krieg

  • Mission Maker
  • Members
  • *
  • Who dares wins.
Re: random under certain number?
« Reply #4 on: 31 Jan 2010, 10:54:20 »
I tried the script, but shells seemed to fall somewhere in the ocean.
I did put _ranDir and _ranDia in the loop as you suggested.

If you see a light at the end of the tunnel, then it's probably an enemy tank.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: random under certain number?
« Reply #5 on: 31 Jan 2010, 11:25:48 »
Ok.
I assumed the _pos is an object you refer to in the script and overlooked it not being there. :P

So put for example a game logic where you want the arty to drop and name it to something you like and instead of _pos use the name of the game logic in the script.

Or instead of typing the name into the script you can do something like:
Code: [Select]
_pos = _this select 0;
_shot = 0

#bigloop
?(_shot == 50) : goto "exit"

_randDia = random 150
_randDir = random 360
_shell = "Shell125" createVehicle [(getPos _pos select 0)+_randDia*sin(_randDir),(getPos _pos select 1)+_randDia*cos(_randDir),50+(random 50)]
_shot = _shot + 1
goto "bigloop"

#exit
exit
Then you execute that script with [gamelogic name] exec "script.sqs"
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Krieg

  • Mission Maker
  • Members
  • *
  • Who dares wins.
Re: random under certain number?
« Reply #6 on: 31 Jan 2010, 13:57:58 »
That works perfectly!
Thanks h-!

Two more questions:

When I tried executing a _pos from onMapSingeClick, it reported some error, but the script itself was too long so I was not able to read what kind of error it was. Is there any way to fix this?

Does random work with time delays (i.e. ~random 1)?
If you see a light at the end of the tunnel, then it's probably an enemy tank.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: random under certain number?
« Reply #7 on: 31 Jan 2010, 16:40:21 »
When using onMapSingleClick you need to do something like:
Code: [Select]
onMapSingleClick {[_pos] exec "script.sqs"}And then change the shell creation line to
Code: [Select]
_shell = "Shell125" createVehicle [(_pos select 0)+_randDia*sin(_randDir),(_pos select 1)+_randDia*cos(_randDir),50+(random 50)]
I'm not completely sure that works because it has been years since I have done anything with mapclick..


Quote
Does random work with time delays
Yes, you probably need to use it like this to avoid errors:
~(random 1)
« Last Edit: 31 Jan 2010, 16:41:59 by h- »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Krieg

  • Mission Maker
  • Members
  • *
  • Who dares wins.
Re: random under certain number?
« Reply #8 on: 31 Jan 2010, 17:39:45 »
Works perfectly! Thanks! :good:
If you see a light at the end of the tunnel, then it's probably an enemy tank.