bugfix for a number of legacy divide by 0 conditions discovered by Tiger.

This commit is contained in:
Mark Vejvoda 2013-05-28 06:07:35 +00:00
parent 43d533aec3
commit 7acd500c08
4 changed files with 119 additions and 19 deletions

View File

@ -1819,6 +1819,9 @@ UnitTriggerEventType ScriptManager::getLastUnitTriggerEventType() {
int ScriptManager::getUnitProperty(int unitId, UnitTriggerEventType type) {
int result = -1;
//printf("File: %s line: %d type: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,type);
Unit *unit= world->findUnitById(unitId);
if(unit != NULL) {
switch(type) {
@ -1851,6 +1854,7 @@ int ScriptManager::getUnitProperty(int unitId, UnitTriggerEventType type) {
break;
}
}
//printf("File: %s line: %d result: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,result);
return result;
}
const string ScriptManager::getUnitPropertyName(int unitId, UnitTriggerEventType type) {
@ -1891,16 +1895,32 @@ const string ScriptManager::getUnitPropertyName(int unitId, UnitTriggerEventType
}
void ScriptManager::onUnitTriggerEvent(const Unit *unit, UnitTriggerEventType event) {
//static bool inEvent = false;
//if(inEvent == true) {
// printf("\n\n!!!!!!!!!!!!!!! File: %s line: %d unit [%d - %s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,unit->getId(),unit->getType()->getName().c_str());
// return;
//}
//inEvent = true;
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
if(UnitTriggerEventList.empty() == false) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
std::map<int,UnitTriggerEventType>::iterator iterFind = UnitTriggerEventList.find(unit->getId());
if(iterFind != UnitTriggerEventList.end()) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
lastUnitTriggerEventUnitId = unit->getId();
lastUnitTriggerEventType = event;
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
luaScript.beginCall("unitTriggerEvent");
luaScript.endCall();
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
}
//inEvent = false;
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
void ScriptManager::registerDayNightEvent() {
@ -1952,6 +1972,8 @@ int ScriptManager::getIsNightTime() {
return tf->isNight();
}
float ScriptManager::getTimeOfDay() {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
ScriptManager_STREFLOP_Wrapper streflopWrapper;
@ -1959,6 +1981,7 @@ float ScriptManager::getTimeOfDay() {
if(tf == NULL) {
throw megaglest_runtime_error("tf == NULL");
}
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
return tf->getTime();
}
@ -3044,7 +3067,7 @@ int ScriptManager::getLastUnitTriggerEventUnitId(LuaHandle* luaHandle) {
}
int ScriptManager::getLastUnitTriggerEventType(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);
luaArguments.returnInt(thisScriptManager->getLastUnitTriggerEventType());
luaArguments.returnInt(static_cast<int>(thisScriptManager->getLastUnitTriggerEventType()));
return luaArguments.getReturnCount();
}

View File

@ -479,7 +479,7 @@ Unit::Unit(int id, UnitPathInterface *unitpath, const Vec2i &pos,
loadCount= 0;
ep= 0;
deadCount= 0;
hp= type->getMaxHp()/20;
hp= type->getMaxHp() / 20;
toBeUndertaken= false;
highlight= 0.f;
@ -639,7 +639,9 @@ void Unit::setCurrField(Field currField) {
this->currField = currField;
if(original_field != this->currField) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_FieldChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
}
// ====================================== get ======================================
@ -783,8 +785,11 @@ float Unit::getRotationX() const{
int Unit::getProductionPercent() const{
if(anyCommand()){
const ProducibleType *produced= commands.front()->getCommandType()->getProduced();
if(produced!=NULL){
return clamp(progress2*100/produced->getProductionTime(), 0, 100);
if(produced != NULL) {
if(produced->getProductionTime() == 0) {
return 0;
}
return clamp(progress2 * 100 / produced->getProductionTime(), 0, 100);
}
}
return -1;
@ -793,9 +798,13 @@ int Unit::getProductionPercent() const{
float Unit::getProgressRatio() const{
if(anyCommand()){
const ProducibleType *produced= commands.front()->getCommandType()->getProduced();
if(produced!=NULL){
float help=progress2;
return clamp(help/produced->getProductionTime(), 0.f, 1.f);
if(produced != NULL){
if(produced->getProductionTime() == 0) {
return 0.f;
}
float help = progress2;
return clamp(help / produced->getProductionTime(), 0.f, 1.f);
}
}
return -1;
@ -808,7 +817,11 @@ float Unit::getHpRatio() const {
throw megaglest_runtime_error(szBuf);
}
return clamp(static_cast<float>(hp)/type->getTotalMaxHp(&totalUpgrade), 0.f, 1.f);
float maxHpAllowed = type->getTotalMaxHp(&totalUpgrade);
if(maxHpAllowed == 0.f) {
return 0.f;
}
return clamp(static_cast<float>(hp) / maxHpAllowed, 0.f, 1.f);
}
float Unit::getEpRatio() const {
@ -818,11 +831,15 @@ float Unit::getEpRatio() const {
throw megaglest_runtime_error(szBuf);
}
if(type->getMaxHp()==0){
if(type->getMaxHp() == 0) {
return 0.f;
}
else{
return clamp(static_cast<float>(ep)/type->getTotalMaxEp(&totalUpgrade), 0.f, 1.f);
else {
float maxEpAllowed = type->getTotalMaxEp(&totalUpgrade);
if(maxEpAllowed == 0.f) {
return 0.f;
}
return clamp(static_cast<float>(ep) / maxEpAllowed, 0.f, 1.f);
}
}
@ -1086,7 +1103,9 @@ void Unit::setCurrSkill(const SkillType *currSkill) {
this->currSkill= currSkill;
if(original_skill != this->currSkill) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_SkillChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
}
@ -1668,7 +1687,9 @@ void Unit::born(const CommandType *ct) {
int original_hp = this->hp;
this->hp= type->getMaxHp();
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
}
@ -2374,7 +2395,9 @@ bool Unit::applyAttackBoost(const AttackBoost *boost, const Unit *source) {
int original_hp = this->hp;
this->hp += (totalUpgrade.getMaxHp() - prevMaxHp);
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
@ -2386,7 +2409,9 @@ bool Unit::applyAttackBoost(const AttackBoost *boost, const Unit *source) {
int original_hp = this->hp;
this->hp += (totalUpgrade.getMaxHpRegeneration() - prevMaxHpRegen);
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
//if(hp > type->getTotalMaxHp(&totalUpgrade)) {
// hp = type->getTotalMaxHp(&totalUpgrade);
@ -2431,7 +2456,9 @@ bool Unit::applyAttackBoost(const AttackBoost *boost, const Unit *source) {
if(this->hp <= 0) {
this->alive= false;
this->hp=0;
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
addItemToVault(&this->hp,this->hp);
checkModelStateInfoForNewHpValue();
@ -2480,7 +2507,9 @@ void Unit::deapplyAttackBoost(const AttackBoost *boost, const Unit *source) {
//hp -= boost->boostUpgrade.getMaxHp();
this->hp -= (prevMaxHp - totalUpgrade.getMaxHp());
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
@ -2492,7 +2521,9 @@ void Unit::deapplyAttackBoost(const AttackBoost *boost, const Unit *source) {
int original_hp = this->hp;
this->hp -= (totalUpgrade.getMaxHpRegeneration() - prevMaxHpRegen);
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
//if(hp > totalUpgrade.getMaxHp()) {
// hp = totalUpgrade.getMaxHp();
@ -2518,7 +2549,9 @@ void Unit::deapplyAttackBoost(const AttackBoost *boost, const Unit *source) {
if(this->hp <= 0) {
this->alive= false;
this->hp=0;
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
addItemToVault(&this->hp,this->hp);
checkModelStateInfoForNewHpValue();
@ -2583,7 +2616,9 @@ void Unit::tick() {
this->hp = type->getTotalMaxHp(&totalUpgrade);
}
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
@ -2619,7 +2654,9 @@ void Unit::tick() {
this->hp = type->getTotalMaxHp(&totalUpgrade);
}
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
@ -2665,7 +2702,9 @@ void Unit::tick() {
this->ep = type->getTotalMaxEp(&totalUpgrade);
}
if(original_ep != this->ep) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_EPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->ep,this->ep);
@ -2695,7 +2734,9 @@ bool Unit::computeEp() {
//decrease ep
this->ep -= currSkill->getEpCost();
if(original_ep != this->ep) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_EPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->ep,this->ep);
@ -2709,7 +2750,9 @@ bool Unit::computeEp() {
int original_ep = this->ep;
this->ep = getType()->getTotalMaxEp(&totalUpgrade);
if(original_ep != this->ep) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_EPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
}
addItemToVault(&this->ep,this->ep);
@ -2728,17 +2771,24 @@ bool Unit::repair(){
//increase hp
checkItemInVault(&this->hp,this->hp);
int original_hp = this->hp;
this->hp += getType()->getMaxHp()/type->getProductionTime() + 1;
if(type->getProductionTime() + 1 == 0) {
throw megaglest_runtime_error("Detected divide by 0 condition: type->getProductionTime() + 1 == 0");
}
this->hp += getType()->getMaxHp() / type->getProductionTime() + 1;
if(this->hp > (getType()->getTotalMaxHp(&totalUpgrade))) {
this->hp = getType()->getTotalMaxHp(&totalUpgrade);
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
return true;
}
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
@ -2760,7 +2810,9 @@ bool Unit::decHp(int i) {
int original_hp = this->hp;
this->hp -= i;
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
@ -2779,7 +2831,9 @@ bool Unit::decHp(int i) {
if(this->hp <= 0) {
this->alive = false;
this->hp = 0;
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
addItemToVault(&this->hp,this->hp);
checkModelStateInfoForNewHpValue();
@ -2921,7 +2975,9 @@ void Unit::applyUpgrade(const UpgradeType *upgradeType){
this->hp += upgradeType->getMaxHp();
this->hp = max(0,this->hp);
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
@ -2949,13 +3005,17 @@ void Unit::checkUnitLevel() {
int maxHp= this->totalUpgrade.getMaxHp();
totalUpgrade.incLevel(type);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_LevelChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
checkItemInVault(&this->hp,this->hp);
int original_hp = this->hp;
this->hp += totalUpgrade.getMaxHp() - maxHp;
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
@ -3026,7 +3086,9 @@ bool Unit::morph(const MorphCommandType *mct) {
int original_hp = this->hp;
this->hp += morphUnitType->getMaxHp() - type->getMaxHp();
if(original_hp != this->hp) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
addItemToVault(&this->hp,this->hp);
@ -3043,11 +3105,15 @@ bool Unit::morph(const MorphCommandType *mct) {
this->faction->applyStaticProduction(morphUnitType,mct);
this->level= NULL;
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_LevelChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
checkUnitLevel();
if(original_field != this->currField) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
game->getScriptManager()->onUnitTriggerEvent(this,utet_FieldChanged);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
return true;

View File

@ -2088,7 +2088,7 @@ void UnitUpdater::updateProduce(Unit *unit, int frameIndex) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
if(unit->getProgress2()>pct->getProduced()->getProductionTime()){
if(unit->getProgress2() > pct->getProduced()->getProductionTime()){
unit->finishCommand();
unit->setCurrSkill(scStop);
@ -2153,14 +2153,14 @@ void UnitUpdater::updateUpgrade(Unit *unit, int frameIndex) {
Command *command= unit->getCurrCommand();
const UpgradeCommandType *uct= static_cast<const UpgradeCommandType*>(command->getCommandType());
if(unit->getCurrSkill()->getClass()!=scUpgrade){
if(unit->getCurrSkill()->getClass() != scUpgrade) {
//if not producing
unit->setCurrSkill(uct->getUpgradeSkillType());
}
else{
else {
//if producing
unit->update2();
if(unit->getProgress2()>uct->getProduced()->getProductionTime()){
if(unit->getProgress2() > uct->getProduced()->getProductionTime()){
unit->finishCommand();
unit->setCurrSkill(scStop);
unit->getFaction()->finishUpgrade(uct->getProducedUpgrade());
@ -2206,7 +2206,7 @@ void UnitUpdater::updateMorph(Unit *unit, int frameIndex) {
}
else{
unit->update2();
if(unit->getProgress2()>mct->getProduced()->getProductionTime()){
if(unit->getProgress2() > mct->getProduced()->getProductionTime()){
//int oldSize = 0;
//bool needMapUpdate = false;

View File

@ -1428,19 +1428,29 @@ void World::giveAttackCommand(int unitId, int unitToAttackId) {
}
void World::giveProductionCommand(int unitId, const string &producedName) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
Unit *unit= findUnitById(unitId);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
if(unit != NULL) {
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
const UnitType *ut= unit->getType();
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
//Search for a command that can produce the unit
for(int i= 0; i< ut->getCommandTypeCount(); ++i) {
const CommandType* ct= ut->getCommandType(i);
if(ct != NULL && ct->getClass() == ccProduce) {
const ProduceCommandType *pct= static_cast<const ProduceCommandType*>(ct);
if(pct != NULL && pct->getProducedUnit()->getName() == producedName) {
const ProduceCommandType *pct= dynamic_cast<const ProduceCommandType*>(ct);
if(pct != NULL && pct->getProducedUnit() != NULL &&
pct->getProducedUnit()->getName() == producedName) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
unit->giveCommand(new Command(pct));
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
break;
@ -1451,6 +1461,7 @@ void World::giveProductionCommand(int unitId, const string &producedName) {
else {
throw megaglest_runtime_error("Invalid unitId index in giveProductionCommand: " + intToStr(unitId) + " producedName = " + producedName);
}
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
}
void World::giveAttackStoppedCommand(int unitId, const string &itemName, bool ignoreRequirements) {