Home   Help Search Login Register  

Author Topic: Random Object Creator by Haroon1992 - [version 1.0]  (Read 4953 times)

0 Members and 1 Guest are viewing this topic.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Random Object Creator by Haroon1992 - [version 1.0]
« on: 13 Jun 2010, 16:54:18 »

Random Object Creator
by Haroon1992

Please read the readme file included inside.
(I'll write the features and other things, later as I don't have much time left now. Sorry for this.. :good: )


ChangeLog (Newest is at the TOP)

Code: [Select]
v1.0
- better performance.
(Due to shortened vars plus the removal of the whole commented readme from the script, as well as some other fixes.)
- new External Objects feature.
- added a variable in which you can store desired objects to count with the collision calculation.
- tweaked interval of creation.
- changed random array selection method.
- removed unnecessary variables.

v0.85
- Modified the way the script works.
- setposing is used instead of sqrt method.
- random array selection method is changed.

v0.82
- Fixed the error causing hint (that's all !)

-v0.8
-changed : array selection method.
-changed : position selection method.
-changed : debugging mode
[b]-improved : now you can add unlimited elements to the array[/b]
-NEW : Nested array, 1st - extra objects array, 2nd - distance number
(see the script for details)
-NEW : New Example Mission with objectives.(rescue the pilots lost in the forest and extract.)
-NEW :

v0.5
- Reduced lag by changing method of array selection
- Reduced lag by creating just one logic and setposing it
(instead of spawning and deleting them)

v0.4
- initial version
« Last Edit: 29 Jan 2011, 16:11:44 by haroon1992 »
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline RKurtzDmitriyev

  • Former Staff
  • ****
Re: Random Object Placer
« Reply #1 on: 13 Jun 2010, 18:57:05 »
Sounds interesting, I'll test it. :good: Random stuff is always cool.  :yes:

EDIT:

And indeed, it is cool. Don't be shy, go ahead and include a screenshot thumbnail of that beautiful forest. :)

Haven't tested it much, but some problems I noticed:

Demo Mission
--Your package, when unzipped (at least using Ultimate Zip), simply spits out a mission.sqm and the script. Consider zipping a pre-made mission folder. Then again, from your warning at the bottom, it sounds like that's what you tried to do. ???

--A hint when the script is done placing objects would be nice.

More importantly,

The Script

--You have a hard-coded maximum of three object types per instance of the script. I'm 99% certain there should be a way to arrange your script so that it can take any number of types. I've made scripts that can process an array with an unbounded number of elements many times. Let me think about it and maybe I can give you a hint.

--Even better, it would be awesome if you could write the script so that the mission maker can specify type and distribution. Perhaps you could do this with nested arrays for your second parameter, like this:

Code: [Select]
[this,[ [typeOf tree1, 0.6], [typeOf tree2, 0.3], [typeOf rock1, 0.1] ],120,500,0] exec "random_objs.sqs"

Causing 60% of the objects to be the same type as tree1, 30% as tree2, etc.

--There has to be a more elegant way to calculate distances than this:

Code: [Select]
? _collisiondis > 0 : _hpos="heliHEmpty" createvehicle _pos
?count _objects > _max : goto "END"
~0.0005
;hint format ["%1",_cant_choose_position]
? _collisiondis > 1  : _cant_choose_position = _cant_choose_position + 0.005
;Exit script if no suitable position is found within 20 seconds.(This is not possible if collisiondis is less than 2)
;? _cant_choose_position > 20 : hint format ["SCRIPT TERMINATED!\nNO MORE SUITABLE POSITION TO PLACE OBJECT WITHOUT COLLISION WITH OTHERS!\nType 1 : %1\nNo. of Type1 : %2\n\nType 2 : %3\nNo. of Type2 : %4\n\nType 3 : %5\nNo. of Type3 : %6\nTotal No. of Objects : %7",_type select 0,count _type1,_type select 1,count _type2,_type select 2,count _type3,count _type1+count _type2+count _type3];exit
? "_x distance _hpos < _collisiondis" count _objects > 0 : goto "posmake";deletevehicle _hpos

At the very least, gameLogics are probably better than invisible chopper pads. Furthermore, rather than continuously creating and deleting invisible units, see if you can find a way to create one distance-checking unit at the beginning and setPos'ing it as needed.

Even better, forget invisible units to check distances entirely and use the getPos command and the coordinate distance formula:

distance = sqrt ( (x1 - x2)^2 + (y1-y2)^2 )

There's even a convenient function called distancepos.sqf in the OFP functions library here at OFPEC, to save you from having to type out a formula every time.

--The editor update stuff confuses me too, not least because there are at least two editor update 103's running around. I'm pretty sure I'm using Mikero's and your script is working fine.

--Regarding turning the function into SQF, that may not be possible. First of all, you'll need to have OFPR in front of you in order to work on it, as SQF external functions were introduced in v1.85, I think. Is this for the mission you were planning to make for your friend who doesn't have Resistance?

More importantly, using that many createVehicle commands in an OFP function may cause an error or crash, as SQF functions seem to interrupt all other processes until the functions are complete. This will force the engine to create dozens of trees, for example, in a single frame.

It might be able to do that, though. Test and see, if you have OFPR in front of you. :)
« Last Edit: 13 Jun 2010, 20:01:31 by RKurtzDmitriyev »
The OFP Editing Center wishes to remind you that the faithful COMREF will never threaten to stab you and, in fact, cannot speak.
However, in the event that it does speak, you are encouraged to heed its advice. ;)

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Random Object Placer
« Reply #2 on: 14 Jun 2010, 16:35:52 »
NEW VERSION IS UP!
and is ATTACHED in THIS post.


