1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

* removing objects (resources, chests, dismissing heroes)

This commit is contained in:
Michał W. Urbańczyk 2008-08-13 04:41:11 +00:00
parent b89c951d09
commit 59b808f4e1
13 changed files with 113 additions and 106 deletions

View File

@ -329,14 +329,26 @@ void CGameState::applyNL(IPack * pack)
}
case 500:
{
RemoveHero *rh = static_cast<RemoveHero*>(pack);
CGHeroInstance *h = static_cast<CGHeroInstance*>(map->objects[rh->id]);
RemoveObject *rh = static_cast<RemoveObject*>(pack);
CGObjectInstance *obj = map->objects[rh->id];
if(obj->ID==34)
{
CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
std::vector<CGHeroInstance*>::iterator nitr = std::find(map->heroes.begin(), map->heroes.end(),h);
map->heroes.erase(nitr);
int player = h->tempOwner;
nitr = std::find(players[player].heroes.begin(), players[player].heroes.end(), h);
players[player].heroes.erase(nitr);
map->objects[h->id] = NULL;
}
map->objects[rh->id] = NULL;
//unblock tiles
if(obj->defInfo)
{
map->removeBlockVisTiles(obj);
}
break;
}
case 501://hero try-move
@ -344,10 +356,12 @@ void CGameState::applyNL(IPack * pack)
TryMoveHero * n = static_cast<TryMoveHero*>(pack);
CGHeroInstance *h = static_cast<CGHeroInstance*>(map->objects[n->id]);
h->movement = n->movePoints;
if(n->result)
if(n->start!=n->end && n->result)
{
map->removeBlockVisTiles(h);
h->pos = n->end;
else
h->pos = n->start;
map->addBlockVisTiles(h);
}
BOOST_FOREACH(int3 t, n->fowRevealed)
players[h->getOwner()].fogOfWarMap[t.x][t.y][t.z] = 1;
break;

View File

@ -602,7 +602,7 @@ void CPickable::onHeroVisit(int objid, int heroID)
break;
}
}
//VLC->mh->removeObject(os);
cb->removeObject(objid);
}
void CPickable::chosen(ui32 which, int heroid, int val)
{

View File

@ -1579,7 +1579,6 @@ void CPlayerInterface::heroMoved(const HeroMoveDetails & details)
//ho->moveDir = 0; //move ended
ho->isStanding = true;
//move finished
CGI->mh->recalculateHideVisPosUnderObj(details.ho, true);
adventureInt->minimap.draw();
adventureInt->heroList.updateMove(ho);
}

View File

@ -241,13 +241,17 @@ void CClient::process(int what)
}
case 500:
{
RemoveHero rh;
RemoveObject rh;
*serv >> rh;
CGObjectInstance *obj = gs->map->objects[rh.id];
if(obj->ID == 34)
{
CGHeroInstance *h = static_cast<CGHeroInstance*>(gs->map->objects[rh.id]);
std::cout << "Removing hero with id = "<<(unsigned)rh.id<<std::endl;
CGI->mh->removeObject(h);
gs->apply(&rh);
playerint[h->tempOwner]->heroKilled(h);
}
CGI->mh->removeObject(obj);
gs->apply(&rh);
break;
}
case 501: //hero movement response - we have to notify interfaces and callback

View File

@ -107,7 +107,20 @@ namespace vstd
{
return std::find(c.begin(),c.end(),i);
}
template <typename Container, typename Item>
typename Container::iterator find(Container & c, const Item &i)
{
return std::find(c.begin(),c.end(),i);
}
template <typename Container, typename Item>
bool operator-=(Container &c, const Item &i)
{
Container::iterator itr = find(c,i);
if(itr == c.end())
return false;
c.erase(itr);
return true;
}
}
using vstd::operator-=;
#endif //GLOBAL_H

View File

