Posted By
|
Message
|
Zi-Xiao
Registered 29/07/2002
Points 537
|
11th January, 2006 at 10:02:08 -
I'm trying to do an overhead war type game. Kinda like worm wars. Now, I want the AI soldiers to aim at the closest target. This is what my code looks like so far:
Theres 2 objects, Soldier1 and Soldier2. For this example, Soldier1 is aiming at Soldier2
on some random condition:
- set soldier1 value "closest target" to 0
- set soldier2 value "distance" to the distance between soldier1 and soldier2
- set soldier2 value "ID" to spread value 0
- start loop "target closest enemy" 1000 times
on loop "target closest enemy"
- add 1 to soldier1 value "closest target"
- start loop "find low" <number of soldier2> times
on loop "find low"
+ value "ID" of soldier2 = loop index "find low"
+ soldier2 "distance" = soldier1 "closest target"
- make soldier1 aim at soldier2
- stop all the loops
- reset all the values used
The basic logic is to work out the closest enemy to each soldier seperately. So I take 1 soldier, get the distance of all other soldiers relative to it, and then take a value starting from 0 and add 1 to it. So when the distance of another soldier equals that value, then it must be the lowest so I get my soldier to aim at him. Hopefully that came across clearly.
Now, as logical as this code seems to me, mmf REFUSES to make it work properly. Can anyone give me a hand?
n/a
|
Radix hot for teacher
Registered 01/10/2003
Points 3139
|
11th January, 2006 at 10:53:36 -
There's an example of closest-target turret type aiming floating around. I think I saw it posted here not so long ago. Have a look through Code-It for threads dealing with the same problem.
n/a
|
Peblo Custom ratings must be 50 characters or less
Registered 05/07/2002
Points 185
|
11th January, 2006 at 11:03:25 -
Do we not have extensions for these kind of things? I seem to remember one...
"Isn't it always amazing how we characterize a person's intelligence by how closely their thinking matches ours?"
~Belgarath
|
Rik N
Registered 26/10/2003
Points 32
|
11th January, 2006 at 19:13:19 -
This may not help too much, but as I posted it in another thread yesterday and it may be relavent, the formula for distance (in pixels) is:
Sqr( ( ( X( "solder1" ) - X( "solder2" ) ) * ( X( "solder1" ) - X( "solder2" ) ) ) + ( ( Y( "solder1" ) - Y( "solder2" ) ) * ( Y( "solder1" ) - Y( "solder2" ) ) ) )
n/a
|
Joe.H Evil Faker
Registered 19/08/2002
Points 3305
|
12th January, 2006 at 09:51:59 -
doesnt MMF have the ability to square stuff instead of writing it out twice?
Sqr((X("soldier1") - X("soldier2"))^2 + (Y("soldier1") - Y("soldier2"))^2)
maybe
My signature is never too big!!!
|
Peblo Custom ratings must be 50 characters or less
Registered 05/07/2002
Points 185
|
12th January, 2006 at 19:52:28 -
It's POW.
"Isn't it always amazing how we characterize a person's intelligence by how closely their thinking matches ours?"
~Belgarath
|
Rik N
Registered 26/10/2003
Points 32
|
12th January, 2006 at 19:53:10 -
I would have thought so, but I didn't know how to do it. As long as it works eh.
n/a
|
Hernan
Registered 04/03/2003
Points 707
|
13th January, 2006 at 16:54:08 -
I know what the problem is. Your problem lies in the actions/conditions :
- set soldier2 value "distance" to the distance between soldier1 and soldier2,
value "ID" of soldier2 = loop index "find low"
soldier2 "distance" = soldier1 "closest target"
Whenever you make these conditions, MMF will always take the newest object (aka the soldier with the highest ID).
To solve this, (I don't know if theres a real solution but this is what I always do) put a 'pick object at random' in your events. This will, however, slow down everything. The more soldiers, the lower the chance is that the correct object will be picked. So raise the nimber of your loops, like the number of soldiers *100 or something. It's a little awkward solution maybe, but I dont know any other way ~_~. If anyone knows a better solution, I'd love to hear it.
Furthermore, I don't know whether a loop within a loop works, plus I don't even know if your coding works anyway. Well, just try the things i suggested.
This space is for rent
|
Rik N
Registered 26/10/2003
Points 32
|
13th January, 2006 at 18:10:48 -
Yeah that's a big problem. I managed to solve it if one group is solitary (ie. only ever one soldier1, and soldier2 is a seperate object with lots of them). Otherwise you could just use a fast loop to duplicate the 'pick at random' event twice as many times as objects you have or something?
n/a
|
Peblo Custom ratings must be 50 characters or less
Registered 05/07/2002
Points 185
|
13th January, 2006 at 21:47:29 -
Can you not spread a value and run a fastloop to compare fixed values?
"Isn't it always amazing how we characterize a person's intelligence by how closely their thinking matches ours?"
~Belgarath
|
Pixalatio
Registered 16/03/2003
Points 652
|
13th January, 2006 at 22:48:29 -
loops inside loops work fine... the only thing i can recomend is make a string object (or similar) in the corner of the app, and append a message whenever a loop is entered, it will be a bugger to look through it all but it may point to where the problem is. If not output other stuff to help pinpoint it... perhaps make a way of finding what is in a soldiers distance value and stuff like that
good luck =
Twas brillig, and the slivey toves
Did gyre and gimble in the wabe,
All mimsy were the borogroves,
And the momewraths outgrabe.
http://www.pixalatio.tk - visit me please
Did you know that your computer has secret buttons hiding behind others.
I intent to never delete any part of my sig until it is t big to handle.
for one small minute my rating was possibly insane
|
Radix hot for teacher
Registered 01/10/2003
Points 3139
|
13th January, 2006 at 23:11:04 -
No, really. There's an example of closest-target turret type aiming floating around. I think I saw it posted here not so long ago. Have a look through Code-It for threads dealing with the same problem.
n/a
|
Retainer
Registered 02/06/2005
Points 218
|
13th January, 2006 at 23:52:12 -
Fastloops inside fastloops is probably the most useful addition in MMF
Alien Invasion. Going forward!
http://www.create-games.com/project.asp?id=1092
|
|
|