version is umm....maybe 0.5 ?
NOTE :
Open the script and see the comments for instructions.


What's new in new version ?

- reduced lag slightly

- new example mission
In that mission, a whole city will be generated on Desert Island...with trees and vehicles.
(And yes,no overlapping will occur,except a very few things like a car half through a buildings wall.
(For 100 % non-overlapping feature,just increase the inside collision dis and outside collison dis.
See below for more details on the new extended collision dis)


-name changed to roc.sqs (random object creator)

-15 max elements for the array (it was 3 in previous version)

-New Argument :
Direction (specify direction of created objects,default is random)

-New Argument :

- New Argument :
MakeGlobal (true/false)
Quote from the scripts' comments

Quote
; MAKEGLOBAL) -
;
; default is : FALSE
; All instances of the script having MAKEGLOBAL set to TRUE will check the objects
; created by ALL those instances in their collision dectection.


; example -
; Suppose there are ,
; TWO instances of the script are running
; and their properties are as follows :


; 1st instance : [other args,[various tree types],other args,TRUE] exec "roc/roc.sqs"
; 2nd instance : [otehr args,[various buildings],other args,TRUE] exec "roc/roc.sqs"
;(NOTE : the collisiondis for both instances is 20m.)

; As MAKEGLOBAL is set to TRUE in both instances,non of the trees and buildings will
; become overlapped with each others.

; if makeglobal is set to FALSE,no buildings will overlap with other buildings but they might
; overlap with the trees(as they are not created by the same instance)
; i hope you understand

- Extended Collision Distance
Quote from the script's comments.
Quote
; COLLISIONDIS) -

;array containing two elements
; default is : [0,0]

; First Element : nearest distance between each objects created within this instance
; of the script.
; Second Element : nearest distance between objects created within this INSTANCE of roc.sqs AND objects created by
; other INSTANCE(S) of roc.sqs.

; NOTES
; Second Element only works when MAKEGLOBAL is set to TRUE(See MAKEGLOBAL)
; example : [20,5]
; the nearest dis btwn the objects created by this instance = 20
; nearest dis btwn objects created by this instance and by OTHER instance(s) = 5


@RKurtzDmitriyev

Have you experienced any lag?

Yeah, I modified the script last night (before you post the feed back)

From three to fifteen
And I managed to keep the maximum element up to 15 elements.

