Jump to content

IsoCell: Get free tile outside of building


Dr_Cox1911

Recommended Posts

I'm currently looking for a clever way of getting a free IsoGridSquare out in the open of a IsoCell.

I found the method IsoCell:getFreeTile(RoomDef paramRoomDef), but as this states it needs a RoomDef. I don't really know where I could get a RoomDef that represents the "outside-world".

 

So do you guys know any clever way of doing this? I could randomly choose tiles within the IsoCell and check them, but is this really the way to go?

Link to comment
Share on other sites

Is it not feasible to implement the code from getFreeTile() in lua?
The code for the method is pretty simplistic, and it appears the whole reason for the RoomDef (least inside of the method) is so there's a bounding box to check.

 

Totally untested and quickly ported from java, so apologies if i missed something:

-- lua version of IsoCell.getFreeTile() but with a manually specified
-- bounding box instead of using a RoomDef
function getFreeTile(cell, start_x, end_x, start_y, end_y, z)
    local free = {}
    for x=start_x, end_x do
        for y=start_y, end_y do
            local square = cell:getGridSquare(x, y, z)
            if square then
                square:setCachedIsFree(false)
                square:setCacheIsFree(false)
                if (square:isFree(false)) then  
                    table.insert(free, square)
                end
            end
        end
    end
    if #free == 0 then return end
    return free[ZombRand(#free) + 1]
end

 

Edited by Fenris_Wolf
Link to comment
Share on other sites

Not really sure the system would scale well at all, in Lua or Java side. Least not if it needs to run during time-critical events.

 

Also note the IsoCell.getGridSquare() method takes different routes (and thus performance differences) depending if its run on the server or not. Naturally, i haven't tested the performance differences so can't say with 100% certainty, but the servers method looks far more efficient.

 

Optimizations could be made to improve performance, that lua port of the function above is pretty un-optimal. Returning the first free tile would be a massive improvement instead of finding all the free tiles and randomly selecting one. If random selection is a requirement, then functions such as table.insert could be pulled local, as well as caching the size of the table...using # to check size is expensive in traditional lua. I can't really comment on kahlua's handing of # for length checks but I'd assume the cost penalty still applies.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...