------------------------------------------------------------------------ -- City class - container for households and associated with the -- 'k2cp-city' entity as the representation of a city. ------------------------------------------------------------------------ City = {} function City:new(o) o = o or {} setmetatable(o, self) self.__index = self return o end function City:init(i, e) self.state = { index = i, households = 0, entity = e } end function City:update(needs) local inventory = self.state.entity.get_inventory(1) local supply = true if inventory.is_empty() then supply = false else for i, v in ipairs(needs) do if inventory.get_item_count(v) - self.state.households <= 0 then supply = false end -- game.players[1].print("Item: " .. v .. " Count: " .. inventory.get_item_count(v)) end end -- game.players[1].print("Supply: " .. tostring(supply)) if supply then self.state.households = self.state.households + 1 for i, v in ipairs(needs) do local stack = {name=v, count=self.state.households} inventory.remove(stack) end else if self.state.households >= 1 then self.state.households = self.state.households - 1 for _, player in pairs(game.players) do player.print("CityPeeps - Due to lack of goods, a household has abandoned city " .. self.state.index) end end end end function City:show_gui(player_index, gui) GUI.push_left_section(player_index) gui[player_index] = GUI.push_parent(GUI.frame("city_gui", "City", GUI.VERTICAL)) GUI.label_data("households", "Households", self.state.households) GUI.pop_all() self:update_gui(player_index, gui) -- for _, need in ipairs(self:needs) do -- local labels = need.label_icon.labels -- labels.item.label.caption = game.item_prototypes[need.item].localised_name -- end -- TODO: Add needs icons/count end function City:update_gui(player_index, gui) gui[player_index].households.data.caption = self.state.households end function City:hide_gui(player_index, gui) gui[player_index].destroy() gui[player_index] = nil end