The limitation i am facing is :

I wanted to make the script select a random element directly from the array given.
Unfortunately,
i can't use round,floor or ceil in ofp.  >:(
Code: [Select]
array select floor random count arraythat's what i wanted to use.
As ofp doesn't have anything like that to eliminate the decimals, I have to create a funciton that do the job .
(See the function to see my method)

Conversion to .sqf
The function will be fully working ONLY if i can use sleep in OFP.But unfortunately, sleep is not in ofp. >:(

Type and distribution
I am not sure how I can add this.but if i add this, the users will have to type each of the distribution
and they will become frustrated.Imagine calculating percentage distribution for 15 elements.
I know you would say "why not calculate the percentage of the rest elements,so that the user would just need to
define his desired element's distributions"
But i don't know how....to calculate it.
:(
A help on this topic would be great.

Mission Packing
Sorry, I myself became confused by WinRar's options.
This version will not have this problem.


Formula Thing
Code: [Select]
distance = sqrt ( (x1 - x2)^2 + (y1-y2)^2 )
I got 47 by 100 in my last math exam test.
So I am very bad in maths,it would be great if you could explain me that further more.
And also,how to use it and where to use it.

HeliH and Gamelogic
This version will use gamelogic.
 (i just opened the script and changed helih to logic
I am at the internet cafe at the moment,so I have not tested it)

Again,it would be great to get help on calculating distances.


Quote
--Regarding turning the function into SQF, that may not be possible. First of all, you'll need to have OFPR in front of you in order to work on it, as SQF external functions were introduced in v1.85, I think. Is this for the mission you were planning to make for your friend who doesn't have Resistance?

Well,i think you didn't know that i also have ofpr.This script was created using ofpr.
createvehicle command is not included in ofpv1.3 ...
And this is not created for the mission I am creating for my brother.

This is for my old mission First Strike (which have been lying about a year in this OFPEC Mission Beta Testing Board,unfinished)

Much Thanks for your testing efforts.....

Regards,
Haroon1992
« Last Edit: 20 Jun 2010, 17:28:06 by haroon1992 »
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline RKurtzDmitriyev

  • Former Staff
  • ****
Re: Random Object Placer
« Reply #3 on: 15 Jun 2010, 15:44:57 »
You should go ahead and update your first post with the new script, to avoid confusion.

Anyway, the new version is beautiful, man. Good work. Like I said, include a little thumbnail screenshot so people can see what your script does. Here's one I took:



Uploaded with ImageShack.us

Now, on to business... ;)

Regarding Lag

Lag was significant in the first version but was much improved for the second version. The first version would be knocked down to about 4 fps if I tried to look at the forest as it was being created. This version has no lag at all for the first 10 seconds of creation or so, lags a bit (maybe 15 fps?) for another 20 seconds, and then dies off again to a good framerate. Good job reducing lag. What did you change?

Selecting Random Elements from Array

Quote
(See the function to see my method)

:blink:

I admire your tenacity in typing up that monster of a function, but there's a much easier way to do this.

First of all, rounding numbers down in OFP can be done easily with the mod command (see macguba's comment in the comref).

Even better, though, the select command has a built-in rounding feature. The number you pass it is rounded to the nearest whole number. So if you put

Code: [Select]
_array select 3.9

it is just like putting

Code: [Select]
_array select 4

So, to select a random element from an array:

Code: [Select]
_element = _array select ((random count _array) - 0.5)

Let me explain the "- 0.5" part. If the array has (for example) 5 elements, (random count array) may generate a number like 4.9. The select command will round that to 5, but _array select 5 does not exist. Also, since _array select 0 will only result when the random number lands between 0 and 0.5, it will only occur half as often as all other possibilities. Subtracting 0.5 from (random count array) fixes both of these problems (-0.4 will be rounded to 0).

Conversion to SQF

Quote
The function will be fully working ONLY if i can use sleep in OFP.But unfortunately, sleep is not in ofp.

Indeed. Basically for OFP you've got to use SQS for anything that needs a time delay. But you can still condense the calculation portions of your script into SQF files. They will be easier to read and probably react a little faster.

Type and Distribution

Quote
if i add this, the users will have to type each of the distribution
and they will become frustrated.Imagine calculating percentage distribution for 15 elements.

Hmmm, good point. I've had to do worse, though. :P

Quote
I know you would say "why not calculate the percentage of the rest elements,so that the user would just need to
define his desired element's distributions"
But i don't know how....to calculate it.

A help on this topic would be great.

If I understand you right, you want to write your script so that the editor can define distribution for only some of the types, and let the engine figure out the distribution for the others.

Do you not know what maths to use, or how to code it? I can explain the maths easily. How to code it depends on how your distribution system will work.

Distance Formula

I will happily explain the distance formula. ;)

OFP islands are like gigantic coordinate grids. Point (0,0) is at the extreme south-west corner. Each unit is 1 meter.

Whenever you use the getPos command, you are asking the engine to figure out the coordinates of an object. If getPos returns [1000, 500, 3], then the coordinates are (1000, 500). The object is 1000 meters to the east and 500 meters to the north of point (0,0). (It's also 3 meters above ground level, but that doesn't concern us right now).

Suppose we have two points, (700, 500) and (200, 1200). 700 and 200 are x-coordinates, 500 and 1200 are y-coordinates. Let's call 700 "x1", 500 "y1", 200 "x2", and 1200 "y2".

The distance between any two points on a coordinate grid can be found by the following formula:



Uploaded with ImageShack.us

d is the distance. For OFP, this translates to

Code: [Select]
_distance = sqrt ( (_x1 - _x2)^2 + (_y1 -_y2)^2

For our two points, this would result in

Code: [Select]
_distance = sqrt ( (700-200)^2 + (500 - 1200)^2 )

The engine will calculate this as about 860.233 meters (unless a demon posseses the processor).

So, in your script, to calculate the distance between a position _pos and an object _obj,

Code: [Select]
_distance = sqrt ( ( (_pos select 0) - (getpos _object select 0) )^2 + ( (_pos select 1) - (getpos _object select 1) ) )^2

NOTE: this will measure the distance between the 2d coordinates. That means that if the player is standing 3m from the bottom of a 300m cliff, the distance measured between him and the top of the cliff will be 3m and not 300m. I don't think this will matter for your purposes. If it does, tell me and perhaps I can figure something out.
« Last Edit: 15 Jun 2010, 15:48:11 by RKurtzDmitriyev »
The OFP Editing Center wishes to remind you that the faithful COMREF will never threaten to stab you and, in fact, cannot speak.
However, in the event that it does speak, you are encouraged to heed its advice. ;)

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Random Object Placer
« Reply #4 on: 15 Jun 2010, 17:28:45 »
EDIT :
I found that setposing a gamelogic is smoother than using sqrt.
(The difference in frame in these two is about 20fps)

SQRT + array select random count array - 0.5
Tested with the laptop,frame drops from 230 fps to 130 at first ten secs.
Then to 15 after the second 10 secs.
GameLogic setposing + array select random count array - 0.5
Drops to 150 at first 10 secs.Then to 80.
And remains around 40.And rises up to 50/60 after all scripts were finished.

This will be version 0.8
Changelog (older version's changelog will be posted soon!)
-v0.8
-changed : array selection method.
-changed : position selection method.
-changed : debugging mode
-improved : now you can add unlimited elements to the array
-NEW : array of objects that you can put in the arguments.
-NEW : New Example Mission with objectives.(rescue the pilots lost in the forest and extract.)

Here is the wide explanation of what's new :

NEW ARGUMENT
;************************************
; OBJECTS) -
;   NOTE : Nested Array!  ( [[insidearray],outsidearray]
;   Put your desired collision objects to check with the instance that you are executing

;Usage
;   format : [[objects to check],distance]

;   Example :
;   [other args,[[car1,man1,tent1],20],other args] exec "roc\roc.sqs"
;   (again,keep in mind that it's a nested array,don't forget to put '[' and ']' as necessary.
;   Suppose the above instance is used to create a dense forest ,
;   I.E : Type array includes just trees,bushes,grasses and any forest related objects.
;   The created object will be place at least the defined distance (second element of collision distance)
;   from the objects put in the objects array.
;   which means,you can safely place car1,man1 and tent1 in the dense forest
;   without worrying about overlapping.
;   Time for a picnic,right?
;   Hope ya understand.
;   This will be useful to create military bases or camps in dense forests,etc.

;Note
;   Type  [[],0]
;   if you don't want to use it and WANT to set the object's direction.(Which is behind this argument!)
;   Example : [other args,[[],0],180] ,direction will be 180.

NEW EXAMPLE MISSION :

You are a lonely BlackOp trasported to Desert Island by a Mark II PBR,with the objectives to rescue two downed pilots who are expected to be captured by resistance forces on Desert Island.
Kill ALL the resistance guys and rescue the pilots.
Mission Credits : haroon1992,Rujik for his birds script,Hostages script found on OFPEC(author name unknown)
(Mission is not that perfect!)

OLD :

No more element limits!!!!

Though you've mentioned an even better idea!
array select random count array - 0.5
I'm gonna use that.Done!

Lag is even more reduced in my latest version (which is not yet available,I'll upload it tomorrow,and on the FIRST post  :good: )

How I reduced lag?
As the script don't need to call the array_select_random_element.sqf Function each 0.01 times, it is obvious that lag will be reduced.
And as you've said, i just create One logic and setposed it.

Now,almost no lag occurs...but I am going to use the NEW array method and NEW distance method to minimize the lag!
Done!
Thank You very much for your invaluable efforts.  :clap:

And if you are willing to help me more, please explain me the maths required to use the destribution method
(as i mentioned.User defines his desired elements,and the script calculate  the distribution of the rest)

Regards,
haroon1992
« Last Edit: 20 Jun 2010, 17:27:22 by haroon1992 »
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Random Object Placer
« Reply #5 on: 19 Jun 2010, 13:14:02 »
So , is there any feedback i could expect? :dunno:
(Downloads == 0   :blink:)
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline RKurtzDmitriyev

  • Former Staff
  • ****
Re: Random Object Placer
« Reply #6 on: 19 Jun 2010, 15:10:22 »
Sorry, Haroon. I didn't realize you'd updated it.

I'm surprised that the sqrt thing caused greater lag, but life is full of surprises. :dunno:
The OFP Editing Center wishes to remind you that the faithful COMREF will never threaten to stab you and, in fact, cannot speak.
However, in the event that it does speak, you are encouraged to heed its advice. ;)

Offline dr. seltsam

  • Members
  • *
Re: Random Object Placer
« Reply #7 on: 19 Jun 2010, 16:36:14 »
Tested it...
After 30sec into the mission the scripts and object creations increase the lag step by step to an unplayable level! I have error messages on screen about script termination. Because of the error message the script termination never happens??

Overall i find this randomized forrest creation a superb idea and would like to see the improvements.
:)


Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Random Object Placer
« Reply #8 on: 19 Jun 2010, 17:07:10 »

Is the error message a hint? if so :
@dr .seltsam
No, the termination message pops up when the script can't find a suitable position to place the object in 20 seconds.
And if the error popped up, it means that the script has terminated!

(And there are more than 10 instances of the script running, that's why you are getting more than one error messages)

Tell me if the error message is not a hint and is a BIS error message.

I created the example mission with a laptop having a core 2 duo processor.
Maybe that's why i didn't see much lag.

(I'll try to reduce unnecessary things )
Did you skip the time during the creation?
(give it a try and you might find the game becoming stable after the creations)


@rcurtz
I think noting a position's coordinates and moving an object to that location is faster than calculating two objects horizontal distances.


Regards,
Haroon1992
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline Krieg

  • Mission Maker
  • Members
  • *
  • Who dares wins.
Re: Random Object Placer
« Reply #9 on: 19 Jun 2010, 17:54:28 »
Gotta say, this script is pretty impressive! No error messages show up, though there was some lag, it was more than manageable (although Quad Core processor and 1GB of memory must be under quite some stress there).

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

Offline RKurtzDmitriyev

  • Former Staff
  • ****
Re: Random Object Placer
« Reply #10 on: 19 Jun 2010, 19:07:23 »
Once again, please consider adding the latest version to your first post. This is a standard OFPEC procedure (see #11).

Anyway, for me this demo mission absolutely kills my machine (framerate of like 3 fps). Perhaps I can figure out why. I'm just guessing but perhaps it's just different objects being fed into the parameters.

I didn't notice any error messages though.

I did, however, keep getting hints that just say "1" over and over again.
The OFP Editing Center wishes to remind you that the faithful COMREF will never threaten to stab you and, in fact, cannot speak.
However, in the event that it does speak, you are encouraged to heed its advice. ;)

Offline dr. seltsam

  • Members
  • *
Re: Random Object Placer
« Reply #11 on: 20 Jun 2010, 16:23:52 »
Quote
Tell me if the error message is not a hint and is a BIS error message.
Code: [Select]
? _cant_choose_position > 20 : comment "hint format ["SCRIPT TERMINATED!\nNO MORE POSITIONS OBEYING COLLISION DIS FOUND!\n\nSTATS\n\nCenter Objects : %1\n\nType Array : %2\n\nType Count : %3\nMax Objects : %4\nObjects Created : %5",_centerobjstats,_type,count _type,_max,count _objects];";deletevehicle _hpos;exit

And... because that should work multiplayer... put a gamelogic on the map, name it
Server

... then the first line of code in your roc.sqs should be:
? !(local Server) : exit

... otherwise you would multiply all the object creations by the number of players + server that are connected to a dedicated server. For the lag... my guess is that are to much and to fast distance checks during the creation process are ongoing here (you run 10 instances of it...). I have only a 2GHz single core CPU, a better comp maybe runs this without problems.
;)
« Last Edit: 20 Jun 2010, 16:29:57 by dr. seltsam »

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Random Object Placer
« Reply #12 on: 20 Jun 2010, 16:48:05 »
NEW VERSION AVAILABLE!
CHECK FIRST POST!

(well just a fix for the error message and some little tweaks)
EDIT : I found that the error can be fixed without ofp, so i just downloaded it and fixed it!

@krieg : Sorry, the one you downloaded must be the oldest version which is the most lagging one.
I hope you download my newest version this time.

FROM NOW ON, I"LL UPLOAD NEW VERSIONS IN THE FIRST POST

(i was uploading them to the current post because they were just example missions!)

The next version will be uploaded tomorrow.
Well, the error message will be fixed and lag MIGHT be reduced in the next version.
I still need to find out a way to minimize lag.

(Old computers have their advantage in this case, as with a Core2Duo i can't assure that lag will not occur
in lower CPUs)

Oh god, i think i forgot to tell you about a NEW feature in the current version!

A very nice feature was added in the CURRENT version.
Extra Objects Array
The ability to add your own desired array of objects to check with the collision/overlapping detection.
And yes, you can also specify the nearest possible distance from it to put the created objects.

This way,you can safely build enemy camps deep inside the generated dense forests.
As well as cars, people and other things in huge generated cities.


In the example mission, i put an invis helih to the extra objects array and set the nearest distance to 25
that way, no instances of the script in which the extra object array is applied will create an object within 25
meters from the HeliH.

I hope you like the idea...
(I tested it with ArmA 2 and found it working,too!
 though a little modification is needed!)

Regards,
Haroon1992
« Last Edit: 20 Jun 2010, 17:24:48 by haroon1992 »
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline Krieg

  • Mission Maker
  • Members
  • *
  • Who dares wins.
The new one I downloaded lagged pretty badly, and kept hinting me "1".
If you see a light at the end of the tunnel, then it's probably an enemy tank.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
@Krieg ,can you tell me your PC's Specs? (Or maybe PM me?)

The hint thing is fixed.

Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(