Adds a buffer for sold aircraft/vehicles.

This feature allows you to cancel the sales of aircraft or ground vehicles if needed.

Upon clicking the minus button, a count of sold units will be appended to the unit count. This count responds to both further presses of the minus button, and also to presses of the plus button. No further units will be requested for the next turn until all sold units have been re-bought.

I've tested a bunch of different scenarios with this:

- Selling and rebuying a unit - the budget increases and decreases as expected.

- Selling one unit, buying a unit worth the new player budget, and then trying to rebuy the old unit - the old unit cannot be rebought until the budget has been freed up for it.

- Closing the base window and re-opening it - the sold unit count is retained.

- Ending the turn - the sold unit count is reset back to 0 as expected.

Contributes to Khopa/dcs_liberation#365
This commit is contained in:
Simon Clark 2021-01-04 19:27:29 +00:00
parent 34bdc0e80b
commit 366ac4ee14
5 changed files with 39 additions and 7 deletions

View File

@ -98,6 +98,7 @@ class GlobalAircraftInventory:
inventory = self.inventories[control_point] inventory = self.inventories[control_point]
for aircraft, count in control_point.base.aircraft.items(): for aircraft, count in control_point.base.aircraft.items():
inventory.add_aircraft(aircraft, count) inventory.add_aircraft(aircraft, count)
control_point.base.sold_units[aircraft] = 0
def for_control_point( def for_control_point(
self, self,

View File

@ -29,6 +29,7 @@ class Base:
self.aa: Dict[AirDefence, int] = {} self.aa: Dict[AirDefence, int] = {}
self.commision_points: Dict[Type, float] = {} self.commision_points: Dict[Type, float] = {}
self.strength = 1 self.strength = 1
self.sold_units = {}
@property @property
def total_aircraft(self) -> int: def total_aircraft(self) -> int:

View File

@ -117,6 +117,7 @@ class GroundPlanner:
print(key) print(key)
continue continue
self.cp.base.sold_units[key] = 0
available = self.cp.base.armor[key] available = self.cp.base.armor[key]
while available > 0: while available > 0:

View File

@ -61,6 +61,12 @@ class QRecruitBehaviour:
unitName = QLabel("<b>" + db.unit_type_name_2(unit_type) + "</b>") unitName = QLabel("<b>" + db.unit_type_name_2(unit_type) + "</b>")
unitName.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) unitName.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
sold_count = self.cp.base.sold_units.get(unit_type)
if sold_count is None:
sold_count = 0
if sold_count > 0:
existing_units = QLabel("<b>{} (-{})</b>".format(existing_units, sold_count))
else:
existing_units = QLabel(str(existing_units)) existing_units = QLabel(str(existing_units))
existing_units.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)) existing_units.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed))
@ -117,6 +123,14 @@ class QRecruitBehaviour:
unit_type in self.pending_deliveries.units and "{}".format(self.pending_deliveries.units[unit_type]) or "0" unit_type in self.pending_deliveries.units and "{}".format(self.pending_deliveries.units[unit_type]) or "0"
)) ))
sold_count = self.cp.base.sold_units.get(unit_type)
if sold_count is None:
sold_count = 0
if sold_count > 0:
self.existing_units_labels[unit_type].setText("<b>{} (-{})</b>".format(
self.cp.base.total_units_of_type(unit_type), self.cp.base.sold_units[unit_type]
))
else:
self.existing_units_labels[unit_type].setText("<b>{}</b>".format( self.existing_units_labels[unit_type].setText("<b>{}</b>".format(
self.cp.base.total_units_of_type(unit_type) self.cp.base.total_units_of_type(unit_type)
)) ))
@ -127,6 +141,14 @@ class QRecruitBehaviour:
def buy(self, unit_type: Type[UnitType]): def buy(self, unit_type: Type[UnitType]):
price = db.PRICES[unit_type] price = db.PRICES[unit_type]
if self.budget >= price: if self.budget >= price:
sold_count = self.cp.base.sold_units.get(unit_type)
if sold_count is None:
sold_count = 0
if sold_count > 0:
self.cp.base.sold_units[unit_type] -= 1
self.cp.base.commision_units({unit_type: 1})
self.budget -= price
else:
self.pending_deliveries.order({unit_type: 1}) self.pending_deliveries.order({unit_type: 1})
self.budget -= price self.budget -= price
else: else:
@ -146,6 +168,7 @@ class QRecruitBehaviour:
price = db.PRICES[unit_type] price = db.PRICES[unit_type]
self.budget += price self.budget += price
self.cp.base.commit_losses({unit_type: 1}) self.cp.base.commit_losses({unit_type: 1})
self.cp.base.sold_units[unit_type] += 1
self._update_count_label(unit_type) self._update_count_label(unit_type)
self.update_available_budget() self.update_available_budget()

View File

@ -85,6 +85,8 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
self.setLayout(main_layout) self.setLayout(main_layout)
def buy(self, unit_type): def buy(self, unit_type):
global_inventory = self.game_model.game.aircraft_inventory
inventory = global_inventory.for_control_point(self.cp)
if self.maximum_units > 0: if self.maximum_units > 0:
if self.cp.unclaimed_parking(self.game_model.game) <= 0: if self.cp.unclaimed_parking(self.game_model.game) <= 0:
logging.debug(f"No space for additional aircraft at {self.cp}.") logging.debug(f"No space for additional aircraft at {self.cp}.")
@ -92,7 +94,11 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
self, "No space for additional aircraft", self, "No space for additional aircraft",
f"There is no parking space left at {self.cp.name} to accommodate another plane.", QMessageBox.Ok) f"There is no parking space left at {self.cp.name} to accommodate another plane.", QMessageBox.Ok)
return return
sold_count = self.cp.base.sold_units.get(unit_type)
if sold_count is None:
sold_count = 0
if sold_count > 0:
inventory.add_aircraft(unit_type, 1)
super().buy(unit_type) super().buy(unit_type)
self.hangar_status.update_label() self.hangar_status.update_label()