Skip to content

Commit

Permalink
Improved worker automatic level-up upgrade choice
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrettin committed Dec 7, 2017
1 parent fe037f4 commit 5ed3e38
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
6 changes: 0 additions & 6 deletions src/action/action_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,14 +977,8 @@ int COrder_Resource::GatherResource(CUnit &unit)
}

// Calculate how much we can load.
//Wyrmgus start
// if (resinfo.ResourceStep) {
if (unit.GetResourceStep(this->CurrentResource) && (harvest_from_outside || Map.Info.IsPointOnMap(this->goalPos, this->MapLayer))) {
//Wyrmgus end
//Wyrmgus start
// addload = resinfo.ResourceStep;
addload = unit.GetResourceStep(this->CurrentResource);
//Wyrmgus end
} else {
//Wyrmgus start
// addload = resinfo.ResourceCapacity;
Expand Down
1 change: 1 addition & 0 deletions src/include/unittype.h
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ class CUnitType
//Wyrmgus start
void RemoveButtons(int button_action = -1, std::string mod_file = "");
int GetAvailableLevelUpUpgrades() const;
int GetResourceStep(const int resource, const int player) const;
VariationInfo *GetDefaultVariation(CPlayer &player, int image_layer = -1) const;
VariationInfo *GetVariation(std::string variation_name, int image_layer = -1) const;
std::string GetRandomVariationIdent(int image_layer = -1) const;
Expand Down
17 changes: 11 additions & 6 deletions src/unit/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,14 +715,19 @@ void CUnit::IncreaseLevel(int level_quantity, bool automatic_learning)
//if is a harvester who is currently gathering, try to upgrade to a unit type which is best for harvesting the current resource
unsigned int best_gathering_rate = 0;
for (size_t i = 0; i != AiHelpers.ExperienceUpgrades[Type->Slot].size(); ++i) {
if (CheckDependByType(*this, *AiHelpers.ExperienceUpgrades[Type->Slot][i], true)) {
if (Character == NULL || !Character->ForbiddenUpgrades[AiHelpers.ExperienceUpgrades[Type->Slot][i]->Slot]) {
if (AiHelpers.ExperienceUpgrades[Type->Slot][i]->ResInfo[this->CurrentResource] && AiHelpers.ExperienceUpgrades[Type->Slot][i]->ResInfo[this->CurrentResource]->ResourceStep >= best_gathering_rate) {
if (AiHelpers.ExperienceUpgrades[Type->Slot][i]->ResInfo[this->CurrentResource]->ResourceStep > best_gathering_rate) {
best_gathering_rate = AiHelpers.ExperienceUpgrades[Type->Slot][i]->ResInfo[this->CurrentResource]->ResourceStep;
CUnitType *experience_upgrade_type = AiHelpers.ExperienceUpgrades[Type->Slot][i];
if (CheckDependByType(*this, *experience_upgrade_type, true)) {
if (Character == NULL || !Character->ForbiddenUpgrades[experience_upgrade_type->Slot]) {
if (!experience_upgrade_type->ResInfo[this->CurrentResource]) {
continue;
}
int gathering_rate = experience_upgrade_type->GetResourceStep(this->CurrentResource, this->Player->Index);
if (gathering_rate >= best_gathering_rate) {
if (gathering_rate > best_gathering_rate) {
best_gathering_rate = gathering_rate;
potential_upgrades.clear();
}
potential_upgrades.push_back(AiHelpers.ExperienceUpgrades[Type->Slot][i]);
potential_upgrades.push_back(experience_upgrade_type);
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/unit/unittype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,41 @@ int CUnitType::GetAvailableLevelUpUpgrades() const
return value;
}

int CUnitType::GetResourceStep(const int resource, const int player) const
{
if (!this->ResInfo[resource]) {
return 0;
}

int resource_step = this->ResInfo[resource]->ResourceStep;

resource_step += this->Stats[player].Variables[GATHERINGBONUS_INDEX].Value;

if (resource == CopperCost) {
resource_step += this->Stats[player].Variables[COPPERGATHERINGBONUS_INDEX].Value;
} else if (resource == SilverCost) {
resource_step += this->Stats[player].Variables[SILVERGATHERINGBONUS_INDEX].Value;
} else if (resource == GoldCost) {
resource_step += this->Stats[player].Variables[GOLDGATHERINGBONUS_INDEX].Value;
} else if (resource == WoodCost) {
resource_step += this->Stats[player].Variables[LUMBERGATHERINGBONUS_INDEX].Value;
} else if (resource == StoneCost || resource == LimestoneCost) {
resource_step += this->Stats[player].Variables[STONEGATHERINGBONUS_INDEX].Value;
} else if (resource == CoalCost) {
resource_step += this->Stats[player].Variables[COALGATHERINGBONUS_INDEX].Value;
} else if (resource == JewelryCost) {
resource_step += this->Stats[player].Variables[JEWELRYGATHERINGBONUS_INDEX].Value;
} else if (resource == FurnitureCost) {
resource_step += this->Stats[player].Variables[FURNITUREGATHERINGBONUS_INDEX].Value;
} else if (resource == LeatherCost) {
resource_step += this->Stats[player].Variables[LEATHERGATHERINGBONUS_INDEX].Value;
} else if (resource == DiamondsCost || resource == EmeraldsCost) {
resource_step += this->Stats[player].Variables[GEMSGATHERINGBONUS_INDEX].Value;
}

return resource_step;
}

VariationInfo *CUnitType::GetDefaultVariation(CPlayer &player, int image_layer) const
{
int variation_max = image_layer == -1 ? VariationMax : this->LayerVarInfo[image_layer].size();
Expand Down

0 comments on commit 5ed3e38

Please sign in to comment.