Skip to content

Commit

Permalink
Update resource needs when popping requests
Browse files Browse the repository at this point in the history
  • Loading branch information
neivv committed Feb 19, 2019
1 parent 055b4d3 commit 8ea165c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aise"
version = "2.21.2"
version = "2.21.3"
authors = ["Markus Heikkinen <ittevien@gmail.com>"]

[lib]
Expand Down
23 changes: 23 additions & 0 deletions src/ai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ impl PlayerAi {
}
}

/// spent_money is true when the ai actually built something, false on failures
pub fn remove_resource_need(&self, cost: &Cost, spent_money: bool) {
unsafe {
(*self.0).mineral_need = (*self.0).mineral_need.saturating_sub(cost.minerals);
(*self.0).gas_need = (*self.0).gas_need.saturating_sub(cost.supply);
(*self.0).supply_need = (*self.0).supply_need.saturating_sub(cost.supply);
if spent_money {
(*self.0).minerals_available =
(*self.0).minerals_available.saturating_sub(cost.minerals);
(*self.0).gas_available = (*self.0).gas_available.saturating_sub(cost.gas);
(*self.0).supply_available = (*self.0).supply_available.saturating_sub(cost.supply);
}
}
}

pub fn pop_request(&self) {
let requests;
let new_count;
Expand Down Expand Up @@ -264,6 +279,14 @@ pub struct Cost {
pub races: RaceFlags,
}

pub fn request_cost(request: &bw::AiSpendingRequest) -> Cost {
match request.ty {
5 => upgrade_cost(UpgradeId(request.id)),
6 => tech_cost(TechId(request.id)),
_ => unit_cost(UnitId(request.id)),
}
}

pub fn unit_cost(unit: UnitId) -> Cost {
let dual_birth = unit.flags() & 0x400 != 0;
Cost {
Expand Down
4 changes: 3 additions & 1 deletion src/ai_spending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub unsafe fn frame_hook(ai_mode: &[AiMode; 8]) {
let player_ai = PlayerAi::get(player);
while let Some(request) = player_ai.first_request() {
let can = can_satisfy_request(game, player, &request, ai_mode);
let mut handled = false;
if can {
let mut handled = false;
// Handle building morphs since preplaced colonies don't check for requests
// (Since their order switches to idle because they don't have a town on frame 0)
// Could handle other requests as well, but no need at the moment.
Expand All @@ -31,6 +31,8 @@ pub unsafe fn frame_hook(ai_mode: &[AiMode; 8]) {
break;
}
}
let cost = ai::request_cost(&request);
player_ai.remove_resource_need(&cost, handled);
player_ai.pop_request()
}
}
Expand Down

0 comments on commit 8ea165c

Please sign in to comment.