From 7ee5d2b30033a83828dbdf0c95400dcac433a029 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 28 May 2013 00:59:44 +0000 Subject: [PATCH] added a bunch of new lua methods for Tigers new scenario to montior changes to a unit --- source/glest_game/game/script_manager.cpp | 170 +++++++++++++++++++ source/glest_game/game/script_manager.h | 28 ++++ source/glest_game/type_instances/unit.cpp | 193 ++++++++++++++++------ source/glest_game/type_instances/unit.h | 2 +- 4 files changed, 338 insertions(+), 55 deletions(-) diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index f478386f..b2981dbc 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -214,6 +214,9 @@ ScriptManager::ScriptManager() { currentCellTriggeredEventAreaExitUnitId = 0; lastDayNightTriggerStatus = 0; registeredDayNightEvent = false; + + lastUnitTriggerEventUnitId = -1; + lastUnitTriggerEventType = utet_None; } ScriptManager::~ScriptManager() { @@ -389,6 +392,13 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro luaScript.registerFunction(registerDayNightEvent, "registerDayNightEvent"); luaScript.registerFunction(unregisterDayNightEvent, "unregisterDayNightEvent"); + luaScript.registerFunction(registerUnitTriggerEvent, "registerUnitTriggerEvent"); + luaScript.registerFunction(unregisterUnitTriggerEvent, "unregisterUnitTriggerEvent"); + luaScript.registerFunction(getLastUnitTriggerEventUnitId, "lastUnitTriggerEventUnit"); + luaScript.registerFunction(getLastUnitTriggerEventType, "lastUnitTriggerEventType"); + luaScript.registerFunction(getUnitProperty, "getUnitProperty"); + luaScript.registerFunction(getUnitPropertyName, "getUnitPropertyName"); + //load code for(int i= 0; igetScriptCount(); ++i){ const Script* script= scenario->getScript(i); @@ -1792,6 +1802,106 @@ bool ScriptManager::getAttackWarningsEnabled() { return world->getAttackWarningsEnabled(); } +void ScriptManager::registerUnitTriggerEvent(int unitId) { + UnitTriggerEventList[unitId]=utet_None; +} + +void ScriptManager::unregisterUnitTriggerEvent(int unitId) { + UnitTriggerEventList.erase(unitId); +} + +int ScriptManager::getLastUnitTriggerEventUnitId() { + return lastUnitTriggerEventUnitId; +} +UnitTriggerEventType ScriptManager::getLastUnitTriggerEventType() { + return lastUnitTriggerEventType; +} + +int ScriptManager::getUnitProperty(int unitId, UnitTriggerEventType type) { + int result = -1; + Unit *unit= world->findUnitById(unitId); + if(unit != NULL) { + switch(type) { + case utet_None: + result = -2; + break; + case utet_HPChanged: + result = unit->getHp(); + break; + case utet_EPChanged: + result = unit->getEp(); + break; + case utet_LevelChanged: + result = -3; + if(unit->getLevel() != NULL) { + result = unit->getLevel()->getKills(); + } + break; + case utet_FieldChanged: + result = unit->getCurrField(); + break; + case utet_SkillChanged: + result = -4; + if(unit->getCurrSkill() != NULL) { + result = unit->getCurrSkill()->getClass(); + } + break; + default: + result = -1000; + break; + } + } + return result; +} +const string ScriptManager::getUnitPropertyName(int unitId, UnitTriggerEventType type) { + string result = ""; + Unit *unit= world->findUnitById(unitId); + if(unit != NULL) { + switch(type) { + case utet_None: + result = ""; + break; + case utet_HPChanged: + result = ""; + break; + case utet_EPChanged: + result = ""; + break; + case utet_LevelChanged: + result = ""; + if(unit->getLevel() != NULL) { + result = unit->getLevel()->getName(false); + } + break; + case utet_FieldChanged: + result = ""; + break; + case utet_SkillChanged: + result = ""; + if(unit->getCurrSkill() != NULL) { + result = unit->getCurrSkill()->getName(); + } + break; + default: + result = "???"; + break; + } + } + return result; +} + +void ScriptManager::onUnitTriggerEvent(const Unit *unit, UnitTriggerEventType event) { + if(UnitTriggerEventList.empty() == false) { + std::map::iterator iterFind = UnitTriggerEventList.find(unit->getId()); + if(iterFind != UnitTriggerEventList.end()) { + lastUnitTriggerEventUnitId = unit->getId(); + lastUnitTriggerEventType = event; + + luaScript.beginCall("unitTriggerEvent"); + luaScript.endCall(); + } + } +} void ScriptManager::registerDayNightEvent() { registeredDayNightEvent = true; @@ -2917,6 +3027,41 @@ int ScriptManager::unregisterDayNightEvent(LuaHandle* luaHandle) { return luaArguments.getReturnCount(); } +int ScriptManager::registerUnitTriggerEvent(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->registerUnitTriggerEvent(luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} +int ScriptManager::unregisterUnitTriggerEvent(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->unregisterUnitTriggerEvent(luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} +int ScriptManager::getLastUnitTriggerEventUnitId(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + luaArguments.returnInt(thisScriptManager->getLastUnitTriggerEventUnitId()); + return luaArguments.getReturnCount(); +} +int ScriptManager::getLastUnitTriggerEventType(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + luaArguments.returnInt(thisScriptManager->getLastUnitTriggerEventType()); + return luaArguments.getReturnCount(); +} + +int ScriptManager::getUnitProperty(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + int value = thisScriptManager->getUnitProperty(luaArguments.getInt(-2),static_cast(luaArguments.getInt(-1))); + luaArguments.returnInt(value); + return luaArguments.getReturnCount(); +} +int ScriptManager::getUnitPropertyName(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + const string unitname = thisScriptManager->getUnitPropertyName(luaArguments.getInt(-2),static_cast(luaArguments.getInt(-1))); + luaArguments.returnString(unitname); + return luaArguments.getReturnCount(); +} + + void ScriptManager::saveGame(XmlNode *rootNode) { std::map mapTagReplacements; XmlNode *scriptManagerNode = rootNode->addChild("ScriptManager"); @@ -3022,6 +3167,16 @@ void ScriptManager::saveGame(XmlNode *rootNode) { scriptManagerNode->addAttribute("registeredDayNightEvent",intToStr(registeredDayNightEvent), mapTagReplacements); scriptManagerNode->addAttribute("lastDayNightTriggerStatus",intToStr(lastDayNightTriggerStatus), mapTagReplacements); + for(std::map::iterator iterMap = UnitTriggerEventList.begin(); + iterMap != UnitTriggerEventList.end(); ++iterMap) { + XmlNode *unitTriggerEventListNode = scriptManagerNode->addChild("UnitTriggerEventList"); + + unitTriggerEventListNode->addAttribute("unitId",intToStr(iterMap->first), mapTagReplacements); + unitTriggerEventListNode->addAttribute("evenType",intToStr(iterMap->second), mapTagReplacements); + } + scriptManagerNode->addAttribute("lastUnitTriggerEventUnitId",intToStr(lastUnitTriggerEventUnitId), mapTagReplacements); + scriptManagerNode->addAttribute("lastUnitTriggerEventType",intToStr(lastUnitTriggerEventType), mapTagReplacements); + luaScript.saveGame(scriptManagerNode); } @@ -3139,6 +3294,21 @@ void ScriptManager::loadGame(const XmlNode *rootNode) { lastDayNightTriggerStatus = scriptManagerNode->getAttribute("lastDayNightTriggerStatus")->getIntValue(); } + vector unitTriggerEventListNodeList = scriptManagerNode->getChildList("UnitTriggerEventList"); + for(unsigned int i = 0; i < unitTriggerEventListNodeList.size(); ++i) { + XmlNode *node = unitTriggerEventListNodeList[i]; + + int unitId = node->getAttribute("unitId")->getIntValue(); + UnitTriggerEventType evenType = static_cast(node->getAttribute("eventType")->getIntValue()); + UnitTriggerEventList[unitId] = evenType; + } + if(scriptManagerNode->hasAttribute("lastUnitTriggerEventUnitId") == true) { + lastUnitTriggerEventUnitId = scriptManagerNode->getAttribute("lastUnitTriggerEventUnitId")->getIntValue(); + } + if(scriptManagerNode->hasAttribute("lastUnitTriggerEventType") == true) { + lastUnitTriggerEventType = static_cast(scriptManagerNode->getAttribute("lastUnitTriggerEventType")->getIntValue()); + } + luaScript.loadGame(scriptManagerNode); } diff --git a/source/glest_game/game/script_manager.h b/source/glest_game/game/script_manager.h index 37a18162..e09bb88f 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -95,6 +95,15 @@ private: // class ScriptManager // ===================================================== +enum UnitTriggerEventType { + utet_None = 0, + utet_HPChanged = 1, + utet_EPChanged, + utet_LevelChanged, + utet_FieldChanged, + utet_SkillChanged +}; + enum CellTriggerEventType { ctet_Unit, ctet_UnitPos, @@ -200,6 +209,10 @@ private: bool registeredDayNightEvent; int lastDayNightTriggerStatus; + std::map UnitTriggerEventList; + int lastUnitTriggerEventUnitId; + UnitTriggerEventType lastUnitTriggerEventType; + RandomGen random; const XmlNode *rootNode; @@ -234,6 +247,7 @@ public: void onCellTriggerEvent(Unit *movingUnit); void onTimerTriggerEvent(); void onDayNightTriggerEvent(); + void onUnitTriggerEvent(const Unit *unit, UnitTriggerEventType event); bool getGameWon() const; bool getIsGameOver() const; @@ -391,6 +405,13 @@ private: void registerDayNightEvent(); void unregisterDayNightEvent(); + void registerUnitTriggerEvent(int unitId); + void unregisterUnitTriggerEvent(int unitId); + int getLastUnitTriggerEventUnitId(); + UnitTriggerEventType getLastUnitTriggerEventType(); + int getUnitProperty(int unitId, UnitTriggerEventType type); + const string getUnitPropertyName(int unitId, UnitTriggerEventType type); + //callbacks, commands static int networkShowMessageForFaction(LuaHandle* luaHandle); static int networkShowMessageForTeam(LuaHandle* luaHandle); @@ -544,6 +565,13 @@ private: static int registerDayNightEvent(LuaHandle* luaHandle); static int unregisterDayNightEvent(LuaHandle* luaHandle); + static int registerUnitTriggerEvent(LuaHandle* luaHandle); + static int unregisterUnitTriggerEvent(LuaHandle* luaHandle); + static int getLastUnitTriggerEventUnitId(LuaHandle* luaHandle); + static int getLastUnitTriggerEventType(LuaHandle* luaHandle); + static int getUnitProperty(LuaHandle* luaHandle); + static int getUnitPropertyName(LuaHandle* luaHandle); + }; }}//end namespace diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 9f9c6b98..9ad1f65e 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -633,6 +633,15 @@ void Unit::setModelFacing(CardinalDir value) { lastRotation = targetRotation = rotation = value * 90.f; } +void Unit::setCurrField(Field currField) { + Field original_field = this->currField; + + this->currField = currField; + + if(original_field != this->currField) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_FieldChanged); + } +} // ====================================== get ====================================== Vec2i Unit::getCenteredPos() const { @@ -824,10 +833,10 @@ const Level *Unit::getNextLevel() const{ throw megaglest_runtime_error(szBuf); } - if(level==NULL && type->getLevelCount()>0){ + if(level == NULL && type->getLevelCount() > 0) { return type->getLevel(0); } - else{ + else { for(int i=1; igetLevelCount(); ++i){ if(type->getLevel(i-1)==level){ return type->getLevel(i); @@ -1073,7 +1082,12 @@ void Unit::setCurrSkill(const SkillType *currSkill) { this->lastModelIndexForCurrSkillType = -1; this->animationRandomCycleCount = 0; } + const SkillType *original_skill = this->currSkill; this->currSkill= currSkill; + + if(original_skill != this->currSkill) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_SkillChanged); + } } void Unit::setCurrSkill(SkillClass sc) { @@ -1651,7 +1665,11 @@ void Unit::born(const CommandType *ct) { setCurrSkill(scStop); checkItemInVault(&this->hp,this->hp); - hp= type->getMaxHp(); + int original_hp = this->hp; + this->hp= type->getMaxHp(); + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } addItemToVault(&this->hp,this->hp); } @@ -2353,7 +2371,11 @@ bool Unit::applyAttackBoost(const AttackBoost *boost, const Unit *source) { checkItemInVault(&this->hp,this->hp); //hp += boost->boostUpgrade.getMaxHp(); - hp += (totalUpgrade.getMaxHp() - prevMaxHp); + int original_hp = this->hp; + this->hp += (totalUpgrade.getMaxHp() - prevMaxHp); + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } addItemToVault(&this->hp,this->hp); //regenerate hp upgrade / or boost @@ -2361,8 +2383,11 @@ bool Unit::applyAttackBoost(const AttackBoost *boost, const Unit *source) { checkItemInVault(&this->hp,this->hp); //printf("BEFORE Apply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); - - hp += (totalUpgrade.getMaxHpRegeneration() - prevMaxHpRegen); + int original_hp = this->hp; + this->hp += (totalUpgrade.getMaxHpRegeneration() - prevMaxHpRegen); + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } //if(hp > type->getTotalMaxHp(&totalUpgrade)) { // hp = type->getTotalMaxHp(&totalUpgrade); //} @@ -2398,14 +2423,15 @@ bool Unit::applyAttackBoost(const AttackBoost *boost, const Unit *source) { if(wasAlive == true) { //startDamageParticles - if(originalHp > hp) { + if(originalHp > this->hp) { startDamageParticles(); } //stop DamageParticles on death - if(hp <= 0) { - alive= false; - hp=0; + if(this->hp <= 0) { + this->alive= false; + this->hp=0; + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); addItemToVault(&this->hp,this->hp); checkModelStateInfoForNewHpValue(); @@ -2450,8 +2476,12 @@ void Unit::deapplyAttackBoost(const AttackBoost *boost, const Unit *source) { totalUpgrade.deapply(&boost->boostUpgrade, this); checkItemInVault(&this->hp,this->hp); + int original_hp = this->hp; //hp -= boost->boostUpgrade.getMaxHp(); - hp -= (prevMaxHp - totalUpgrade.getMaxHp()); + this->hp -= (prevMaxHp - totalUpgrade.getMaxHp()); + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } addItemToVault(&this->hp,this->hp); //regenerate hp upgrade / or boost @@ -2459,8 +2489,11 @@ void Unit::deapplyAttackBoost(const AttackBoost *boost, const Unit *source) { checkItemInVault(&this->hp,this->hp); //printf("BEFORE DeApply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); - - hp -= (totalUpgrade.getMaxHpRegeneration() - prevMaxHpRegen); + int original_hp = this->hp; + this->hp -= (totalUpgrade.getMaxHpRegeneration() - prevMaxHpRegen); + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } //if(hp > totalUpgrade.getMaxHp()) { // hp = totalUpgrade.getMaxHp(); //} @@ -2477,14 +2510,15 @@ void Unit::deapplyAttackBoost(const AttackBoost *boost, const Unit *source) { //printf("DE-APPLYING ATTACK BOOST wasalive = true to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); //startDamageParticles - if(originalHp > hp) { + if(originalHp > this->hp) { startDamageParticles(); } //stop DamageParticles on death - if(hp <= 0) { - alive= false; - hp=0; + if(this->hp <= 0) { + this->alive= false; + this->hp=0; + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); addItemToVault(&this->hp,this->hp); checkModelStateInfoForNewHpValue(); @@ -2543,9 +2577,13 @@ void Unit::tick() { if( currSkill->getClass() != scBeBuilt){ if(type->getTotalMaxHpRegeneration(&totalUpgrade) >= 0) { checkItemInVault(&this->hp,this->hp); - hp += type->getTotalMaxHpRegeneration(&totalUpgrade); - if(hp > type->getTotalMaxHp(&totalUpgrade)) { - hp = type->getTotalMaxHp(&totalUpgrade); + int original_hp = this->hp; + this->hp += type->getTotalMaxHpRegeneration(&totalUpgrade); + if(this->hp > type->getTotalMaxHp(&totalUpgrade)) { + this->hp = type->getTotalMaxHp(&totalUpgrade); + } + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); } addItemToVault(&this->hp,this->hp); @@ -2575,10 +2613,13 @@ void Unit::tick() { if(type->getHpRegeneration() >= 0) { if( currSkill->getClass() != scBeBuilt){ checkItemInVault(&this->hp,this->hp); - - hp += type->getHpRegeneration(); - if(hp > type->getTotalMaxHp(&totalUpgrade)) { - hp = type->getTotalMaxHp(&totalUpgrade); + int original_hp = this->hp; + this->hp += type->getHpRegeneration(); + if(this->hp > type->getTotalMaxHp(&totalUpgrade)) { + this->hp = type->getTotalMaxHp(&totalUpgrade); + } + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); } addItemToVault(&this->hp,this->hp); @@ -2618,9 +2659,13 @@ void Unit::tick() { //regenerate ep upgrade / or boost checkItemInVault(&this->ep,this->ep); //regenerate ep - ep += type->getTotalMaxEpRegeneration(&totalUpgrade); - if(ep > type->getTotalMaxEp(&totalUpgrade)){ - ep = type->getTotalMaxEp(&totalUpgrade); + int original_ep = this->ep; + this->ep += type->getTotalMaxEpRegeneration(&totalUpgrade); + if(this->ep > type->getTotalMaxEp(&totalUpgrade)){ + this->ep = type->getTotalMaxEp(&totalUpgrade); + } + if(original_ep != this->ep) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_EPChanged); } addItemToVault(&this->ep,this->ep); @@ -2641,13 +2686,17 @@ bool Unit::computeEp() { } //if not enough ep - if(ep - currSkill->getEpCost() < 0) { + if(this->ep - currSkill->getEpCost() < 0) { return true; } checkItemInVault(&this->ep,this->ep); + int original_ep = this->ep; //decrease ep - ep -= currSkill->getEpCost(); + this->ep -= currSkill->getEpCost(); + if(original_ep != this->ep) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_EPChanged); + } addItemToVault(&this->ep,this->ep); if(getType() == NULL) { @@ -2656,8 +2705,12 @@ bool Unit::computeEp() { throw megaglest_runtime_error(szBuf); } - if(ep > getType()->getTotalMaxEp(&totalUpgrade)){ - ep = getType()->getTotalMaxEp(&totalUpgrade); + if(this->ep > getType()->getTotalMaxEp(&totalUpgrade)){ + int original_ep = this->ep; + this->ep = getType()->getTotalMaxEp(&totalUpgrade); + if(original_ep != this->ep) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_EPChanged); + } } addItemToVault(&this->ep,this->ep); @@ -2674,12 +2727,19 @@ bool Unit::repair(){ //increase hp checkItemInVault(&this->hp,this->hp); - hp += getType()->getMaxHp()/type->getProductionTime() + 1; - if(hp > (getType()->getTotalMaxHp(&totalUpgrade))) { - hp = getType()->getTotalMaxHp(&totalUpgrade); + int original_hp = this->hp; + this->hp += getType()->getMaxHp()/type->getProductionTime() + 1; + if(this->hp > (getType()->getTotalMaxHp(&totalUpgrade))) { + this->hp = getType()->getTotalMaxHp(&totalUpgrade); + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } addItemToVault(&this->hp,this->hp); return true; } + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } addItemToVault(&this->hp,this->hp); checkModelStateInfoForNewHpValue(); @@ -2692,12 +2752,16 @@ bool Unit::repair(){ //decrements HP and returns if dead bool Unit::decHp(int i) { - if(hp == 0) { + if(this->hp == 0) { return false; } checkItemInVault(&this->hp,this->hp); - hp -= i; + int original_hp = this->hp; + this->hp -= i; + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } addItemToVault(&this->hp,this->hp); checkModelStateInfoForNewHpValue(); @@ -2712,9 +2776,10 @@ bool Unit::decHp(int i) { startDamageParticles(); //stop DamageParticles on death - if(hp <= 0) { - alive= false; - hp=0; + if(this->hp <= 0) { + this->alive = false; + this->hp = 0; + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); addItemToVault(&this->hp,this->hp); checkModelStateInfoForNewHpValue(); @@ -2852,8 +2917,12 @@ void Unit::applyUpgrade(const UpgradeType *upgradeType){ totalUpgrade.sum(upgradeType, this); checkItemInVault(&this->hp,this->hp); - hp += upgradeType->getMaxHp(); - hp = max(0,hp); + int original_hp = this->hp; + this->hp += upgradeType->getMaxHp(); + this->hp = max(0,this->hp); + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } addItemToVault(&this->hp,this->hp); checkModelStateInfoForNewHpValue(); @@ -2875,13 +2944,19 @@ void Unit::incKills(int team) { void Unit::checkUnitLevel() { const Level *nextLevel= getNextLevel(); - if(nextLevel != NULL && enemyKills >= nextLevel->getKills()) { - level= nextLevel; - int maxHp= totalUpgrade.getMaxHp(); + if(nextLevel != NULL && this->enemyKills >= nextLevel->getKills()) { + this->level= nextLevel; + + int maxHp= this->totalUpgrade.getMaxHp(); totalUpgrade.incLevel(type); + game->getScriptManager()->onUnitTriggerEvent(this,utet_LevelChanged); checkItemInVault(&this->hp,this->hp); - hp += totalUpgrade.getMaxHp() - maxHp; + int original_hp = this->hp; + this->hp += totalUpgrade.getMaxHp() - maxHp; + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } addItemToVault(&this->hp,this->hp); checkModelStateInfoForNewHpValue(); @@ -2948,23 +3023,33 @@ bool Unit::morph(const MorphCommandType *mct) { checkItemInVault(&this->hp,this->hp); - hp += morphUnitType->getMaxHp() - type->getMaxHp(); + int original_hp = this->hp; + this->hp += morphUnitType->getMaxHp() - type->getMaxHp(); + if(original_hp != this->hp) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); + } addItemToVault(&this->hp,this->hp); checkModelStateInfoForNewHpValue(); - preMorph_type = type; - type= morphUnitType; - currField=morphUnitField; + this->preMorph_type = this->type; + this->type= morphUnitType; + Field original_field = this->currField; + this->currField=morphUnitField; computeTotalUpgrade(); - map->putUnitCells(this, pos); - faction->applyDiscount(morphUnitType, mct->getDiscount()); - faction->addStore(type); - faction->applyStaticProduction(morphUnitType,mct); + map->putUnitCells(this, this->pos); + this->faction->applyDiscount(morphUnitType, mct->getDiscount()); + this->faction->addStore(this->type); + this->faction->applyStaticProduction(morphUnitType,mct); - level= NULL; + this->level= NULL; + game->getScriptManager()->onUnitTriggerEvent(this,utet_LevelChanged); checkUnitLevel(); + if(original_field != this->currField) { + game->getScriptManager()->onUnitTriggerEvent(this,utet_FieldChanged); + } + return true; } else{ diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index 088fac8d..b48e577f 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -582,7 +582,7 @@ public: bool isInteresting(InterestingUnitType iut) const; //set - inline void setCurrField(Field currField) {this->currField= currField;} + void setCurrField(Field currField); void setCurrSkill(const SkillType *currSkill); void setCurrSkill(SkillClass sc);