@ -77,10 +77,10 @@ struct SetSecSkill : public CPack<SetSecSkill> //106
h & abs & id & which & val;
}
};
struct RemoveHero : public CPack<RemoveHero> //500
struct RemoveObject : public CPack<RemoveObject> //500
{
RemoveHero(){type = 500;};
RemoveHero(si32 ID){id = ID;type = 500;};
RemoveObject(){type = 500;};
RemoveObject(si32 ID){id = ID;type = 500;};
si32 id;
template <typename Handler> void serialize(Handler &h, const int version)

49
map.cpp
View File

@ -2300,33 +2300,62 @@ borderguardend:
{
if(!objects[f]->defInfo)
continue;
CDefHandler * curd = objects[f]->defInfo->handler;
addBlockVisTiles(objects[f]);
}
}
void Mapa::removeBlockVisTiles(CGObjectInstance * obj)
{
for(int fx=0; fx<8; ++fx)
{
for(int fy=0; fy<6; ++fy)
{
int xVal = objects[f]->pos.x + fx - 7;
int yVal = objects[f]->pos.y + fy - 5;
int zVal = objects[f]->pos.z;
int xVal = obj->pos.x + fx - 7;
int yVal = obj->pos.y + fy - 5;
int zVal = obj->pos.z;
if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
{
TerrainTile & curt = terrain[xVal][yVal][zVal];
if(((objects[f]->defInfo->visitMap[fy] >> (7 - fx)) & 1))
if(((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
{
curt.visitableObjects.push_back(objects[f]);
curt.visitableObjects -= obj;
curt.visitable = curt.visitableObjects.size();
}
if(!((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
{
curt.blockingObjects -= obj;
curt.blocked = curt.visitableObjects.size();
}
}
}
}
}
void Mapa::addBlockVisTiles(CGObjectInstance * obj)
{
for(int fx=0; fx<8; ++fx)
{
for(int fy=0; fy<6; ++fy)
{
int xVal = obj->pos.x + fx - 7;
int yVal = obj->pos.y + fy - 5;
int zVal = obj->pos.z;
if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
{
TerrainTile & curt = terrain[xVal][yVal][zVal];
if(((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
{
curt.visitableObjects.push_back(obj);
curt.visitable = true;
}
if(!((objects[f]->defInfo->blockMap[fy] >> (7 - fx)) & 1))
if(!((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
{
curt.blockingObjects.push_back(objects[f]);
curt.blockingObjects.push_back(obj);
curt.blocked = true;
}
}
}
}
}
}
Mapa::Mapa(std::string filename)
{
std::cout<<"Opening map file: "<<filename<<"\t "<<std::flush;

2
map.h
View File

@ -485,6 +485,8 @@ struct DLL_EXPORT Mapa
std::vector<CGTownInstance*> towns;
void initFromBytes(unsigned char * bufor); //creates map from decompressed .h3m data
void addBlockVisTiles(CGObjectInstance * obj);
void removeBlockVisTiles(CGObjectInstance * obj);
Mapa(std::string filename); //creates map structure from .h3m file
CGHeroInstance * getHero(int ID, int mode=0);
};

View File

@ -1161,66 +1161,6 @@ bool CMapHandler::hideObject(CGObjectInstance *obj)
bool CMapHandler::removeObject(CGObjectInstance *obj)
{
hideObject(obj);
recalculateHideVisPosUnderObj(obj);
return true;
}
bool CMapHandler::recalculateHideVisPos(int3 &pos)
{
//ttiles[pos.x][pos.y][pos.z].tileInfo->visitable = false;
//ttiles[pos.x][pos.y][pos.z].tileInfo->blocked = false;
//for(int i=0; i<ttiles[pos.x][pos.y][pos.z].objects.size(); ++i)
//{
// CDefHandler * curd = ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->handler;
// for(int fx=0; fx<8; ++fx)
// {
// for(int fy=0; fy<6; ++fy)
// {
// int xVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.x + fx - 7;
// int yVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.y + fy - 5;
// int zVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.z;
// if(xVal>=0 && xVal<ttiles.size()-Woff && yVal>=0 && yVal<ttiles[0].size()-Hoff)
// {
// TerrainTile2 & curt = ttiles[xVal][yVal][zVal];
// if(((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->visitMap[fy] >> (7 - fx)) & 1))
// curt.tileInfo->visitable = true;
// if(!((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->blockMap[fy] >> (7 - fx)) & 1))
// curt.tileInfo->blocked = true;
// }
// }
// }
//}
return true;
}
bool CMapHandler::recalculateHideVisPosUnderObj(CGObjectInstance *obj, bool withBorder)
{
//if(withBorder)
//{
// for(int fx=-1; fx<=obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
// {
// for(int fy=-1; fy<=obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
// {
// if((obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32+1)>=0 && (obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32+1)<ttiles.size()-Woff && (obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32+1)>=0 && (obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32+1)<ttiles[0].size()-Hoff)
// {
// recalculateHideVisPos(int3(obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32 +1, obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32 + 1, obj->pos.z));
// }
// }
// }
//}
//else
//{
// for(int fx=0; fx<obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
// {
// for(int fy=0; fy<obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
// {
// if((obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32+1)>=0 && (obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32+1)<ttiles.size()-Woff && (obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32+1)>=0 && (obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32+1)<ttiles[0].size()-Hoff)
// {
// recalculateHideVisPos(int3(obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32 +1, obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32 + 1, obj->pos.z));
// }
// }
// }
//}
return true;
}

View File

@ -89,8 +89,6 @@ public:
bool printObject(CGObjectInstance * obj); //puts appropriate things to ttiles, so obj will be visible on map
bool hideObject(CGObjectInstance * obj); //removes appropriate things from ttiles, so obj will be no longer visible on map (but still will exist)
bool removeObject(CGObjectInstance * obj); //removes object from each place in VCMI (I hope)
bool recalculateHideVisPos(int3& pos); //recalculates position for hidden / visitable positions
bool recalculateHideVisPosUnderObj(CGObjectInstance * obj, bool withBorder = false); //recalculates position for hidden / visitable positions under given object
void init();
void calculateBlockedPos();
void initObjectRects();

View File

@ -490,7 +490,7 @@ void CGameHandler::handleConnection(std::set<int> players, CConnection &c)
{
si32 id;
c >> id;
RemoveHero rh(id);
RemoveObject rh(id);
sendAndApply(&rh);
break;
}

View File

@ -26,6 +26,13 @@ void CScriptCallback::setBlockVis(int objid, bool bv)
SetObjectProperty sop(objid,2,bv);
gh->sendAndApply(&sop);
}
void CScriptCallback::removeObject(int objid)
{
RemoveObject ro;
ro.id = objid;
gh->sendAndApply(&ro);
}
void CScriptCallback::setOwner(int objid, ui8 owner)
{
SetObjectProperty sop(objid,1,owner);

View File

@ -35,6 +35,7 @@ public:
const CGTownInstance* getTown(int objid);
//do sth
void removeObject(int objid);
void setBlockVis(int objid, bool bv);
void setOwner(int objid, ui8 owner);
void setHoverName(int objid, MetaString * name);