- fixed null pointer discovered by titi

This commit is contained in:
Mark Vejvoda 2013-01-24 23:10:25 +00:00
parent 8399ed2c6a
commit 743efc0822

View File

@ -1089,94 +1089,100 @@ bool Gui::computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&t
else if(obj != NULL){ else if(obj != NULL){
targetUnit= NULL; targetUnit= NULL;
highlightedResourceObjectPos=Map::toSurfCoords(obj->getMapPos()); highlightedResourceObjectPos=Map::toSurfCoords(obj->getMapPos());
getHighlightedResourceObject()->resetHighlight();
// get real click pos
renderer.computePosition(screenPos, targetPos);
//validPosObjWorld= true; Object *selObj = getHighlightedResourceObject();
//posObjWorld = targetPos; if(selObj != NULL) {
selObj->resetHighlight();
// get real click pos
renderer.computePosition(screenPos, targetPos);
int tx= targetPos.x; //validPosObjWorld= true;
int ty= targetPos.y; //posObjWorld = targetPos;
int ox= obj->getMapPos().x; int tx= targetPos.x;
int oy= obj->getMapPos().y; int ty= targetPos.y;
Resource* clickedRecource= world->getMap()->getSurfaceCell(Map::toSurfCoords(obj->getMapPos()))->getResource(); int ox= obj->getMapPos().x;
int oy= obj->getMapPos().y;
// lets see if the click had the same Resource Resource* clickedRecource= world->getMap()->getSurfaceCell(Map::toSurfCoords(obj->getMapPos()))->getResource();
if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(targetPos))->getResource()){
// same ressource is meant, so use the user selected position // lets see if the click had the same Resource
if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(targetPos))->getResource()){
// same ressource is meant, so use the user selected position
return true;
}
else{// calculate a valid resource position which is as near as possible to the selected position
Vec2i testIt= Vec2i(obj->getMapPos());
///////////////
// test both //
///////////////
if(ty < oy){
testIt.y--;
}
else if(ty > oy){
testIt.y++;
}
if(tx < ox){
testIt.x--;
}
else if(tx > ox){
testIt.x++;
}
if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){
// same ressource is meant, so use this position
targetPos= testIt;
//posObjWorld= targetPos;
return true;
}
else{
testIt= Vec2i(obj->getMapPos());
}
/////////////////
// test y-only //
/////////////////
if(ty < oy){
testIt.y--;
}
else if(ty > oy){
testIt.y++;
}
if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){
// same ressource is meant, so use this position
targetPos= testIt;
//posObjWorld= targetPos;
return true;
}
else{
testIt= Vec2i(obj->getMapPos());
}
/////////////////
// test x-only //
/////////////////
if(tx < ox){
testIt.x--;
}
else if(tx > ox){
testIt.x++;
}
if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){
// same ressource is meant, so use this position
targetPos= testIt;
//posObjWorld= targetPos;
return true;
}
}
// give up and use the object position;
targetPos= obj->getMapPos();
posObjWorld= targetPos;
return true; return true;
} }
else{// calculate a valid resource position which is as near as possible to the selected position else {
Vec2i testIt= Vec2i(obj->getMapPos()); return false;
///////////////
// test both //
///////////////
if(ty < oy){
testIt.y--;
}
else if(ty > oy){
testIt.y++;
}
if(tx < ox){
testIt.x--;
}
else if(tx > ox){
testIt.x++;
}
if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){
// same ressource is meant, so use this position
targetPos= testIt;
//posObjWorld= targetPos;
return true;
}
else{
testIt= Vec2i(obj->getMapPos());
}
/////////////////
// test y-only //
/////////////////
if(ty < oy){
testIt.y--;
}
else if(ty > oy){
testIt.y++;
}
if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){
// same ressource is meant, so use this position
targetPos= testIt;
//posObjWorld= targetPos;
return true;
}
else{
testIt= Vec2i(obj->getMapPos());
}
/////////////////
// test x-only //
/////////////////
if(tx < ox){
testIt.x--;
}
else if(tx > ox){
testIt.x++;
}
if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){
// same ressource is meant, so use this position
targetPos= testIt;
//posObjWorld= targetPos;
return true;
}
} }
// give up and use the object position;
targetPos= obj->getMapPos();
posObjWorld= targetPos;
return true;
} }
else{ else{
targetUnit= NULL; targetUnit= NULL;
@ -1184,10 +1190,13 @@ bool Gui::computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&t
validPosObjWorld= true; validPosObjWorld= true;
posObjWorld= targetPos; posObjWorld= targetPos;
if(world->getMap()->getSurfaceCell(Map::toSurfCoords(targetPos))->getResource()!=NULL) if(world->getMap()->getSurfaceCell(Map::toSurfCoords(targetPos))->getResource() != NULL) {
{
highlightedResourceObjectPos=Map::toSurfCoords(targetPos); highlightedResourceObjectPos=Map::toSurfCoords(targetPos);
getHighlightedResourceObject()->resetHighlight();
Object *selObj = getHighlightedResourceObject();
if(selObj != NULL) {
selObj->resetHighlight();
}
} }
return true; return true;