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 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
/*
* CObjectClassesHandler.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 "CObjectClassesHandler.h"
#include "../filesystem/Filesystem.h"
#include "../filesystem/CBinaryReader.h"
#include "../VCMI_Lib.h"
#include "../GameConstants.h"
#include "../StringConstants.h"
#include "../CGeneralTextHandler.h"
#include "../CModHandler.h"
#include "../JsonNode.h"
#include "../CSoundBase.h"
#include "CRewardableConstructor.h"
#include "CommonConstructors.h"
#include "MapObjects.h"
VCMI_LIB_NAMESPACE_BEGIN
CObjectClassesHandler::CObjectClassesHandler()
{
#define SET_HANDLER_CLASS(STRING, CLASSNAME) handlerConstructors[STRING] = std::make_shared<CLASSNAME>;
#define SET_HANDLER(STRING, TYPENAME) handlerConstructors[STRING] = std::make_shared<CDefaultObjectTypeHandler<TYPENAME>>
// list of all known handlers, hardcoded for now since the only way to add new objects is via C++ code
//Note: should be in sync with registerTypesMapObjectTypes function
SET_HANDLER_CLASS("configurable", CRewardableConstructor);
SET_HANDLER_CLASS("dwelling", CDwellingInstanceConstructor);
SET_HANDLER_CLASS("hero", CHeroInstanceConstructor);
SET_HANDLER_CLASS("town", CTownInstanceConstructor);
SET_HANDLER_CLASS("bank", CBankInstanceConstructor);
SET_HANDLER_CLASS("static", CObstacleConstructor);
SET_HANDLER_CLASS("", CObstacleConstructor);
SET_HANDLER("randomArtifact", CGArtifact);
SET_HANDLER("randomHero", CGHeroInstance);
SET_HANDLER("randomResource", CGResource);
SET_HANDLER("randomTown", CGTownInstance);
SET_HANDLER("randomMonster", CGCreature);
SET_HANDLER("randomDwelling", CGDwelling);
SET_HANDLER("generic", CGObjectInstance);
SET_HANDLER("cartographer", CCartographer);
SET_HANDLER("artifact", CGArtifact);
SET_HANDLER("blackMarket", CGBlackMarket);
SET_HANDLER("boat", CGBoat);
SET_HANDLER("bonusingObject", CGBonusingObject);
SET_HANDLER("borderGate", CGBorderGate);
SET_HANDLER("borderGuard", CGBorderGuard);
SET_HANDLER("monster", CGCreature);
SET_HANDLER("denOfThieves", CGDenOfthieves);
SET_HANDLER("event", CGEvent);
SET_HANDLER("garrison", CGGarrison);
SET_HANDLER("heroPlaceholder", CGHeroPlaceholder);
SET_HANDLER("keymaster", CGKeymasterTent);
SET_HANDLER("lighthouse", CGLighthouse);
SET_HANDLER("magi", CGMagi);
SET_HANDLER("magicSpring", CGMagicSpring);
SET_HANDLER("magicWell", CGMagicWell);
SET_HANDLER("market", CGMarket);
SET_HANDLER("mine", CGMine);
SET_HANDLER("obelisk", CGObelisk);
SET_HANDLER("observatory", CGObservatory);
SET_HANDLER("onceVisitable", CGOnceVisitable);
SET_HANDLER("pandora", CGPandoraBox);
SET_HANDLER("pickable", CGPickable);
SET_HANDLER("prison", CGHeroInstance);
SET_HANDLER("questGuard", CGQuestGuard);
SET_HANDLER("resource", CGResource);
SET_HANDLER("scholar", CGScholar);
SET_HANDLER("seerHut", CGSeerHut);
SET_HANDLER("shipyard", CGShipyard);
SET_HANDLER("shrine", CGShrine);
SET_HANDLER("sign", CGSignBottle);
SET_HANDLER("siren", CGSirens);
SET_HANDLER("monolith", CGMonolith);
SET_HANDLER("subterraneanGate", CGSubterraneanGate);
SET_HANDLER("whirlpool", CGWhirlpool);
SET_HANDLER("university", CGUniversity);
SET_HANDLER("oncePerHero", CGVisitableOPH);
SET_HANDLER("oncePerWeek", CGVisitableOPW);
SET_HANDLER("witch", CGWitchHut);
SET_HANDLER("terrain", CGTerrainPatch);
#undef SET_HANDLER_CLASS
#undef SET_HANDLER
}
CObjectClassesHandler::~CObjectClassesHandler()
{
for(auto p : objects)
delete p.second;
}
std::vector<JsonNode> CObjectClassesHandler::loadLegacyData(size_t dataSize)
{
CLegacyConfigParser parser("Data/Objects.txt");
size_t totalNumber = static_cast<size_t>(parser.readNumber()); // first line contains number of objects to read and nothing else
parser.endLine();
for (size_t i = 0; i < totalNumber; i++)
{
auto tmpl = new ObjectTemplate;
tmpl->readTxt(parser);
parser.endLine();
std::pair<si32, si32> key(tmpl->id.num, tmpl->subid);
legacyTemplates.insert(std::make_pair(key, std::shared_ptr<const ObjectTemplate>(tmpl)));
}
std::vector<JsonNode> ret(dataSize);// create storage for 256 objects
assert(dataSize == 256);
CLegacyConfigParser namesParser("Data/ObjNames.txt");
for (size_t i=0; i<256; i++)
{
ret[i]["name"].String() = namesParser.readString();
namesParser.endLine();
}
CLegacyConfigParser cregen1Parser("data/crgen1");
do
customNames[Obj::CREATURE_GENERATOR1].push_back(cregen1Parser.readString());
while(cregen1Parser.endLine());
CLegacyConfigParser cregen4Parser("data/crgen4");
do
customNames[Obj::CREATURE_GENERATOR4].push_back(cregen4Parser.readString());
while(cregen4Parser.endLine());
return ret;
}
/// selects preferred ID (or subID) for new object
template<typename Map>
si32 selectNextID(const JsonNode & fixedID, const Map & map, si32 fixedObjectsBound)
{
assert(fixedObjectsBound > 0);
if(fixedID.isNull())
{
auto lastID = map.empty() ? 0 : map.rbegin()->first;
return lastID < fixedObjectsBound ? fixedObjectsBound : lastID + 1;
}
auto id = static_cast<si32>(fixedID.Float());
if(id >= fixedObjectsBound)
logGlobal->error("Getting next ID overflowed: %d >= %d", id, fixedObjectsBound);
return id;
}
void CObjectClassesHandler::loadObjectEntry(const std::string & identifier, const JsonNode & entry, ObjectContainter * obj, bool isSubobject)
{
static const si32 fixedObjectsBound = 1000; // legacy value for backward compatibilitty
static const si32 fixedSubobjectsBound = 10000000; // large enough arbitrary value to avoid ID-collisions
si32 usedBound = fixedObjectsBound;
if(!handlerConstructors.count(obj->handlerName))
{
logGlobal->error("Handler with name %s was not found!", obj->handlerName);
return;
}
const auto convertedId = VLC->modh->normalizeIdentifier(entry.meta, CModHandler::scopeBuiltin(), identifier);
const auto & entryIndex = entry["index"];
bool useSelectNextID = !isSubobject || entryIndex.isNull();
if(useSelectNextID && isSubobject)
{
usedBound = fixedSubobjectsBound;
logGlobal->error("Subobject index is Null. convertedId = '%s' obj->id = %d", convertedId, obj->id);
}
si32 id = useSelectNextID ? selectNextID(entryIndex, obj->subObjects, usedBound)
: (si32)entryIndex.Float();
auto handler = handlerConstructors.at(obj->handlerName)();
handler->setType(obj->id, id);
handler->setTypeName(obj->identifier, convertedId);
if (customNames.count(obj->id) && customNames.at(obj->id).size() > id)
handler->init(entry, customNames.at(obj->id).at(id));
else
handler->init(entry);
//if (handler->getTemplates().empty())
{
auto range = legacyTemplates.equal_range(std::make_pair(obj->id, id));
for (auto & templ : boost::make_iterator_range(range.first, range.second))
{
handler->addTemplate(templ.second);
}
legacyTemplates.erase(range.first, range.second);
}
logGlobal->debug("Loaded object %s(%d)::%s(%d)", obj->identifier, obj->id, convertedId, id);
//some mods redefine content handlers in the decoration.json in such way:
//"core:sign" : { "types" : { "forgeSign" : { ...
static const std::vector<std::string> breakersRMG
{
"hota.hota decorations:hotaPandoraBox"
, "hota.hota decorations:hotaSubterreanGate"
};
const bool isExistingKey = obj->subObjects.count(id) > 0;
const bool isBreaker = std::any_of(breakersRMG.begin(), breakersRMG.end(),
[&handler](const std::string & str)
{
return str.compare(handler->subTypeName) == 0;
});
const bool passedHandler = !isExistingKey && !isBreaker;
if(passedHandler)
{
obj->subObjects[id] = handler;
obj->subIds[convertedId] = id;
}
else if(isExistingKey) //It's supposed that fan mods handlers are not overridden by default handlers
{
logGlobal->trace("Handler '%s' has not been overridden with handler '%s' in object %s(%d)::%s(%d)",
obj->subObjects[id]->subTypeName, obj->handlerName, obj->identifier, obj->id, convertedId, id);
}
else
{
logGlobal->warn("Handler '%s' for object %s(%d)::%s(%d) has not been activated as RMG breaker",
obj->handlerName, obj->identifier, obj->id, convertedId, id);
}
}
CObjectClassesHandler::ObjectContainter * CObjectClassesHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name)
{
auto obj = new ObjectContainter();
static const si32 fixedObjectsBound = 256; //Legacy value for backward compatibility
obj->identifier = name;
obj->name = json["name"].String();
obj->handlerName = json["handler"].String();
obj->base = json["base"];
obj->id = selectNextID(json["index"], objects, fixedObjectsBound);
if(json["defaultAiValue"].isNull())
obj->groupDefaultAiValue = boost::none;
else
obj->groupDefaultAiValue = static_cast<boost::optional<si32>>(json["defaultAiValue"].Integer());
for (auto entry : json["types"].Struct())
loadObjectEntry(entry.first, entry.second, obj);
return obj;
}
void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
{
auto object = loadFromJson(scope, data, normalizeIdentifier(scope, CModHandler::scopeBuiltin(), name));
objects[object->id] = object;
VLC->modh->identifiers.registerObject(scope, "object", name, object->id);
}
void CObjectClassesHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
{
auto object = loadFromJson(scope, data, normalizeIdentifier(scope, CModHandler::scopeBuiltin(), name));
assert(objects[(si32)index] == nullptr); // ensure that this id was not loaded before
objects[(si32)index] = object;
VLC->modh->identifiers.registerObject(scope, "object", name, object->id);
}
void CObjectClassesHandler::loadSubObject(const std::string & identifier, JsonNode config, si32 ID, boost::optional<si32> subID)
{
static const bool isSubObject = true;
config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not NULL
assert(objects.count(ID));
if (subID)
{
assert(objects.at(ID)->subObjects.count(subID.get()) == 0);
assert(config["index"].isNull());
config["index"].Float() = subID.get();
config["index"].setMeta(config.meta);
}
JsonUtils::inherit(config, objects.at(ID)->base);
loadObjectEntry(identifier, config, objects[ID], isSubObject);
}
void CObjectClassesHandler::removeSubObject(si32 ID, si32 subID)
{
assert(objects.count(ID));
assert(objects.at(ID)->subObjects.count(subID));
objects.at(ID)->subObjects.erase(subID); //TODO: cleanup string id map
}
std::vector<bool> CObjectClassesHandler::getDefaultAllowed() const
{
return std::vector<bool>(); //TODO?
}
TObjectTypeHandler CObjectClassesHandler::getHandlerFor(si32 type, si32 subtype) const
{
if (objects.count(type))
{
if (objects.at(type)->subObjects.count(subtype))
return objects.at(type)->subObjects.at(subtype);
}
std::string errorString = "Failed to find object of type " + std::to_string(type) + "::" + std::to_string(subtype);
logGlobal->error(errorString);
throw std::runtime_error(errorString);
}
TObjectTypeHandler CObjectClassesHandler::getHandlerFor(std::string scope, std::string type, std::string subtype) const
{
boost::optional<si32> id = VLC->modh->identifiers.getIdentifier(scope, "object", type, false);
if(id)
{
auto object = objects.at(id.get());
if(object->subIds.count(subtype))
{
si32 subId = object->subIds.at(subtype);
return object->subObjects.at(subId);
}
}
std::string errorString = "Failed to find object of type " + type + "::" + subtype;
logGlobal->error(errorString);
throw std::runtime_error(errorString);
}
TObjectTypeHandler CObjectClassesHandler::getHandlerFor(CompoundMapObjectID compoundIdentifier) const
{
return getHandlerFor(compoundIdentifier.primaryID, compoundIdentifier.secondaryID);
}
std::set<si32> CObjectClassesHandler::knownObjects() const
{
std::set<si32> ret;
for (auto entry : objects)
ret.insert(entry.first);
return ret;
}
std::set<si32> CObjectClassesHandler::knownSubObjects(si32 primaryID) const
{
std::set<si32> ret;
if (objects.count(primaryID))
{
for (auto entry : objects.at(primaryID)->subObjects)
ret.insert(entry.first);
}
return ret;
}
void CObjectClassesHandler::beforeValidate(JsonNode & object)
{
for (auto & entry : object["types"].Struct())
{
JsonUtils::inherit(entry.second, object["base"]);
for (auto & templ : entry.second["templates"].Struct())
JsonUtils::inherit(templ.second, entry.second["base"]);
}
}
void CObjectClassesHandler::afterLoadFinalization()
{
for(auto entry : objects)
{
for(auto obj : entry.second->subObjects)
{
obj.second->afterLoadFinalization();
if(obj.second->getTemplates().empty())
logGlobal->warn("No templates found for %d:%d", entry.first, obj.first);
}
}
//duplicate existing two-way portals to make reserve for RMG
auto& portalVec = objects[Obj::MONOLITH_TWO_WAY]->subObjects;
size_t portalCount = portalVec.size();
size_t currentIndex = portalCount;
while(portalVec.size() < 100)
{
portalVec[(si32)currentIndex] = portalVec[static_cast<si32>(currentIndex % portalCount)];
currentIndex++;
}
}
std::string CObjectClassesHandler::getObjectName(si32 type) const
{
if (objects.count(type))
return objects.at(type)->name;
logGlobal->error("Access to non existing object of type %d", type);
return "";
}
std::string CObjectClassesHandler::getObjectName(si32 type, si32 subtype) const
{
if (knownSubObjects(type).count(subtype))
{
auto name = getHandlerFor(type, subtype)->getCustomName();
if (name)
return name.get();
}
return getObjectName(type);
}
SObjectSounds CObjectClassesHandler::getObjectSounds(si32 type) const
{
if(objects.count(type))
return objects.at(type)->sounds;
logGlobal->error("Access to non existing object of type %d", type);
return SObjectSounds();
}
SObjectSounds CObjectClassesHandler::getObjectSounds(si32 type, si32 subtype) const
{
if(knownSubObjects(type).count(subtype))
return getHandlerFor(type, subtype)->getSounds();
else
return getObjectSounds(type);
}
std::string CObjectClassesHandler::getObjectHandlerName(si32 type) const
{
return objects.at(type)->handlerName;
}
boost::optional<si32> CObjectClassesHandler::getObjGroupAiValue(si32 primaryID) const
{
return objects.at(primaryID)->groupDefaultAiValue;
}
AObjectTypeHandler::AObjectTypeHandler():
type(-1), subtype(-1)
{
}
AObjectTypeHandler::~AObjectTypeHandler()
{
}
void AObjectTypeHandler::setType(si32 type, si32 subtype)
{
this->type = type;
this->subtype = subtype;
}
void AObjectTypeHandler::setTypeName(std::string type, std::string subtype)
{
this->typeName = type;
this->subTypeName = subtype;
}
static ui32 loadJsonOrMax(const JsonNode & input)
{
if (input.isNull())
return std::numeric_limits<ui32>::max();
else
return static_cast<ui32>(input.Float());
}
void AObjectTypeHandler::init(const JsonNode & input, boost::optional<std::string> name)
{
base = input["base"];
if (!input["rmg"].isNull())
{
rmgInfo.value = static_cast<ui32>(input["rmg"]["value"].Float());
rmgInfo.mapLimit = loadJsonOrMax(input["rmg"]["mapLimit"]);
rmgInfo.zoneLimit = loadJsonOrMax(input["rmg"]["zoneLimit"]);
rmgInfo.rarity = static_cast<ui32>(input["rmg"]["rarity"].Float());
} // else block is not needed - set in constructor
for (auto entry : input["templates"].Struct())
{
entry.second.setType(JsonNode::JsonType::DATA_STRUCT);
JsonUtils::inherit(entry.second, base);
auto tmpl = new ObjectTemplate;
tmpl->id = Obj(type);
tmpl->subid = subtype;
tmpl->stringID = entry.first; // FIXME: create "fullID" - type.object.template?
try
{
tmpl->readJson(entry.second);
templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
}
catch (const std::exception & e)
{
logGlobal->warn("Failed to load terrains for object %s: %s", entry.first, e.what());
}
}
if (input["name"].isNull())
objectName = name;
else
objectName.reset(input["name"].String());
for(const JsonNode & node : input["sounds"]["ambient"].Vector())
sounds.ambient.push_back(node.String());
for(const JsonNode & node : input["sounds"]["visit"].Vector())
sounds.visit.push_back(node.String());
for(const JsonNode & node : input["sounds"]["removal"].Vector())
sounds.removal.push_back(node.String());
if(input["aiValue"].isNull())
aiValue = boost::none;
else
aiValue = static_cast<boost::optional<si32>>(input["aiValue"].Integer());
if(input["battleground"].getType() == JsonNode::JsonType::DATA_STRING)
battlefield = input["battleground"].String();
else
battlefield = boost::none;
initTypeData(input);
}
bool AObjectTypeHandler::objectFilter(const CGObjectInstance *, std::shared_ptr<const ObjectTemplate>) const
{
return false; // by default there are no overrides
}
void AObjectTypeHandler::preInitObject(CGObjectInstance * obj) const
{
obj->ID = Obj(type);
obj->subID = subtype;
obj->typeName = typeName;
obj->subTypeName = subTypeName;
}
void AObjectTypeHandler::initTypeData(const JsonNode & input)
{
// empty implementation for overrides
}
boost::optional<std::string> AObjectTypeHandler::getCustomName() const
{
return objectName;
}
SObjectSounds AObjectTypeHandler::getSounds() const
{
return sounds;
}
void AObjectTypeHandler::addTemplate(std::shared_ptr<const ObjectTemplate> templ)
{
templates.push_back(templ);
}
void AObjectTypeHandler::addTemplate(JsonNode config)
{
config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not null
JsonUtils::inherit(config, base);
auto tmpl = new ObjectTemplate;
tmpl->id = Obj(type);
tmpl->subid = subtype;
tmpl->stringID.clear(); // TODO?
tmpl->readJson(config);
templates.emplace_back(tmpl);
}
std::vector<std::shared_ptr<const ObjectTemplate>> AObjectTypeHandler::getTemplates() const
{
return templates;
}
BattleField AObjectTypeHandler::getBattlefield() const
{
return battlefield ? BattleField::fromString(battlefield.get()) : BattleField::NONE;
}
std::vector<std::shared_ptr<const ObjectTemplate>>AObjectTypeHandler::getTemplates(TerrainId terrainType) const
{
std::vector<std::shared_ptr<const ObjectTemplate>> templates = getTemplates();
std::vector<std::shared_ptr<const ObjectTemplate>> filtered;
std::copy_if(templates.begin(), templates.end(), std::back_inserter(filtered), [&](std::shared_ptr<const ObjectTemplate> obj)
{
return obj->canBePlacedAt(terrainType);
});
// H3 defines allowed terrains in a weird way - artifacts, monsters and resources have faulty masks here
// Perhaps we should re-define faulty templates and remove this workaround (already done for resources)
if (type == Obj::ARTIFACT || type == Obj::MONSTER)
return templates;
else
return filtered;
}
std::shared_ptr<const ObjectTemplate> AObjectTypeHandler::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
{
std::vector<std::shared_ptr<const ObjectTemplate>> ret = getTemplates(terrainType);
for (const auto & tmpl: ret)
{
if (objectFilter(object, tmpl))
return tmpl;
}
return std::shared_ptr<const ObjectTemplate>(); //empty
}
const RandomMapInfo & AObjectTypeHandler::getRMGInfo()
{
return rmgInfo;
}
boost::optional<si32> AObjectTypeHandler::getAiValue() const
{
return aiValue;
}
bool AObjectTypeHandler::isStaticObject()
{
return false; // most of classes are not static
}
void AObjectTypeHandler::afterLoadFinalization()
{
}
VCMI_LIB_NAMESPACE_END
|