Author Topic: Help for my AI  (Read 3610 times)

0 Members and 1 Guest are viewing this topic.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Help for my AI
« on: February 06, 2011, 08:44:56 PM »
(Changed layout of this topic, dunno why though :D)

Current Code:

Engine download below, NOTE IT'S ALPHA 0.1 VERSION and only the basics is in the code!
And second note: The pririties aren't fully done and it may lag, you will know where to change though(if you don't it is cairuns!)!

Current problem:

My AI can't attack properly and the priority levels are some bugged, but playable :)

Requests:

Try download and find bugs for me!!!

.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

Past Problems:

Quote
There is no error message poppin' up anymore, which seems good, but I get a problem... The priority ain't changing...
Fixed, YEY!

Quote
attempt to index field '?' (a nil value)
at line 189. FIXED!

Another problem is:

Quote
invalid arguments to method call
at line 123. FIXED!

Another problem, not that big, but gets big over time:

Why are the roids recognized as "having treespace remaining" while it doesn't?

Picture info:
200 Means it is supposed to be scouted, but only if the neighbours have a higher priority(number).

100 Means need trees, now why won't this code work?
Code: [Select]
GetAsteroid(asteroids):AvailableStructureSpace()and yes, I know it is a boolean :) FIXED and it works for people that will use this :)

DOWNLOAD:
Recomended number of roids is under or 50 roids!!!!!
« Last Edit: February 09, 2011, 08:44:50 PM by Aino »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Help for my AI
« Reply #1 on: February 06, 2011, 11:14:54 PM »
It's because you haven't initialised the values for your array slots.  Line 189 checks a bunch of things against different array slots.  The error means it tried to do the compare operation, but one of the values was a number and the other was "nil".

Code: [Select]
for i = 0,numberofasteroids do
array[i] = 0
end

That would be sufficient, presuming that array is meant to hold numbers.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Help for my AI
« Reply #2 on: February 07, 2011, 01:35:49 PM »
Need help witht he second problem, it tries to call something from the function argument, but there is nothing in it(I belive this is what happens!).

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Help for my AI
« Reply #3 on: February 07, 2011, 01:37:02 PM »
It's because you haven't initialised the values for your array slots.  Line 189 checks a bunch of things against different array slots.  The error means it tried to do the compare operation, but one of the values was a number and the other was "nil".

Code: [Select]
for i = 0,numberofasteroids do
array[i] = 0
end

That would be sufficient, presuming that array is meant to hold numbers.

I have allready done that:
Code: [Select]
for i = 0,numberofasteroids do
cairoidpriority[i] = 1500
GetAsteroid(i):Reveal(1)
end
but I made an if check, so it only runs if the roids have something :)

Note: The reveal is for testing purposes, if you wonder :P

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Help for my AI
« Reply #4 on: February 07, 2011, 04:36:02 PM »
Please help, the error is somewhere around here:
Code: [Select]
i = math.random(0,numberofasteroids)
for asteroids = 0,numberofasteroids do

if i ~= asteroids then

length = GetAsteroid(asteroids).position.x - GetAsteroid(i).position.x
height = GetAsteroid(asteroids).position.y - GetAsteroid(i).position.y

distancetoroid = math.sqrt((length * length) + (height * height))
distancetoroid = distancetoroid - GetAsteroid(asteroids).Radius

if distancetoroid < GetAsteroid(i).SendDistance then

numberofneighbours = numberofneighbours + 1
neighbours[numberofneighbours] = GetAsteroid(asteroids)

if neighbours[asteroids] ~= nil then

if neighbours[asteroids].owner ~= caiid and GetAsteroid(asteroids):IsVisible(caiid) == true and GetAsteroid(asteroids):GetNumSeedlingsExcluding(caiid)*2 < GetAsteroid(i):GetNumSeedlings(caiid) and GetAsteroid(asteroids):GetNumSeedlingsExcluding(caiid) > 0 then

GetAsteroid(i):SendSeedlingsToTarget(caiid, GetAsteroid(asteroids):GetNumSeedlings(caiid), GetAsteroid(asteroids))

end

--is there any lower priorities here?
if cairoidpriority[asteroids] > cairoidpriority[i] and GetAsteroid(i).owner == caiid then
--nope, but is there any enemies nearby?
if GetAsteroid(asteroids).owner ~= caiid and GetAsteroid(asteroids).owner ~= 0 then
--yes, enemies nearby and no help to give around, lets make ourself a gatheringpoint!
cairoidpriority[i] = 700
end
end

if cairoidpriority[asteroids] < cairoidpriority[i] then
if GetAsteroid(i).owner == caiid or GetAsteroid(i).owner == 0 then

if needed[asteriods] == (-1) then
needed[asteroids] = GetAsteroid(i):GetNumSeedlings(caiid) - cailearned
end

