local sit = require 'room.sit'
local plants = require 'objects.plants'

local common = require 'room.common'
encode_tooltip = common.encode_tooltip_def
decode_tooltip = common.decode_tooltip_def

function update(room)
    local width, height = room:get_size()
    local objects = room.objects
    local num_chairs = sit.seat_capacity(objects)

    local stand_areas = {}
    room.properties['stand_areas'] = stand_areas

    local owned_tiles = 0
    -- Inset by one to ignore the border
    for y = 1, height - 2 do
        for x = 1, width - 2 do
            if room:owns_tile(x, y) then
                owned_tiles = owned_tiles + 1
                table.insert(stand_areas, {x = x, y = y})
            end
        end
    end

    room.capacity = math.ceil(owned_tiles / 3)

    room.properties.can_sit = num_chairs > 0
end

function save(room)
    return {
        plants = plants.save_plants(room),
    }
end

function load(room, data)
    plants.load_plants(room, data.plants)
end

function force_entity_release(room, e)
end

function server(room)
    local objects = room.objects

    plants.tick_plants(room)

    for _, e in ipairs(room.owned) do
        e:release()
    end
    for _, e in ipairs(room.visitors) do
        e:release()
    end
end

function client(room, entities)
    plants.tick_plants_client(room)
end

function on_exec(room, method, data)
    plants.on_exec(room, method, data)
end