Trying to determine if a location is empty.

Trying to determine if a location is empty.

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Trying to determine if a location is empty.

Post 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?
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: Trying to determine if a location is empty.

Post 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.
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Re: Trying to determine if a location is empty.

Post by KDR_11k »

Also wouldn't GetGroundBlocking be more effective?
User avatar
TheFatController
Balanced Annihilation Developer
Posts: 1177
Joined: 10 Dec 2006, 18:46

Re: Trying to determine if a location is empty.

Post 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
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Trying to determine if a location is empty.

Post by Argh »

This didn't work, took code down.
Last edited by Argh on 25 Jan 2008, 04:40, edited 1 time in total.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Trying to determine if a location is empty.

Post by Argh »

Looks like this is a failure, too :P Nevermind...
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Trying to determine if a location is empty.

Post 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
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Trying to determine if a location is empty.

Post 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>
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Trying to determine if a location is empty.

Post 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.
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Re: Trying to determine if a location is empty.

Post 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
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Trying to determine if a location is empty.

Post 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.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Trying to determine if a location is empty.

Post 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.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Re: Trying to determine if a location is empty.

Post 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)
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Trying to determine if a location is empty.

Post 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)
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Trying to determine if a location is empty.

Post 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.
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Re: Trying to determine if a location is empty.

Post 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.
Post Reply

Return to “Lua Scripts”