Euflorium: The Eufloria Community
Eufloria => Eufloria Classic => Eufloria Classic Mods => Topic started by: Aino on February 14, 2011, 09:55:43 PM
-
I have my project, CC, which will not work what so ever, what mey be the problem?
for i = 0,numberofroids do
for j = neighbourstart[i],neighbourend[i] do
if CCpriorities[i] > CCpriorities[j] then
*codes*
end
end
end
And the setup is like this:
function CCInit()
numberofroids=-1
for i = 0,200 do
if GetAsteroid(i) ~= nil then
if GetAsteroid(i).radius > 10 then
numberofroids = numberofroids + 1
end
end
end
neighbourstart = {}
neighbourend = {}
neighbournumber = -1
neighbour = {}
for i = 0,numberofroids do
if neighbournumber == -1 then
neighbourstart[i] = 0
else
neighbourstart[i] = neighbournumber
end
for j = 0,numberofroids do
if j ~= i then
if GetAsteroid(j) ~= nil and GetAsteroid(i) ~= nil then
length = GetAsteroid(i).position.x - GetAsteroid(j).position.x
height = GetAsteroid(i).position.y - GetAsteroid(j).position.y
distancetoroid = math.sqrt((length * length) + (height * height))
distancetoroid = distancetoroid - GetAsteroid(j).Radius
if distancetoroid < GetAsteroid(i).SendDistance then
neighbournumber = neighbournumber + 1
neighbour[j] = j
end
end
end
end
neighbourend[i] = neighbournumber
end
CCpriorities = {}
for i = 0,numberofroids do
CCpriorities[i] = numberofroids
end
end
One of you know why... and the error is nil, but for me it seems I have set everything to what it is supposed to be :(
-
Kind of hard to decypher when I don't have a conceptual map of what the code is supposed to do... but I did notice this bit:
neighbournumber = -1
neighbour = {}
for i = 0,numberofroids do
if neighbournumber == -1 then
neighbourstart[i] = 0
else
neighbourstart[i] = neighbournumber
end
In the first line, the variable neighbournumber is set to -1.
Then, you have a conditional to check whether it's equal to -1. It will ALWAYS be equal to -1, because that's what you've just set it to.
That means that this code will never be run:
else
neighbourstart[i] = neighbournumber
Is that ok? If so, why bother having an "else" there at all?
-
No, it's not in a while loop, so it just run the For loop and add more and more neighbournumbers. It won't reset, because I have seen it in effect and working :)
-
if GetAsteroid(j) ~= nil and GetAsteroid(i) ~= nil then
I know I told you to do this before, but those will ever be nil. As long as you've made sure any "spacer" asteroids (with radius of 0) have the very highest ID number of any asteroid in the level.
So you could safely remove this line, I think. It would make the code a little clearer and gets rid of a deeply nested conditional which would improve performance. :>
-
No, it's not in a while loop, so it just run the For loop and add more and more neighbournumbers. It won't reset, because I have seen it in effect and working :)
Are you sure? If that's really the case, you could replace these lines:
if neighbournumber == -1 then
neighbourstart[i] = 0
else
neighbourstart[i] = neighbournumber
end
..with just this line:
neighbourstart[i] = 0
...and it would do the exact same thing as it is doing now.
-
Figured out one thing:
for i = 0,numberofroids do
for j = neighbourstart[i],neighbourend[i] do
if CCpriorities[i] > CCpriorities[j] then
*codes*
end
end
end
is wrong in one way... It got to be like this:
for i = 0,numberofroids do
for j = neighbourstart[i],neighbourend[i] do
if CCpriorities[i] > CCpriorities[neighbour[j]] then
*codes*
end
end
end
-
But what if I do this?
for i = 0,numberofroids do
for j = neighbourstart[i],neighbourend[i] do
if neighbour[j] ~= i then
checkedneighbour = neighbour[j]
if CCpriorities[checkedneighbour] ~= nil then
CCpriorities[checkedneighbour] = numberofroids
elseif CCpriorities[i] > CCpriorities[checkedneighbour] then
CCpriorities[i] = CCpriorities[checkedneighbour] + 1
if GetAsteroid(i):GetNumSeedlings() > CCdefensive then
GetAsteroid(i):SendSeedlingToTarget(1, GetAsteroid(i):GetNumSeedlings() - CCdefensive, GetAsteroid(checkedneighbour))
end
end
end
end
end
EDIT:
SRY!!
This I meant:
for i = 0,numberofroids do
for j = neighbourstart[i],neighbourend[i] do
if neighbour[j] ~= i then
checkedneighbour = neighbour[j]
if CCpriorities[checkedneighbour] == nil then
CCpriorities[checkedneighbour] = numberofroids
elseif CCpriorities[i] > CCpriorities[checkedneighbour] and CCpriorities[checkedneighbour] ~= nil then
CCpriorities[i] = CCpriorities[checkedneighbour] + 1
if GetAsteroid(i):GetNumSeedlings() > CCdefensive then
GetAsteroid(i):SendSeedlingToTarget(1, GetAsteroid(i):GetNumSeedlings() - CCdefensive, GetAsteroid(checkedneighbour))
end
end
end
end
or something around that...
-
Also, neighbourstart[n] will always be equal to 0 for all values of n.
So that means you could change this code:
for i = 0,numberofroids do
for j = neighbourstart[i],neighbourend[i] do
if CCpriorities[i] > CCpriorities[j] then
*codes*
end
end
end
To this:
for i = 0,numberofroids do
for j = 0,neighbourend[i] do
if CCpriorities[i] > CCpriorities[j] then
*codes*
end
end
end
That would mean you could change this code:
function CCInit()
numberofroids=-1
for i = 0,200 do
if GetAsteroid(i) ~= nil then
if GetAsteroid(i).radius > 10 then
numberofroids = numberofroids + 1
end
end
end
neighbourstart = {}
neighbourend = {}
neighbournumber = -1
neighbour = {}
for i = 0,numberofroids do
if neighbournumber == -1 then
neighbourstart[i] = 0
else
neighbourstart[i] = neighbournumber
end
for j = 0,numberofroids do
if j ~= i then
if GetAsteroid(j) ~= nil and GetAsteroid(i) ~= nil then
length = GetAsteroid(i).position.x - GetAsteroid(j).position.x
height = GetAsteroid(i).position.y - GetAsteroid(j).position.y
distancetoroid = math.sqrt((length * length) + (height * height))
distancetoroid = distancetoroid - GetAsteroid(j).Radius
if distancetoroid < GetAsteroid(i).SendDistance then
neighbournumber = neighbournumber + 1
neighbour[j] = j
end
end
end
end
neighbourend[i] = neighbournumber
end
CCpriorities = {}
for i = 0,numberofroids do
CCpriorities[i] = numberofroids
end
end
To this:
function CCInit()
numberofroids=-1
for i = 0,200 do
if GetAsteroid(i) ~= nil then
if GetAsteroid(i).radius > 10 then
numberofroids = numberofroids + 1
end
end
end
neighbourend = {}
neighbournumber = -1
neighbour = {}
for i = 0,numberofroids do
for j = 0,numberofroids do
if j ~= i then
if GetAsteroid(j) ~= nil and GetAsteroid(i) ~= nil then
length = GetAsteroid(i).position.x - GetAsteroid(j).position.x
height = GetAsteroid(i).position.y - GetAsteroid(j).position.y
distancetoroid = math.sqrt((length * length) + (height * height))
distancetoroid = distancetoroid - GetAsteroid(j).Radius
if distancetoroid < GetAsteroid(i).SendDistance then
neighbournumber = neighbournumber + 1
neighbour[j] = j
end
end
end
end
neighbourend[i] = neighbournumber
end
CCpriorities = {}
for i = 0,numberofroids do
CCpriorities[i] = numberofroids
end
end
Furthermore, if you take my suggestion about removing the nil checks, the code would simply be this:
function CCInit()
numberofroids=-1
for i = 0,200 do
if GetAsteroid(i) ~= nil then
if GetAsteroid(i).radius > 10 then
numberofroids = numberofroids + 1
end
end
end
neighbourend = {}
neighbournumber = -1
neighbour = {}
for i = 0,numberofroids do
for j = 0,numberofroids do
if j ~= i then
length = GetAsteroid(i).position.x - GetAsteroid(j).position.x
height = GetAsteroid(i).position.y - GetAsteroid(j).position.y
distancetoroid = math.sqrt((length * length) + (height * height))
distancetoroid = distancetoroid - GetAsteroid(j).Radius
if distancetoroid < GetAsteroid(i).SendDistance then
neighbournumber = neighbournumber + 1
neighbour[j] = j
end
end
end
neighbourend[i] = neighbournumber
end
CCpriorities = {}
for i = 0,numberofroids do
CCpriorities[i] = numberofroids
end
end
That simplifies the code a lot, but it doesn't explain why it crashes.
So I don't think the true cause the error is here.
-
Can you just post the whole code so I can see what order everything happens in?
I had a brief look at the new code you posted up anyway.
What's the meaning of this line?
if neighbour[j] ~= i then
Shouldn't it be "if j ~= i then" ?
-
Assuming your latest codes are run _after_ everything else, look at this problem:
if CCpriorities[checkedneighbour] ~= nil then
CCpriorities[checkedneighbour] = numberofroids
elseif CCpriorities[i] > CCpriorities[checkedneighbour] then
CCpriorities[i] = CCpriorities[checkedneighbour] + 1
if GetAsteroid(i):GetNumSeedlings() > CCdefensive then
GetAsteroid(i):SendSeedlingToTarget(1, GetAsteroid(i):GetNumSeedlings() - CCdefensive, GetAsteroid(checkedneighbour))
end
end
You already assigned the value for all slots of CCpriorities to be however many asteroids are in the level. In the first line of this code, you check if it's not equal to nil. Well, it's never going to be equal to nil, is it? Because you set it to a number.
So that means that only this line will run:
CCpriorities[checkedneighbour] = numberofroids
The rest of that code will never run.
-
Ok, three posts and three answers:
1st: I can't remove it. Because if you look at scenario like this:
0 5 6
2 4
3 1
and all of the numbers are roids...
Let's say 0 reaches 5, 2 and 3 and 2 reaches 0,1,3,4 and 5...
Then: For the neighbours for roid 0 it will start with 0,1 and 2 then for roid 2 it will follow the same and results 3,4,5,6 and 7.
Now all this numbers need to do something, the number is calling a variable (neighbours) which contains the ID for the neighbour it is set to :)
Now cleaning that up, the second question makes sense since neighbour[j] is the ID for the roid...
3rd: Well, look at the updated one, I know I was wrong and it didn't work :P
-
it's like it don't want to work at all! God damnit -.-
Only if I add this:
and neighbour[j] ~= nil then
behind:
if neighbour[j] ~= i
it will begin crashing instead of not doing what is behind, why the hell?
-
Look, I'm sorry, but I can't help you unless you post the whole code. Otherwise it's like trying to do a 1000 piece jigsaw with 400 pieces missing.
-
Ok, reply when u have DL'ed it cause I don't want tho release this just yet :P
I'll just PM you it, how stupid am I? (Very stupid :))
-
Ok done. When you release the map, I will re-attach this file though, so that other people can benefit from our discussions. Hope that's ok. :>
-
Ye, or maybe I shall :P But not now ... And it's not a map, it's a function and I want it to run smooth because it's not supposed to cause much problems, but that itself causes all these problems -.-
-
Maybe it's not working because you forgot the coroutine.yield()
function LevelLogic()
while GameRunning() do
CCEngine()
end
end
It's missing. :>
-
Ye, or maybe I shall :P But not now ... And it's not a map, it's a function
It's still code. :>
-
Holy crap, how can I skip that? D:
-
But not a map, but it will be included in a map i guess, else it is useless, hope I will get to use all these functions I make :)
-
Maybe the lack of coroutine.yield() caused the game to crash when I had errors? I don't wanna check though xD
-
Sometimes it's the things you least expect.. :>
-
If you have tested the map, you will se a nice little box in the corner, to make people understand it I need to make letters inside there to x.x
... or just make THEM having different colors untill I find the eager to make letters drawable ...
-
Strtange.. it haven't reacted on:
if CCRallyAllowed == false then
as a nil, because it is nil currently xD
-
One last problem, it won't do anything when pressing the box... annikk, you have the code and plaease look at it? I bet I have done the coords wrond though :P
-
nah, fix'd it... Gonna release this with a map, but first I wanna test it with EverSwarm :D:D
-
HOLY CRAP, It runs smooth as seeding hell... I will use this neighbour system on my AI too, cause I tested it with 108 roids and it runs just like a normal map without it does, or maybe my code has an error that isn't succesfully ruining my code? *Implent dramatic sound here!*
EDIT: Tested with 208 roids(and fixed the numroid thing to recognixe the 8 last too:P) and it runs at the same smoothness level :O
EDIT 2: There is one error that makes it not set priorities correct... Trying to figure out why...
-
I still have no idea what on earth you are working on... but it sounds cool, and it sounds like you're making progress! :>
-
Lol, ye... I'll PM you the code if you want.
-
function LevelSetup()
Globals.G.Asteroids=(0)
Globals.G.EnemyFactionsMin=(0)
Globals.G.EnemyFactionsMax=(0)
Globals.G.GreysProbability=(0)
Globals.AI.GraceTimer=(99999)
SetBackdropColour(0,0,0)
a = AddAsteroidWithAttribs(2000,0, 0.3,0.3,0.3)
a.Owner = 0
a.TreeCap = 2
a:SetRadius(300)
a.SendDistance = 3500
a:Reveal(1)
a.Moveable = false
a = AddAsteroidWithAttribs(0,0, 0.7,0.6,0.5)
a.Owner = 1
a.TreeCap = 20
a:SetRadius(500)
a.SendDistance = 7000
a.Moveable = false
a:AddSeedlings(20)
a = AddAsteroidWithAttribs(4000,0, 0.3,0.3,0.3)
a.Owner = 0
a.TreeCap = 2
a:SetRadius(300)
a.SendDistance = 2500
a:Reveal(1)
a.Moveable = false
a = AddAsteroidWithAttribs(6000,0, 0.3,0.3,0.3)
a.Owner = 0
a.TreeCap = 2
a:SetRadius(300)
a.SendDistance = 2500
a:Reveal(1)
a.Moveable = false
a = AddAsteroidWithAttribs(8000,0, 0.3,0.3,0.3)
a.Owner = 3
a.TreeCap = 2
a:SetRadius(300)
a.SendDistance = 2500
a:Reveal(1)
a.Moveable = false
a:AddSeedlings(68)
a = AddAsteroidWithAttribs(0,-2000, 0.3,0.3,0.3)
a.Owner = 0
a.TreeCap = 2
a:SetRadius(300)
a.SendDistance = 3500
a:Reveal(1)
a.Moveable = false
a = AddAsteroidWithAttribs(0,-4000, 0.3,0.3,0.3)
a.Owner = 0
a.TreeCap = 2
a:SetRadius(300)
a.SendDistance = 2500
a:Reveal(1)
a.Moveable = false
a = AddAsteroidWithAttribs(0,-6000, 0.3,0.3,0.3)
a.Owner = 0
a.TreeCap = 2
a:SetRadius(300)
a.SendDistance = 2500
a:Reveal(1)
a.Moveable = false
a = AddAsteroidWithAttribs(0,-8000, 0.3,0.3,0.3)
a.Owner = 2
a.TreeCap = 2
a:SetRadius(300)
a.SendDistance = 2500
a:Reveal(1)
a.Moveable = false
a:AddSeedlings(68)
SetCameraPositionToAsteroidID(1)
SetCameraZoomNow(5)
AddAsteroidRing(200, 0, 0, 15000, 150)
CCInit()
end
function ScreenDraw()
maxx = GetScreenWidth()
maxy = GetScreenHeight()
x1 = maxx
y1 = maxy
x2 = maxx - (maxx/15)
y2 = maxy
DrawLine(x1,y1,x2,y2,255,0,0,255,255,0,0,255,5)
x1 = maxx - (maxx/15)
y1 = maxy
x2 = maxx - (maxx/15)
y2 = maxy - (maxy/15)
DrawLine(x1,y1,x2,y2,255,0,0,255,255,0,0,255,5)
x1 = maxx
y1 = maxy
x2 = maxx
y2 = maxy - (maxy/15)
DrawLine(x1,y1,x2,y2,255,0,0,255,255,0,0,255,5)
x1 = maxx
y1 = maxy - (maxy/15)
x2 = maxx - (maxx/15)
y2 = maxy - (maxy/15)
DrawLine(x1,y1,x2,y2,255,0,0,255,255,0,0,255,5)
end
function LevelLogic()
while GameRunning() do
CCEngine()
coroutine.yield()
end
end
function CCInit()
numberofroids=-1
for i = 0,300 do
if GetAsteroid(i) ~= nil then
if GetAsteroid(i).radius > 10 then
numberofroids = numberofroids + 1
end
end
end
neighbourstart = {}
neighbourend = {}
neighbournumber = -1
neighbour = {}
for i = 0,numberofroids do
if neighbournumber == -1 then
neighbourstart[i] = 0
else
neighbourstart[i] = neighbournumber + 1
end
for j = 0,numberofroids do
if j ~= i then
length = GetAsteroid(i).position.x - GetAsteroid(j).position.x
height = GetAsteroid(i).position.y - GetAsteroid(j).position.y
distancetoroid = math.sqrt((length * length) + (height * height))
distancetoroid = distancetoroid - GetAsteroid(j).Radius
if distancetoroid < GetAsteroid(i).SendDistance then
neighbournumber = neighbournumber + 1
neighbour[j] = j
end
end
end
neighbourend[i] = neighbournumber
end
CCpriorities = {}
barrier = {}
CCdefensive = 10
checked = {}
for i = 0,numberofroids do
CCpriorities[i] = numberofroids
barrier[i] = false
checked[i] = 0
end
end
function CCEngine()
for i = 0,numberofroids do
if barrier[i] == false then
for j = neighbourstart[i],neighbourend[i] do
if neighbour[j] ~= i and neighbour[j] ~= nil then
if CCpriorities[i] > CCpriorities[neighbour[j]] then
CCpriorities[i] = CCpriorities[neighbour[j]] + 1
if GetAsteroid(i):GetNumSeedlings(1) > CCdefensive then
sendthisamount = GetAsteroid(i):GetNumSeedlings(1) - CCdefensive
GetAsteroid(i):SendSeedlingsToTarget(1, sendthisamount, GetAsteroid(neighbour[j]))
end
checked[i] = 1
end
end
end
end
end
end
function OnMouseLeftDownScreen(x,y)
if x >= maxx - maxx and x <= maxx - maxx + (maxx/15) and y >= maxy - maxy and y <= maxy - maxy + (maxy/15) then
for i = 0,numberofroids do
CCpriorities[i] = numberofroids
checked[i] = 0
end
CCpriorities[selectedid] = 0
end
end
function OnAsteroidSelected(id)
print(CCpriorities[id] .. " " .. neighbourstart[id] .. " " .. neighbourend[id] .. " " .. id .. " " .. checked[id])
selectedid = id
end
The checked shows that it ain't checking all roids, acctually just the two first (0,1) but why???
-
You need to put comments on most lines of your code, otherwise it's impossible to tell what it's supposed to be doing.
A general description of what problem you are trying to solve, and how, would also help.
-
Ye, gonna add descriptions from now on :)
And the problem is that the for loop doesn't go trough all the roids(which explains no lag :/) but I dunno why, everything seems fine...
-
Sorry... when I said "what problem are you trying to solve?" what I really meant was "What is your code supposed to do - overall?"
When you want to code a big project, you should always start by working out what your overall goal is. You need to be able to define well what ought to happen. Write this down, and be detailed.
Next, write down (in as much detail as possible) HOW you are going to accomplish this. Will you use a for loop? Or several for loops? What will be inside them, and why? Do you know what equations (such as pythagoras) are going to be needed? Write all of this down.
If your project is large and complicated, it's also a good idea to draw diagrams of things. This is an extremely useful exercise and coding things like the 3D engine, gravity engine, and my own AI would have been insanely difficult had I not made diagrams.
Once you've done all that, THEN it's time to start coding. :> Whenever you're about to type a line of code that is anything more complicated than initialising or incrementing a variable (or whatever), you should first write out a comment for the line, to say what you want it to do. Then, on the next line, translate that comment into code.
If you don't follow a process like this, you will run into problems later - as you've perhaps already discovered when you programmed your AI. :>
-
As an example, look at all the planning I did before writing the new version of Infected AI:
http://www.dyson-game.com/smf/index.php?topic=1171.0
I wrote basically that whole thread before typing a single line of code. :> It paid off bigtime.
-
I wanna code, not write a tons of things D:
I usually run my coding on the idea, not typing it in and then coding it. I am allready having some pieces of paper in front of me, designing stuff(not good though xD) and writing down examples for codes :) Without the I would never figure out aboue neighbourstart and neighbourend and the numbers inbetween stores the acctual ID, that took me some thinking, but seems simple when it is thought of already :)
Main goal in these sentences:
1: I do paint and do stuff before coding, but I also code as it runs into my head, test and fixing all the problem :)
2: I'm gonna begin making comments before making the code 2 reasons:
1: You understand better then...
2: I know what to do better :)
-
That sounds like a good idea. :>
I know when you get an idea for a level or a function, the temptation is to start coding immediately. But every 20 minutes you spend planning beforehand will save you an hour or two of troubleshooting later on.. :>
I speak from experience - specifically, the experience of coding Infected AI version 1. I had the idea, started coding immediately and didn't stop until I was done - which took about 12 hours. It took an hour of troubleshooting to even get the level to load, and when it finally did, my AI did nothing. Absolutely squat. :>
It took months to get IAIv1 right. AIv2 took me only a couple weeks. :>
-
Lol, nice :) But I have made a map already, I think I will release it now :)
-
Ok. :> Are you sure you don't want to get it beta tested first though, in case there are some bugs that you didn't find yourself?
-
It's not supposed to be on this topic actually, cause it has nothing to do with this :) I guess there will be few bugs, since there isn't much coding, but adding a whole new concept, hows that possible?
-
Well just ask for beta testers on the forum if you need your map tested, then PM them the code. :>
You'll need to allow a few days for them to play the map and get back to you.
If there's not much scripting it might be ok to just release it as-is though. Up to you. :>
-
Released :)
Maybe if I am going to release maps with big codes I will make beta testing a must for me :)