diff --git a/source/glest_game/types/faction_type.cpp b/source/glest_game/types/faction_type.cpp index 5f01c7e8..0c9687a2 100644 --- a/source/glest_game/types/faction_type.cpp +++ b/source/glest_game/types/faction_type.cpp @@ -527,7 +527,7 @@ const UnitType *FactionType::getUnitType(const string &name) const{ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n",__FILE__,__FUNCTION__,__LINE__,name.c_str(),i,unitTypes[i].getName().c_str()); } - throw runtime_error("Unit not found: "+name); + throw runtime_error("Unit not found: [" + name + "]"); } const UpgradeType *FactionType::getUpgradeType(const string &name) const{ diff --git a/source/glest_game/types/skill_type.cpp b/source/glest_game/types/skill_type.cpp index c4eafc42..f44cae17 100755 --- a/source/glest_game/types/skill_type.cpp +++ b/source/glest_game/types/skill_type.cpp @@ -49,7 +49,8 @@ void SkillType::load(const XmlNode *sn, const string &dir, const TechTree *tt, c mpCost= sn->getChild("ep-cost")->getAttribute("value")->getIntValue(); if (sn->hasChild("hp-cost")) { hpCost = sn->getChild("hp-cost")->getAttribute("value")->getIntValue(); - } else { + } + else { hpCost = 0; } @@ -166,6 +167,8 @@ AttackSkillType::AttackSkillType() { projectile= false; splash= false; splashRadius= 0; + spawnUnit=""; + spawnUnitcount=0; projectileParticleSystemType= NULL; splashParticleSystemType= NULL; diff --git a/source/glest_game/types/skill_type.h b/source/glest_game/types/skill_type.h index e92f2709..a3c6a919 100755 --- a/source/glest_game/types/skill_type.h +++ b/source/glest_game/types/skill_type.h @@ -171,7 +171,7 @@ public: bool getAttackField(Field field) const {return attackFields[field];} float getAttackStartTime() const {return attackStartTime;} string getSpawnUnit() const {return spawnUnit;} - int getSpawnUnitCount() const {return spawnUnitcount;} + int getSpawnUnitCount() const {return spawnUnitcount;} //get proj bool getProjectile() const {return projectile;} diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index f67fa007..dee6fa5d 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -434,6 +434,12 @@ bool Map::isFreeCells(const Vec2i & pos, int size, Field field) const { bool Map::isFreeCellsOrHasUnit(const Vec2i &pos, int size, Field field, const Unit *unit, const UnitType *munit) const { + if(unit == NULL) { + throw runtime_error("unit == NULL"); + } + if(munit == NULL) { + throw runtime_error("munit == NULL"); + } for (int i = 1; i <= munit->getSize(); ++i) { for (int j = 1; j <= munit->getSize(); ++j) { if (munit->hasCellMap() == true) { @@ -443,10 +449,6 @@ bool Map::isFreeCellsOrHasUnit(const Vec2i &pos, int size, Field field, Vec2i(pos.x + i - 1, pos.y + j - 1), field, unit) == false) { return false; } - else { - } - } - else { } } else { diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index d1e465f2..f2ca14af 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -148,52 +148,60 @@ void UnitUpdater::updateUnit(Unit *unit) { unit->setCurrSkill(scStop); unit->cancelCommand(); } - if(unit->getCurrSkill()->getClass() != scAttack){ -unit->computeHp(); + if(unit->getCurrSkill() != NULL && unit->getCurrSkill()->getClass() != scAttack) { + unit->computeHp(); } - else{ - Command *command= unit->getCurrCommand(); - const AttackCommandType *act= static_cast(command->getCommandType()); - if(act->getAttackSkillType()->getSpawnUnit() != ""){ - for (int y=0; y < act->getAttackSkillType()->getSpawnUnitCount(); ++y) { - Unit *spawned; - UnitPathInterface *newpath = NULL; - switch(this->game->getGameSettings()->getPathFinderType()) { - case pfBasic: - newpath = new UnitPathBasic(); - break; - case pfRoutePlanner: - newpath = new UnitPath(); - break; - default: - throw runtime_error("detected unsupported pathfinder type!"); - } - const FactionType *ft= unit->getFaction()->getType(); - spawned= new Unit(world->getNextUnitId(unit->getFaction()), newpath, Vec2i(0),ft->getUnitType(act->getAttackSkillType()->getSpawnUnit()), unit->getFaction(), world->getMap(),CardinalDir::NORTH); - SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] about to place unit for unit [%s]\n",__FILE__,__FUNCTION__,__LINE__,spawned->toString().c_str()); - if(!world->placeUnit(unit->getCenteredPos(), 10, spawned)) { - SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] COULD NOT PLACE UNIT for unitID [%d]\n",__FILE__,__FUNCTION__,__LINE__,spawned->getId()); + else if(unit->getCommandSize() > 0) { + Command *command= unit->getCurrCommand(); + if(command != NULL) { + const AttackCommandType *act= static_cast(command->getCommandType()); + if( act != NULL && act->getAttackSkillType() != NULL && + act->getAttackSkillType()->getSpawnUnit() != "" && act->getAttackSkillType()->getSpawnUnitCount() > 0) { - delete spawned; - } - else{ - spawned->create(); - spawned->born(); - world->getStats()->produce(unit->getFactionIndex()); - const CommandType *ct= spawned->computeCommandType(command->getPos(),command->getUnit()); - if(ct!=NULL){ - SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - spawned->giveCommand(new Command(ct, unit->getMeetingPos())); + const FactionType *ft= unit->getFaction()->getType(); + const UnitType *spawnUnitType = ft->getUnitType(act->getAttackSkillType()->getSpawnUnit()); + int spawnCount = act->getAttackSkillType()->getSpawnUnitCount(); + for (int y=0; y < spawnCount; ++y) { + UnitPathInterface *newpath = NULL; + switch(this->game->getGameSettings()->getPathFinderType()) { + case pfBasic: + newpath = new UnitPathBasic(); + break; + case pfRoutePlanner: + newpath = new UnitPath(); + break; + default: + throw runtime_error("detected unsupported pathfinder type!"); + } + + Unit *spawned= new Unit(world->getNextUnitId(unit->getFaction()), newpath, + Vec2i(0), spawnUnitType, unit->getFaction(), + world->getMap(), CardinalDir::NORTH); + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] about to place unit for unit [%s]\n",__FILE__,__FUNCTION__,__LINE__,spawned->toString().c_str()); + if(!world->placeUnit(unit->getCenteredPos(), 10, spawned)) { + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] COULD NOT PLACE UNIT for unitID [%d]\n",__FILE__,__FUNCTION__,__LINE__,spawned->getId()); + + // This will also cleanup newPath + delete spawned; + spawned = NULL; + } + else { + spawned->create(); + spawned->born(); + world->getStats()->produce(unit->getFactionIndex()); + const CommandType *ct= spawned->computeCommandType(command->getPos(),command->getUnit()); + if(ct != NULL){ + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + spawned->giveCommand(new Command(ct, unit->getMeetingPos())); + } + scriptManager->onUnitCreated(spawned); + + //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + } + } } - scriptManager->onUnitCreated(spawned); - - //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); } } - - - } - } //move unit in cells if(unit->getCurrSkill()->getClass() == scMove) { @@ -1641,6 +1649,13 @@ void UnitUpdater::hit(Unit *attacker, const AttackSkillType* ast, const Vec2i &t } void UnitUpdater::damage(Unit *attacker, const AttackSkillType* ast, Unit *attacked, float distance){ + if(attacker == NULL) { + throw runtime_error("attacker == NULL"); + } + if(ast == NULL) { + throw runtime_error("ast == NULL"); + } + //get vars float damage= ast->getTotalAttackStrength(attacker->getTotalUpgrade()); int var= ast->getAttackVar();