GetAsteroid(i):SendSeedlingsToTarget(caiid, needed[asteroids], GetAsteroid(asteroids))
cairoidsent[i] = needed[asteroid]
cairoidpriority[i] = cairoidpriority[asteroids] + 1
end
end
end
end
end
end

Hope you may try to help, cause I want this AI to be published :D

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Help for my AI
« Reply #5 on: February 07, 2011, 05:13:15 PM »
Ok, fixed it... Now I feel so stupid cause it was this single little line:
Code: [Select]
GetAsteroid(asteroid).Name = cairoidpriority[i]at line 222 -.-

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Help for my AI
« Reply #6 on: February 08, 2011, 09:35:26 AM »
How is this coming along now? :>

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Help for my AI
« Reply #7 on: February 08, 2011, 11:08:52 AM »
Going fine, cept that priority problem I have D:

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Help for my AI
« Reply #8 on: February 08, 2011, 11:30:07 AM »
Getting the priority system (or metric, as I called it) right was definitely one of the hardest things about building my AI.

What I did is set each asteroid name to the priority number, that way I could click on each asteroid in-game to see what its metric number was.  That allowed me to identify problems with the propogation of the numbers across a given metric network.  It's actually surprisingly hard to produce a series of rules that will always self-correct...

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Help for my AI
« Reply #9 on: February 08, 2011, 01:29:47 PM »
Well, it isn't so hard... only make the priority(metric) and if the neighbour has a higher, then the neighbour will have the same as the roid + 1 :P

like this:

Quote
selected roid's roidpriority = some number
if neighbour's roidpriority > selected roid's roidpriority then
neighbour's roidpriority = selected roid's roidpriority + 1
end
(in quote because it's not a code...)

But this may be hard, since you did it first and I wouldn't be able to do this without your help with the neighbour stuff, didn't know you would need square root :P

EDIT: Though, I am getting problems... Maybe you can help me tell if the code I am having problems with work?(Cause it won't it seems like)
« Last Edit: February 08, 2011, 01:33:45 PM by Aino »

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Help for my AI
« Reply #10 on: February 08, 2011, 01:37:39 PM »
What I think will be cool, is that the AI will try configure itself by the way you attack/defend yourself, you can also fool it though xD

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Help for my AI
« Reply #11 on: February 08, 2011, 01:42:54 PM »
Problems are inevitable with this sort of system.  Just work through them I guess.  If there's anything you're really stuck on then post up and I'll try to help.


The problem arises thusly; suppose you set a metric of 0 whenever an asteroid detects that it is under attack.  The immediate neighbour of that asteroid might then set itself to metric 1, to indicate that seedlings should proceed to the asteroid with metric 0.
Now imagine that hte asteroid with metric 0 is no longer under siege; all the attackers have been destroyed.  What now?
Maybe you reset that asteroid's metric to 1500.  That asteroid will then glance at its neighbour, see the metric of 1, and change its own metric to 2.
The neighbour will then see the metric of 2, and set its own metric of 3...

And so on.  This would just spiral upwards, and result in seedlings flying around apparently at random.


This is the sort of problem that you really need to solve conceptually first, before you can program the desired behaviour.  Diagrams are key for this.  If you think you know exactly how it would work, draw it out on a diagram.   Write down the rules that will determine the metric numbers.  Make up some imaginary scenarios and figure out if the rules you are using for your metric system will provide correct behaviour.  For this type of project, 30 minutes spent on conceptual diagrams is worth 10 hours of coding, in my experience.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Help for my AI
« Reply #12 on: February 08, 2011, 01:49:54 PM »
Mhm, ye :P Gonna remake the system so it will take care of that :)

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Help for my AI
« Reply #13 on: February 08, 2011, 01:58:22 PM »
By the way, the square root in there is a result of using Pythagoras to calculate how far away one asteroid is from another asteroid.

Pythagoras is that thing you've forgotten you learned in school, that allows you to find the "hypotenuse" of a right-angled triangle assuming you know the lengths of the opposite and adjacent sides.

The formula is:

a^2 = b^2 + c^2


But we need a, not a^2.  So we take the square root of both sides:

a = math.sqrt(b^2 + c^2)


"a" is the distance between the centers of the 2 asteroids (ie, the hypotenuse)
"b" is the horizontal difference (the adjacent)
"c" is the vertical difference (the opposite)

To find b, subtract the X-coordinate of one asteroid from the other.
To find c, subtract the Y-coordinate of one asteroid from the other.


Finally, we can check if Asteroid 1 is in range of Asteroid 2 by checking if "c" plus the radius of Asteroid 1, is less than the send distance of Asteroid 2.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Help for my AI
« Reply #14 on: February 08, 2011, 02:04:46 PM »
O.o

I haven't learnt it yet you see, 9th grade xD

But, it's nice learning it before you do at school, cause then you know what the teacher is talking about before you are done with it :D
Neither did I know of "hypotenuse" o.O