1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
|
/*
* CObjectHandler.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CObjectHandler.h"
#include "../NetPacks.h"
#include "../CGeneralTextHandler.h"
#include "../CHeroHandler.h"
#include "../CSoundBase.h"
#include "../filesystem/ResourceID.h"
#include "../IGameCallback.h"
#include "../CGameState.h"
#include "../StringConstants.h"
#include "../mapping/CMap.h"
#include "CObjectClassesHandler.h"
#include "CGTownInstance.h"
#include "../serializer/JsonSerializeFormat.h"
IGameCallback * IObjectInterface::cb = nullptr;
///helpers
static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1)
{
OpenWindow ow;
ow.window = type;
ow.id1 = id1;
ow.id2 = id2;
IObjectInterface::cb->sendAndApply(&ow);
}
static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
{
InfoWindow iw;
iw.soundID = soundID;
iw.player = playerID;
iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
IObjectInterface::cb->sendAndApply(&iw);
}
/*static void showInfoDialog(const ObjectInstanceID heroID, const ui32 txtID, const ui16 soundID)
{
const PlayerColor playerID = IObjectInterface::cb->getOwner(heroID);
showInfoDialog(playerID,txtID,soundID);
}*/
static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID = 0)
{
const PlayerColor playerID = h->getOwner();
showInfoDialog(playerID,txtID,soundID);
}
///IObjectInterface
void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
{}
void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
{}
void IObjectInterface::newTurn(CRandomGenerator & rand) const
{}
IObjectInterface::~IObjectInterface()
{}
IObjectInterface::IObjectInterface()
{}
void IObjectInterface::initObj(CRandomGenerator & rand)
{}
void IObjectInterface::setProperty( ui8 what, ui32 val )
{}
bool IObjectInterface::wasVisited (PlayerColor player) const
{
return false;
}
bool IObjectInterface::wasVisited (const CGHeroInstance * h) const
{
return false;
}
void IObjectInterface::postInit()
{}
void IObjectInterface::preInit()
{}
void IObjectInterface::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
{}
void IObjectInterface::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
{}
void IObjectInterface::garrisonDialogClosed(const CGHeroInstance *hero) const
{}
void IObjectInterface::heroLevelUpDone(const CGHeroInstance *hero) const
{}
CObjectHandler::CObjectHandler()
{
logGlobal->trace("\t\tReading resources prices ");
const JsonNode config2(ResourceID("config/resources.json"));
for(const JsonNode &price : config2["resources_prices"].Vector())
{
resVals.push_back(price.Float());
}
logGlobal->trace("\t\tDone loading resource prices!");
}
PlayerColor CGObjectInstance::getOwner() const
{
//if (state)
// return state->owner;
//else
return tempOwner; //won't have owner
}
CGObjectInstance::CGObjectInstance():
pos(-1,-1,-1),
ID(Obj::NO_OBJ),
subID(-1),
tempOwner(PlayerColor::UNFLAGGABLE),
blockVisit(false)
{
}
CGObjectInstance::~CGObjectInstance()
{
}
void CGObjectInstance::setOwner(PlayerColor ow)
{
tempOwner = ow;
}
int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
{
return appearance.getWidth();
}
int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
{
return appearance.getHeight();
}
bool CGObjectInstance::visitableAt(int x, int y) const //returns true if object is visitable at location (x, y) form left top tile of image (x, y in tiles)
{
return appearance.isVisitableAt(pos.x - x, pos.y - y);
}
bool CGObjectInstance::blockingAt(int x, int y) const
{
return appearance.isBlockedAt(pos.x - x, pos.y - y);
}
bool CGObjectInstance::coveringAt(int x, int y) const
{
return appearance.isVisibleAt(pos.x - x, pos.y - y);
}
std::set<int3> CGObjectInstance::getBlockedPos() const
{
std::set<int3> ret;
for(int w=0; w<getWidth(); ++w)
{
for(int h=0; h<getHeight(); ++h)
{
if(appearance.isBlockedAt(w, h))
ret.insert(int3(pos.x - w, pos.y - h, pos.z));
}
}
return ret;
}
std::set<int3> CGObjectInstance::getBlockedOffsets() const
{
return appearance.getBlockedOffsets();
}
void CGObjectInstance::setType(si32 ID, si32 subID)
{
const TerrainTile &tile = cb->gameState()->map->getTile(visitablePos());
this->ID = Obj(ID);
this->subID = subID;
//recalculate blockvis tiles - new appearance might have different blockmap than before
cb->gameState()->map->removeBlockVisTiles(this, true);
auto handler = VLC->objtypeh->getHandlerFor(ID, subID);
if(!handler)
{
logGlobal->error("Unknown object type %d:%d at %s", ID, subID, visitablePos().toString());
return;
}
if(!handler->getTemplates(tile.terType).empty())
appearance = handler->getTemplates(tile.terType)[0];
else
appearance = handler->getTemplates()[0]; // get at least some appearance since alternative is crash
cb->gameState()->map->addBlockVisTiles(this);
}
void CGObjectInstance::initObj(CRandomGenerator & rand)
{
switch(ID)
{
case Obj::TAVERN:
blockVisit = true;
break;
}
}
void CGObjectInstance::setProperty( ui8 what, ui32 val )
{
setPropertyDer(what, val); // call this before any actual changes (needed at least for dwellings)
switch(what)
{
case ObjProperty::OWNER:
tempOwner = PlayerColor(val);
break;
case ObjProperty::BLOCKVIS:
blockVisit = val;
break;
case ObjProperty::ID:
ID = Obj(val);
break;
case ObjProperty::SUBID:
subID = val;
break;
}
}
void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
{}
int3 CGObjectInstance::getSightCenter() const
{
return visitablePos();
}
int CGObjectInstance::getSightRadius() const
{
return 3;
}
int3 CGObjectInstance::getVisitableOffset() const
{
return appearance.getVisitableOffset();
}
void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
{
GiveBonus gbonus;
gbonus.bonus.type = Bonus::NONE;
gbonus.id = heroID.getNum();
gbonus.bonus.duration = duration;
gbonus.bonus.source = Bonus::OBJECT;
gbonus.bonus.sid = ID;
cb->giveHeroBonus(&gbonus);
}
std::string CGObjectInstance::getObjectName() const
{
return VLC->objtypeh->getObjectName(ID, subID);
}
boost::optional<std::string> CGObjectInstance::getAmbientSound() const
{
const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).ambient;
if(sounds.size())
return sounds.front(); // TODO: Support randomization of ambient sounds
return boost::none;
}
boost::optional<std::string> CGObjectInstance::getVisitSound() const
{
const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).visit;
if(sounds.size())
return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
return boost::none;
}
boost::optional<std::string> CGObjectInstance::getRemovalSound() const
{
const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).removal;
if(sounds.size())
return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
return boost::none;
}
std::string CGObjectInstance::getHoverText(PlayerColor player) const
{
return getObjectName();
}
std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
{
return getHoverText(hero->tempOwner);
}
void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
{
switch(ID)
{
case Obj::HILL_FORT:
{
openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum());
}
break;
case Obj::SANCTUARY:
{
//You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
showInfoDialog(h, 114);
}
break;
case Obj::TAVERN:
{
openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum());
}
break;
}
}
int3 CGObjectInstance::visitablePos() const
{
return pos - getVisitableOffset();
}
bool CGObjectInstance::isVisitable() const
{
return appearance.isVisitable();
}
bool CGObjectInstance::passableFor(PlayerColor color) const
{
return false;
}
void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
{
//only save here, loading is handled by map loader
if(handler.saving)
{
handler.serializeString("type", typeName);
handler.serializeString("subtype", subTypeName);
handler.serializeInt("x", pos.x);
handler.serializeInt("y", pos.y);
handler.serializeInt("l", pos.z);
JsonNode app;
appearance.writeJson(app, false);
handler.serializeRaw("template",app, boost::none);
}
{
auto options = handler.enterStruct("options");
serializeJsonOptions(handler);
}
}
void CGObjectInstance::afterAddToMap(CMap * map)
{
//nothing here
}
void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler)
{
//nothing here
}
void CGObjectInstance::serializeJsonOwner(JsonSerializeFormat & handler)
{
ui8 temp = tempOwner.getNum();
handler.serializeEnum("owner", temp, PlayerColor::NEUTRAL.getNum(), GameConstants::PLAYER_COLOR_NAMES);
if(!handler.saving)
tempOwner = PlayerColor(temp);
}
CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
{
}
bool CGObjectInstanceBySubIdFinder::operator()(CGObjectInstance * obj) const
{
return this->obj->subID == obj->subID;
}
int3 IBoatGenerator::bestLocation() const
{
std::vector<int3> offsets;
getOutOffsets(offsets);
for (auto & offset : offsets)
{
if(const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offset, false)) //tile is in the map
{
if(tile->terType == ETerrainType::WATER && (!tile->blocked || tile->blockingObjects.front()->ID == Obj::BOAT)) //and is water and is not blocked or is blocked by boat
return o->pos + offset;
}
}
return int3 (-1,-1,-1);
}
IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
{
int3 tile = bestLocation();
const TerrainTile *t = IObjectInterface::cb->getTile(tile);
if(!t)
return TILE_BLOCKED; //no available water
else if(!t->blockingObjects.size())
return GOOD; //OK
else if(t->blockingObjects.front()->ID == Obj::BOAT)
return BOAT_ALREADY_BUILT; //blocked with boat
else
return TILE_BLOCKED; //blocked
}
int IBoatGenerator::getBoatType() const
{
//We make good ships by default
return 1;
}
IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
: o(O)
{
}
void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
{
switch(shipyardStatus())
{
case BOAT_ALREADY_BUILT:
out.addTxt(MetaString::GENERAL_TXT, 51);
break;
case TILE_BLOCKED:
if(visitor)
{
out.addTxt(MetaString::GENERAL_TXT, 134);
out.addReplacement(visitor->name);
}
else
out.addTxt(MetaString::ADVOB_TXT, 189);
break;
case NO_WATER:
logGlobal->error("Shipyard without water! %s \t %d", o->pos.toString(), o->id.getNum());
return;
}
}
void IShipyard::getBoatCost( std::vector<si32> &cost ) const
{
cost.resize(GameConstants::RESOURCE_QUANTITY);
cost[Res::WOOD] = 10;
cost[Res::GOLD] = 1000;
}
IShipyard::IShipyard(const CGObjectInstance *O)
: IBoatGenerator(O)
{
}
IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
{
if(!obj)
return nullptr;
if(obj->ID == Obj::TOWN)
{
return static_cast<CGTownInstance*>(obj);
}
else if(obj->ID == Obj::SHIPYARD)
{
return static_cast<CGShipyard*>(obj);
}
else
{
return nullptr;
}
}
const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
{
return castFrom(const_cast<CGObjectInstance*>(obj));
}
|