Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix the slot handling in EquipSet

  * Add __RemoveFromSlot and __AddToSlot helper functions
  * Add support for non-table Equipment.support values.
  • Loading branch information...
commit 5117b65ae13174b85a18619ceeec7ab23918691e 1 parent 824da47
@laarmen authored
Showing with 102 additions and 37 deletions.
  1. +71 −6 data/libs/EquipSet.lua
  2. +31 −31 data/libs/Equipment.lua
View
77 data/libs/EquipSet.lua
@@ -75,18 +75,51 @@ make_EquipSet_generator = function (specs)
-- Add some equipment to the ship
-- Careful: it doesn't apply any modifier or anything else.
-- It just manages how much place there is in the slots.
- --
+ -- It will add to the default slots as defined in e.slots, filling the first one before trying the next.
+ -- If you wish to add to a specific slot, use EquipSet:__AddToSlot()
+ --
-- Parameters:
--
-- e - the equipment to install
-- num - the number of pieces to install
--
-- Return:
- --
+ --
-- installed - the number of pieces actually installed.
--
function EquipSet:__AddEquip(e, num)
- local slot = e.slot
+ if type(e.slots) ~= "table" then
+ return self:__AddToSlot(e, num, e.slots)
+ end
+ local added = 0
+ for _,slot in ipairs(self.slots) do
+ if added >= num then
+ return added
+ end
+ added += self:__AddToSlot(e, num-added, slot)
+ end
+ return added
+ end
+
+ --
+ -- Method: __AddToSlot
+ --
+ -- Add some equipment to a ship's specific slot.
+ -- Careful: it doesn't apply any modifier or anything else.
+ -- It just manages how much place there is in the slot.
+ -- This function does NOT check whether the slot is appropriate for the equipment.
+ --
+ -- Parameters:
+ --
+ -- e - the equipment to install
+ -- num - the number of pieces to install
+ -- slot - the name of the slot to add to.
+ --
+ -- Return:
+ --
+ -- installed - the number of pieces actually installed.
+ --
+ function EquipSet:__AddToSlot(e, num, slot)
local free = self:FreeSpace(slot)
if free <= 0 then
return 0
@@ -109,18 +142,50 @@ make_EquipSet_generator = function (specs)
-- Remove some equipment from the ship
-- Careful: it doesn't apply any modifier or anything else.
-- It just manages how much place there is in the slots.
- --
+ -- It will remove from the default slots, emptying them in the order given in e.slots.
+ -- If you wish to remove from a specific slot, use EquipSet:__RemoveFromSlot()
+ --
-- Parameters:
--
-- e - the equipment to remove
-- num - the number of pieces to remove
--
-- Return:
- --
+ --
-- removed - the number of pieces actually removed.
--
function EquipSet:__RemoveEquip(e, num)
- local slot = self.slots[e.slot]
+ if type(e.slots) ~= "table" then
+ return self:__RemoveFromSlot(e, num, e.slots)
+ end
+ local removed = 0
+ for _,slot in ipairs(self.slots) do
+ if removed >= num then
+ return removed
+ end
+ removed += self:__RemoveFromSlot(e, num - removed, slot)
+ end
+ return removed
+ end
+
+ --
+ -- Method: __RemoveFromSlot (private)
+ --
+ -- Remove some equipment from a ship's specific slot
+ -- Careful: it doesn't apply any modifier or anything else.
+ -- It just manages how much place there is in the slot.
+ --
+ -- Parameters:
+ --
+ -- e - the equipment to remove
+ -- num - the number of pieces to remove
+ -- slot - the name of the slot to remove from.
+ --
+ -- Return:
+ --
+ -- removed - the number of pieces actually removed.
+ --
+ function EquipSet:__RemoveFromSlot (e, num, slot)
if not slot[e] then
return 0
end
View
62 data/libs/Equipment.lua
@@ -34,159 +34,159 @@ end
cargo = {}
cargo.hydrogen = EquipType.new({
name="Hydrogen", description="Hydrogen",
- slots={"cargo"}, price=100,
+ slots="cargo", price=100,
modifiers={mass=1}, requirements = {},
economy_type="mining", purchasable=true
})
cargo.liquid_oxygen = EquipType.new({
name="Liquid Oxygen", description="Liquid Oxygen",
- slots={"cargo"}, price=150, modifiers={mass=1},
+ slots="cargo", price=150, modifiers={mass=1},
economy_type="mining", purchasable=true
})
cargo.water = EquipType.new({
name="Water", description="",
- slots={"cargo"}, price=120, modifiers={mass=1},
+ slots="cargo", price=120, modifiers={mass=1},
economy_type="mining", purchasable=true
})
cargo.carbon_ore = EquipType.new({
name="Carbon Ore", description="",
- slots={"cargo"}, price=500, modifiers={mass=1},
+ slots="cargo", price=500, modifiers={mass=1},
economy_type="mining", purchasable=true
})
cargo.metal_ore = EquipType.new({
name="Metal Ore", description="",
- slots={"cargo"}, price=300, modifiers={mass=1},
+ slots="cargo", price=300, modifiers={mass=1},
economy_type="mining", purchasable=true
})
cargo.metal_alloys = EquipType.new({
name="Metal Alloys", description="",
- slots={"cargo"}, price=800, modifiers={mass=1},
+ slots="cargo", price=800, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.precious_metals = EquipType.new({
name="Precious Metals", description="",
- slots={"cargo"}, price=18000, modifiers={mass=1},
+ slots="cargo", price=18000, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.plastics = EquipType.new({
name="Plastics", description="",
- slots={"cargo"}, price=1200, modifiers={mass=1},
+ slots="cargo", price=1200, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.fruit_and_veg = EquipType.new({
name="Fruits and Vegetables", description="",
- slots={"cargo"}, price=1200, modifiers={mass=1},
+ slots="cargo", price=1200, modifiers={mass=1},
economy_type="agriculture", purchasable=true
})
cargo.animal_meat = EquipType.new({
name="Animal Meat", description="",
- slots={"cargo"}, price=1800, modifiers={mass=1},
+ slots="cargo", price=1800, modifiers={mass=1},
economy_type="agriculture", purchasable=true
})
cargo.live_animals = EquipType.new({
name="Live Animals", description="",
- slots={"cargo"}, price=3200, modifiers={mass=1},
+ slots="cargo", price=3200, modifiers={mass=1},
economy_type="agriculture", purchasable=true
})
cargo.liquor = EquipType.new({
name="Liquor", description="",
- slots={"cargo"}, price=800, modifiers={mass=1},
+ slots="cargo", price=800, modifiers={mass=1},
economy_type="agriculture", purchasable=true
})
cargo.grain = EquipType.new({
name="Grain", description="",
- slots={"cargo"}, price=1000, modifiers={mass=1},
+ slots="cargo", price=1000, modifiers={mass=1},
economy_type="agriculture", purchasable=true
})
cargo.slaves = EquipType.new({
name="Slaves", description="", requirements = {},
- slots={"cargo"}, price=23200, modifiers={mass=1},
+ slots="cargo", price=23200, modifiers={mass=1},
economy_type="agriculture", purchasable=true
})
cargo.textiles = EquipType.new({
name="Textiles", description="",
- slots={"cargo"}, price=850, modifiers={mass=1},
+ slots="cargo", price=850, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.fertilizer = EquipType.new({
name="Fertilizer", description="",
- slots={"cargo"}, price=400, modifiers={mass=1},
+ slots="cargo", price=400, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.medicines = EquipType.new({
name="Medicines", description="",
- slots={"cargo"}, price=2200, modifiers={mass=1},
+ slots="cargo", price=2200, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.consumer_goods = EquipType.new({
name="Consumer Goods", description="",
- slots={"cargo"}, price=14000, modifiers={mass=1},
+ slots="cargo", price=14000, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.computers = EquipType.new({
name="Computers", description="",
- slots={"cargo"}, price=8000,
+ slots="cargo", price=8000,
modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.rubbish = EquipType.new({
name="Rubbish", description='', requirements = {},
- slots={"cargo"}, price=-10, modifiers={mass=1},
+ slots="cargo", price=-10, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.radioactives = EquipType.new({
name="Radioactive waste", description='', requirements = {},
- slots={"cargo"}, price=-35, modifiers={mass=1},
+ slots="cargo", price=-35, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.narcotics = EquipType.new({
name="Narcotics", description='', requirements = {},
- slots={"cargo"}, price=15700, modifiers={mass=1},
+ slots="cargo", price=15700, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.nerve_gas = EquipType.new({
name="Nerve gas", description='', requirements = {},
- slots={"cargo"}, price=26500, modifiers={mass=1},
+ slots="cargo", price=26500, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.military_fuel = EquipType.new({
name="Military Fuel", description='',
- slots={"cargo"}, price=6000, modifiers={mass=1},
+ slots="cargo", price=6000, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.robots = EquipType.new({
name="Robots", description='',
- slots={"cargo"}, price=6300, modifiers={mass=1},
+ slots="cargo", price=6300, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.hand_weapons = EquipType.new({
name="Hand weapons", description='',
- slots={"cargo"}, price=12400, modifiers={mass=1},
+ slots="cargo", price=12400, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.air_processors = EquipType.new({
name="Air Processors", description='',
- slots={"cargo"}, price=2000, modifiers={mass=1},
+ slots="cargo", price=2000, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.farm_machinery = EquipType.new({
name="Farm machinery", description="",
- slots={"cargo"}, price=1100, modifiers={mass=1},
+ slots="cargo", price=1100, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.industrial_machinery = EquipType.new({
name="Mining machinery", description="",
- slots={"cargo"}, price=1200, modifiers={mass=1},
+ slots="cargo", price=1200, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.battle_weapons = EquipType.new({
name="Battle weapons", description="",
- slots={"cargo"}, price=22000, modifiers={mass=1},
+ slots="cargo", price=22000, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.industrial_machinery = EquipType.new({
name="Industrial machinery", description="Industrial machinery",
- slots={"cargo"}, price=1300, modifiers={mass=1},
+ slots="cargo", price=1300, modifiers={mass=1},
economy_type="industry", purchasable=true
})
cargo.liquid_oxygen.requirements = { cargo.water, cargo.industrial_machinery }
Please sign in to comment.
Something went wrong with that request. Please try again.