Page 1 of 1

Trying to determine if a location is empty.

Posted: 24 Jan 2008, 14:24
by Argh
I'm trying to write an expression that checks a given location X,Z for the presence of any Units. If there aren't any Units around, then it should return nil. However, I am having trouble getting it to return a value that is useful.

Here's the code:

Code: Select all

                    local Empty = Spring.GetUnitsInSphere(X, 0, Z, 200)
					
					if Empty[1] == nil then Do Stuff end
I don't get it. Shouldn't the first index of Empty return non-nil if any Units are within that sphere? Or am I doing something wrong here?

Re: Trying to determine if a location is empty.

Posted: 24 Jan 2008, 14:37
by Kloot
Wiki wrote: Spring.GetUnitsInSphere

( number x, number y, number z, number radius [,number teamID] )
-> nil | unitTable = { [1] = number unitID, etc... }
This...

Code: Select all

if Empty[1] == nil
... will therefore fail if the sphere contains no units, and
always evaluate to false if it does.

Re: Trying to determine if a location is empty.

Posted: 24 Jan 2008, 18:23
by KDR_11k
Also wouldn't GetGroundBlocking be more effective?

Re: Trying to determine if a location is empty.

Posted: 24 Jan 2008, 18:58
by TheFatController
GetPositionLosState is pretty cool if it's what you need, it returns three booleans for if the point can be seen by any units. I think something like LOS(or)RADAR, Radar, LOS for an x,y,z location

Re: Trying to determine if a location is empty.

Posted: 25 Jan 2008, 03:47
by Argh
This didn't work, took code down.

Re: Trying to determine if a location is empty.

Posted: 25 Jan 2008, 04:23
by Argh
Looks like this is a failure, too :P Nevermind...

Re: Trying to determine if a location is empty.

Posted: 25 Jan 2008, 04:58
by Argh
Ok, this code seems to find a collision every time:

Code: Select all

                	Empty = Spring.GetUnitsInRectangle(MinX,MinZ,MaxX,MaxZ)
                	Empty2 = Empty[1]                	
                	if Empty ~= nil then
                	if Empty2 ~= nil then CanPlace = 0 end
					if Empty2 == nil then CanPlace = 1 end
					end

Re: Trying to determine if a location is empty.

Posted: 25 Jan 2008, 05:25
by Argh
Success, at last. Arbitrarily-placed Units, of any Team, can now be placed within World Builder, during the first frame of simulation, and will not get procedural elements placed over them. Should help get this to be used by mappers, because they can place any specialized objects they desire, with no worries that they'll get visually obliterated or cause bad gameplay.

Lurker, this does not obviate my desire to have a grid solver, however- I still want something to help place sub-objects within a given range X,Z, and this procedure's an all-or-nothing deal, not very sophisticated.

Lastly, yes, this means that I can finally send the demo out, I just want to make a bit more content first. <starts making stuff>

Re: Trying to determine if a location is empty.

Posted: 25 Jan 2008, 09:39
by lurker
Yeah. I haven't had any time the last couple days, but it's becoming the weekend now. I'm working on it / trying to stop introducing algorithm flaws. It would probably help me make the api to have a copy of the demo.

Re: Trying to determine if a location is empty.

Posted: 25 Jan 2008, 10:33
by KDR_11k
For reference, this is how Fibre handles building placement:

Code: Select all

		local ok, f = Spring.TestBuildOrder(-cmd, c.x, c.y, c.z, c.heading)
		if ok == 2 and not f then

Re: Trying to determine if a location is empty.

Posted: 25 Jan 2008, 10:51
by Argh
Cool, thanks for sharing that. I'll try that and compare the results, maybe it'll be more flexible than what I'm doing, which is not terribly flexible, and allow me to do some more randomization.

Re: Trying to determine if a location is empty.

Posted: 25 Jan 2008, 22:44
by Argh
Um, KDR, how does Spring.TestBuildOrder know the size of the Unit it's trying to build? Using a point won't do at all.

Re: Trying to determine if a location is empty.

Posted: 25 Jan 2008, 23:11
by trepan
Straight from the wiki, you don't try very hard...

Code: Select all

Spring.TestBuildOrder

 ( number unitDefID, number x, number y, number z, number facing)
   -> number blocking [,number featureID]

 blocking can be:
   0 - blocked
   1 - mobile unit in the way
   2 - free  (or if featureID is != 0 then with a blocking feature that can be reclaimed)

Re: Trying to determine if a location is empty.

Posted: 26 Jan 2008, 01:25
by FLOZi
As much as I often disagree with Argh's methods, 9 times out of 10 asking someone in the know is far more productive than looking thorugh the wiki (Very unfortunately)

Re: Trying to determine if a location is empty.

Posted: 26 Jan 2008, 07:09
by Argh
@KDR: I've tested your method, and after some trouble, got it working, thanks to your example code in Fibre. However, it's quite a bit slower than what I'm doing. On the other hand, your method is more generally useful by far- mine's very crude, and requires strict controls to work propertly, but is speedy.

@FLOZi: Asking for help, on the Forum area devoted to the second major programming language game designers for Spring are supposed to learn, seems entirely appropriate to me. Everybody learns. I do not care if people don't like it, frankly.

Re: Trying to determine if a location is empty.

Posted: 26 Jan 2008, 08:54
by KDR_11k
FLOZi wrote:As much as I often disagree with Argh's methods, 9 times out of 10 asking someone in the know is far more productive than looking thorugh the wiki (Very unfortunately)
Meh, if you're just looking for a callout the Wiki has a nice list that's often sufficient.