mirror of
https://github.com/mod-playerbots/mod-playerbots.git
synced 2026-02-26 05:15:54 +00:00
Fix rest of trainers' related stuff + codestyle changes and corrections (#2104)
# Pull Request
* Fix the rest of the trainer-related functionality: list spells and
learn (cast vs. direct learn) spells.
* Rewrite `TrainerAction`: split the logic between appropriate methods
(`GetTarget`, `isUseful`, `isPossible`) instead of pushing everything
inside a single `Execute` method.
* Change method definitions to remove unnecessary declarations and
parameters overhead.
* Move the `Trainer` header into the implementation. Rewrite
`RpgTrainTrigger` to fit the original logic and move all validation to
`RpgTrainAction` (`isUseful` + `isPossible`).
* Implement "can train" context value calculation to use with
`RpgTrainTrigger`.
* Update and optimize "train cost" context value calculation -- it
should be much faster.
* Replace `AiPlayerbot.AutoTrainSpells` with
`AiPlayerbot.AllowLearnTrainerSpells` and remove the "free" value
behavior — please use `AiPlayerbot.BotCheats` if you want bots to learn
trainer's spells for "free".
* Add `nullptr` checks wherever necessary (only inside targeted
methods/functions).
* Make some codestyle changes and corrections based on the AC codestyle
guide.
---
## Design Philosophy
We prioritize **stability, performance, and predictability** over
behavioral realism.
Complex player-mimicking logic is intentionally limited due to its
negative impact on scalability, maintainability, and
long-term robustness.
Excessive processing overhead can lead to server hiccups, increased CPU
usage, and degraded performance for all
participants. Because every action and
decision tree is executed **per bot and per trigger**, even small
increases in logic complexity can scale poorly and
negatively affect both players and
world (random) bots. Bots are not expected to behave perfectly, and
perfect simulation of human decision-making is not a
project goal. Increased behavioral
realism often introduces disproportionate cost, reduced predictability,
and significantly higher maintenance overhead.
Every additional branch of logic increases long-term responsibility. All
decision paths must be tested, validated, and
maintained continuously as the system evolves.
If advanced or AI-intensive behavior is introduced, the **default
configuration must remain the lightweight decision
model**. More complex behavior should only be
available as an **explicit opt-in option**, clearly documented as having
a measurable performance cost.
Principles:
- **Stability before intelligence**
A stable system is always preferred over a smarter one.
- **Performance is a shared resource**
Any increase in bot cost affects all players and all bots.
- **Simple logic scales better than smart logic**
Predictable behavior under load is more valuable than perfect decisions.
- **Complexity must justify itself**
If a feature cannot clearly explain its cost, it should not exist.
- **Defaults must be cheap**
Expensive behavior must always be optional and clearly communicated.
- **Bots should look reasonable, not perfect**
The goal is believable behavior, not human simulation.
Before submitting, confirm that this change aligns with those
principles.
---
## How to Test the Changes
Force bots to learn spells from trainers using the chat command `trainer
learn` or `trainer learn <spellId>`. Bots should properly list available
spells (`trainer` command) or learn them (based on configuration and
command).
## Complexity & Impact
- Does this change add new decision branches?
- [x] No
- [ ] Yes (**explain below**)
- Does this change increase per-bot or per-tick processing?
- [x] No
- [ ] Yes (**describe and justify impact**)
- Could this logic scale poorly under load?
- [x] No
- [ ] Yes (**explain why**)
---
## Defaults & Configuration
- Does this change modify default bot behavior?
- [x] No
- [ ] Yes (**explain why**)
If this introduces more advanced or AI-heavy logic:
- [x] Lightweight mode remains the default
- [ ] More complex behavior is optional and thereby configurable
---
## AI Assistance
- Was AI assistance (e.g. ChatGPT or similar tools) used while working
on this change?
- [x] No
- [ ] Yes (**explain below**)
If yes, please specify:
- AI tool or model used (e.g. ChatGPT, GPT-4, Claude, etc.)
- Purpose of usage (e.g. brainstorming, refactoring, documentation, code
generation)
- Which parts of the change were influenced or generated
- Whether the result was manually reviewed and adapted
AI assistance is allowed, but all submitted code must be fully
understood, reviewed, and owned by the contributor.
Any AI-influenced changes must be verified against existing CORE and PB
logic. We expect contributors to be honest
about what they do and do not understand.
---
## Final Checklist
- [x] Stability is not compromised
- [x] Performance impact is understood, tested, and acceptable
- [x] Added logic complexity is justified and explained
- [x] Documentation updated if needed
---
## Notes for Reviewers
Anything that significantly improves realism at the cost of stability or
performance should be carefully discussed
before merging.
---------
Co-authored-by: bashermens <31279994+hermensbas@users.noreply.github.com>
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "RpgSubActions.h"
|
||||
|
||||
#include "BudgetValues.h"
|
||||
#include "ChooseRpgTargetAction.h"
|
||||
#include "EmoteAction.h"
|
||||
#include "Formations.h"
|
||||
@@ -53,7 +54,11 @@ ObjectGuid RpgHelper::guid() { return (ObjectGuid)guidP(); }
|
||||
|
||||
bool RpgHelper::InRange()
|
||||
{
|
||||
return guidP() ? (guidP().sqDistance2d(bot) < INTERACTION_DISTANCE * INTERACTION_DISTANCE) : false;
|
||||
GuidPosition gp = guidP();
|
||||
if (!gp)
|
||||
return false;
|
||||
|
||||
return gp.sqDistance2d(bot) < INTERACTION_DISTANCE * INTERACTION_DISTANCE;
|
||||
}
|
||||
|
||||
void RpgHelper::setFacingTo(GuidPosition guidPosition)
|
||||
@@ -250,6 +255,60 @@ Event RpgSellAction::ActionEvent(Event /*event*/) { return Event("rpg action", "
|
||||
|
||||
std::string const RpgRepairAction::ActionName() { return "repair"; }
|
||||
|
||||
bool RpgTrainAction::isUseful()
|
||||
{
|
||||
if (!rpg->InRange())
|
||||
return false;
|
||||
|
||||
Creature* creature = rpg->guidP().GetCreature();
|
||||
if (!creature)
|
||||
return false;
|
||||
|
||||
if (!creature->IsInWorld() || creature->IsDuringRemoveFromWorld() || !creature->IsAlive())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RpgTrainAction::isPossible()
|
||||
{
|
||||
GuidPosition gp = rpg->guidP();
|
||||
|
||||
CreatureTemplate const* cinfo = gp.GetCreatureTemplate();
|
||||
if (!cinfo)
|
||||
return false;
|
||||
|
||||
Trainer::Trainer* trainer = sObjectMgr->GetTrainer(cinfo->Entry);
|
||||
if (!trainer)
|
||||
return false;
|
||||
|
||||
if (!trainer->IsTrainerValidForPlayer(bot))
|
||||
return false;
|
||||
|
||||
FactionTemplateEntry const* factionTemplate = sFactionTemplateStore.LookupEntry(cinfo->faction);
|
||||
float reputationDiscount = bot->GetReputationPriceDiscount(factionTemplate);
|
||||
uint32 currentGold = AI_VALUE2(uint32, "free money for", (uint32)NeedMoneyFor::spells);
|
||||
|
||||
for (auto& spell : trainer->GetSpells())
|
||||
{
|
||||
Trainer::Spell const* trainerSpell = trainer->GetSpell(spell.SpellId);
|
||||
if (!trainerSpell)
|
||||
continue;
|
||||
|
||||
if (!trainer->CanTeachSpell(bot, trainerSpell))
|
||||
continue;
|
||||
|
||||
if (currentGold < static_cast<uint32>(floor(trainerSpell->MoneyCost * reputationDiscount)))
|
||||
continue;
|
||||
|
||||
// we only check if at least one spell can be learned from the trainer;
|
||||
// otherwise, the train action should not be allowed
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string const RpgTrainAction::ActionName() { return "trainer"; }
|
||||
|
||||
bool RpgHealAction::Execute(Event /*event*/)
|
||||
|
||||
@@ -165,6 +165,9 @@ class RpgTrainAction : public RpgSubAction
|
||||
public:
|
||||
RpgTrainAction(PlayerbotAI* botAI, std::string const name = "rpg train") : RpgSubAction(botAI, name) {}
|
||||
|
||||
bool isPossible() override;
|
||||
bool isUseful() override;
|
||||
|
||||
private:
|
||||
std::string const ActionName() override;
|
||||
};
|
||||
|
||||
@@ -9,77 +9,120 @@
|
||||
#include "Event.h"
|
||||
#include "PlayerbotFactory.h"
|
||||
#include "Playerbots.h"
|
||||
#include "Trainer.h"
|
||||
|
||||
void TrainerAction::Learn(uint32 cost, const Trainer::Spell tSpell, std::ostringstream& msg)
|
||||
bool TrainerAction::Execute(Event event)
|
||||
{
|
||||
if (sPlayerbotAIConfig.autoTrainSpells != "free" && !botAI->HasCheat(BotCheatMask::gold))
|
||||
{
|
||||
if (AI_VALUE2(uint32, "free money for", (uint32)NeedMoneyFor::spells) < cost)
|
||||
{
|
||||
msg << " - too expensive";
|
||||
return;
|
||||
}
|
||||
std::string const param = event.getParam();
|
||||
|
||||
bot->ModifyMoney(-int32(cost));
|
||||
}
|
||||
Creature* target = GetCreatureTarget();
|
||||
if (!target)
|
||||
return false;
|
||||
|
||||
SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(tSpell.SpellId);
|
||||
if (!spellInfo)
|
||||
return;
|
||||
Trainer::Trainer* trainer = sObjectMgr->GetTrainer(target->GetEntry());
|
||||
if (!trainer)
|
||||
return false;
|
||||
|
||||
bool learned = false;
|
||||
for (uint8 j = 0; j < 3; ++j)
|
||||
{
|
||||
if (spellInfo->Effects[j].Effect == SPELL_EFFECT_LEARN_SPELL)
|
||||
{
|
||||
uint32 learnedSpell = spellInfo->Effects[j].TriggerSpell;
|
||||
if (!bot->HasSpell(learnedSpell))
|
||||
{
|
||||
bot->learnSpell(learnedSpell);
|
||||
learned = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// NOTE: Original version uses SpellIds here, but occasionally only inserts
|
||||
// a single spell ID value from parameters. If someone wants to impl multiple
|
||||
// spells as parameters, check SkipSpellsListAction::parseIds as an example.
|
||||
uint32 spellId = chat->parseSpell(param);
|
||||
|
||||
if (!learned && !bot->HasSpell(tSpell.SpellId))
|
||||
bot->learnSpell(tSpell.SpellId);
|
||||
bool learnSpells = param.find("learn") != std::string::npos || sRandomPlayerbotMgr.IsRandomBot(bot) ||
|
||||
(sPlayerbotAIConfig.allowLearnTrainerSpells &&
|
||||
// TODO: Rewrite to only exclude start primary profession skills and make config dependent.
|
||||
(trainer->GetTrainerType() != Trainer::Type::Tradeskill || !botAI->HasActivePlayerMaster()));
|
||||
|
||||
msg << " - learned";
|
||||
Iterate(target, learnSpells, spellId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TrainerAction::Iterate(Creature* creature, TrainerSpellAction action, SpellIds& spells)
|
||||
bool TrainerAction::isUseful()
|
||||
{
|
||||
Creature* target = GetCreatureTarget();
|
||||
if (!target)
|
||||
return false;
|
||||
|
||||
if (!target->IsInWorld() || target->IsDuringRemoveFromWorld() || !target->IsAlive())
|
||||
return false;
|
||||
|
||||
return target->IsTrainer();
|
||||
}
|
||||
|
||||
bool TrainerAction::isPossible()
|
||||
{
|
||||
Creature* target = GetCreatureTarget();
|
||||
if (!target)
|
||||
return false;
|
||||
|
||||
Trainer::Trainer* trainer = sObjectMgr->GetTrainer(target->GetEntry());
|
||||
if (!trainer)
|
||||
return false;
|
||||
|
||||
if (!trainer->IsTrainerValidForPlayer(bot))
|
||||
return false;
|
||||
|
||||
if (trainer->GetSpells().empty())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Unit* TrainerAction::GetTarget()
|
||||
{
|
||||
// There are just two scenarios: the bot has a master or it doesn't. If the
|
||||
// bot has a master, the master should target a unit; otherwise, the bot
|
||||
// should target the unit itself.
|
||||
if (Player* master = GetMaster())
|
||||
return master->GetSelectedUnit();
|
||||
|
||||
return bot->GetSelectedUnit();
|
||||
}
|
||||
|
||||
Creature* TrainerAction::GetCreatureTarget()
|
||||
{
|
||||
Unit* target = GetTarget();
|
||||
return target ? target->ToCreature() : nullptr;
|
||||
}
|
||||
|
||||
void TrainerAction::Iterate(Creature* creature, bool learnSpells, uint32 spellId)
|
||||
{
|
||||
TellHeader(creature);
|
||||
|
||||
Trainer::Trainer* trainer = sObjectMgr->GetTrainer(creature->GetEntry());
|
||||
|
||||
if (!trainer)
|
||||
return;
|
||||
|
||||
float fDiscountMod = bot->GetReputationPriceDiscount(creature);
|
||||
float reputationDiscount = bot->GetReputationPriceDiscount(creature);
|
||||
uint32 totalCost = 0;
|
||||
|
||||
for (auto& spell : trainer->GetSpells())
|
||||
{
|
||||
if (!trainer->CanTeachSpell(bot, trainer->GetSpell(spell.SpellId)))
|
||||
// simplified version of Trainer::TeachSpell method
|
||||
|
||||
Trainer::Spell const* trainerSpell = trainer->GetSpell(spell.SpellId);
|
||||
if (!trainerSpell)
|
||||
continue;
|
||||
|
||||
if (!spells.empty() && spells.find(spell.SpellId) == spells.end())
|
||||
if (!trainer->CanTeachSpell(bot, trainerSpell))
|
||||
continue;
|
||||
|
||||
SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(spell.SpellId);
|
||||
if (spellId && trainerSpell->SpellId != spellId)
|
||||
continue;
|
||||
|
||||
SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(trainerSpell->SpellId);
|
||||
if (!spellInfo)
|
||||
continue;
|
||||
|
||||
uint32 cost = uint32(floor(spell.MoneyCost * fDiscountMod));
|
||||
uint32 cost = static_cast<uint32>(floor(trainerSpell->MoneyCost * reputationDiscount));
|
||||
totalCost += cost;
|
||||
|
||||
std::ostringstream out;
|
||||
out << chat->FormatSpell(spellInfo) << chat->formatMoney(cost);
|
||||
|
||||
if (action)
|
||||
(this->*action)(cost, spell, out);
|
||||
if (learnSpells)
|
||||
Learn(spellInfo, cost, out);
|
||||
|
||||
botAI->TellMaster(out);
|
||||
}
|
||||
@@ -87,55 +130,25 @@ void TrainerAction::Iterate(Creature* creature, TrainerSpellAction action, Spell
|
||||
TellFooter(totalCost);
|
||||
}
|
||||
|
||||
bool TrainerAction::Execute(Event event)
|
||||
void TrainerAction::Learn(SpellInfo const* spellInfo, uint32 cost, std::ostringstream& out)
|
||||
{
|
||||
std::string const text = event.getParam();
|
||||
|
||||
Player* master = GetMaster();
|
||||
|
||||
Creature* creature = botAI->GetCreature(bot->GetTarget());
|
||||
|
||||
if (master)
|
||||
if (!botAI->HasCheat(BotCheatMask::gold))
|
||||
{
|
||||
creature = master->GetSelectedUnit() ? master->GetSelectedUnit()->ToCreature() : nullptr;
|
||||
}
|
||||
// if (AI_VALUE(GuidPosition, "rpg target") != bot->GetTarget())
|
||||
// if (master)
|
||||
// creature = botAI->GetCreature(master->GetTarget());
|
||||
// else
|
||||
// return false;
|
||||
if (AI_VALUE2(uint32, "free money for", (uint32)NeedMoneyFor::spells) < cost)
|
||||
{
|
||||
out << " - too expensive";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!creature || !creature->IsTrainer())
|
||||
return false;
|
||||
|
||||
Trainer::Trainer* trainer = sObjectMgr->GetTrainer(creature->GetEntry());
|
||||
|
||||
if (!trainer || !trainer->IsTrainerValidForPlayer(bot))
|
||||
return false;
|
||||
|
||||
std::vector<Trainer::Spell> trainer_spells = trainer->GetSpells();
|
||||
|
||||
if (trainer_spells.empty())
|
||||
{
|
||||
botAI->TellError("No spells can be learned from this trainer");
|
||||
return false;
|
||||
bot->ModifyMoney(-static_cast<int32>(cost));
|
||||
}
|
||||
|
||||
uint32 spell = chat->parseSpell(text);
|
||||
SpellIds spells;
|
||||
if (spell)
|
||||
spells.insert(spell);
|
||||
|
||||
if (text.find("learn") != std::string::npos || sRandomPlayerbotMgr.IsRandomBot(bot) ||
|
||||
(sPlayerbotAIConfig.autoTrainSpells != "no" &&
|
||||
(trainer->GetTrainerType() != Trainer::Type::Tradeskill ||
|
||||
!botAI->HasActivePlayerMaster()))) // Todo rewrite to only exclude start primary profession skills and make
|
||||
// config dependent.
|
||||
Iterate(creature, &TrainerAction::Learn, spells);
|
||||
if (spellInfo->HasEffect(SPELL_EFFECT_LEARN_SPELL))
|
||||
bot->CastSpell(bot, spellInfo->Id, true);
|
||||
else
|
||||
Iterate(creature, nullptr, spells);
|
||||
bot->learnSpell(spellInfo->Id, false);
|
||||
|
||||
return true;
|
||||
out << " - learned";
|
||||
}
|
||||
|
||||
void TrainerAction::TellHeader(Creature* creature)
|
||||
@@ -245,7 +258,8 @@ bool MaintenanceAction::Execute(Event /*event*/)
|
||||
if (sPlayerbotAIConfig.altMaintenanceKeyring)
|
||||
factory.InitKeyring();
|
||||
|
||||
if (sPlayerbotAIConfig.altMaintenanceGemsEnchants && bot->GetLevel() >= sPlayerbotAIConfig.minEnchantingBotLevel)
|
||||
if (sPlayerbotAIConfig.altMaintenanceGemsEnchants &&
|
||||
bot->GetLevel() >= sPlayerbotAIConfig.minEnchantingBotLevel)
|
||||
factory.ApplyEnchantAndGemsNew();
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
#include "Action.h"
|
||||
#include "ChatHelper.h"
|
||||
#include "Trainer.h"
|
||||
|
||||
class Creature;
|
||||
class PlayerbotAI;
|
||||
@@ -21,11 +20,14 @@ public:
|
||||
TrainerAction(PlayerbotAI* botAI) : Action(botAI, "trainer") {}
|
||||
|
||||
bool Execute(Event event) override;
|
||||
bool isUseful() override;
|
||||
bool isPossible() override;
|
||||
Unit* GetTarget() override;
|
||||
|
||||
private:
|
||||
typedef void (TrainerAction::*TrainerSpellAction)(uint32, const Trainer::Spell, std::ostringstream& msg);
|
||||
void Iterate(Creature* creature, TrainerSpellAction action, SpellIds& spells);
|
||||
void Learn(uint32 cost, const Trainer::Spell tSpell, std::ostringstream& msg);
|
||||
Creature* GetCreatureTarget();
|
||||
void Iterate(Creature* creature, bool learnSpells, uint32 spellId);
|
||||
void Learn(SpellInfo const* spellInfo, uint32 cost, std::ostringstream& out);
|
||||
void TellHeader(Creature* creature);
|
||||
void TellFooter(uint32 totalCost);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user