1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/lib/mapObjects/CObjectHandler.cpp

527 lines
12 KiB
C++
Raw Normal View History

/*
2014-06-05 19:57:43 +03:00
* 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"
2015-11-15 17:07:15 +02:00
#include "../StringConstants.h"
#include "../mapping/CMap.h"
2014-06-05 19:57:43 +03:00
#include "CObjectClassesHandler.h"
#include "CGTownInstance.h"
2014-06-05 19:57:43 +03:00
#include "../serializer/JsonSerializeFormat.h"
2014-06-05 19:57:43 +03:00
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)
2014-06-05 19:57:43 +03:00
{
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
2014-06-05 19:57:43 +03:00
{}
IObjectInterface::~IObjectInterface()
{}
IObjectInterface::IObjectInterface()
{}
void IObjectInterface::initObj(CRandomGenerator & rand)
2014-06-05 19:57:43 +03:00
{}
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()
{
2017-08-10 18:39:27 +02:00
logGlobal->trace("\t\tReading resources prices ");
2014-06-05 19:57:43 +03:00
const JsonNode config2(ResourceID("config/resources.json"));
for(const JsonNode &price : config2["resources_prices"].Vector())
{
resVals.push_back(static_cast<ui32>(price.Float()));
2014-06-05 19:57:43 +03:00
}
2017-08-10 18:39:27 +02:00
logGlobal->trace("\t\tDone loading resource prices!");
2014-06-05 19:57:43 +03:00
}
CGObjectInstance::CGObjectInstance():
pos(-1,-1,-1),
ID(Obj::NO_OBJ),
subID(-1),
tempOwner(PlayerColor::UNFLAGGABLE),
blockVisit(false)
{
}
CGObjectInstance::~CGObjectInstance()
{
}
int32_t CGObjectInstance::getObjGroupIndex() const
{
return ID.num;
}
int32_t CGObjectInstance::getObjTypeIndex() const
{
return subID;
}
int3 CGObjectInstance::getPosition() const
{
return pos;
}
2014-06-05 19:57:43 +03:00
void CGObjectInstance::setOwner(PlayerColor ow)
{
tempOwner = ow;
2014-06-05 19:57:43 +03:00
}
int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
{
return appearance->getWidth();
2014-06-05 19:57:43 +03:00
}
int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
{
return appearance->getHeight();
2014-06-05 19:57:43 +03:00
}
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);
2014-06-05 19:57:43 +03:00
}
bool CGObjectInstance::blockingAt(int x, int y) const
{
return appearance->isBlockedAt(pos.x - x, pos.y - y);
2014-06-05 19:57:43 +03:00
}
bool CGObjectInstance::coveringAt(int x, int y) const
{
return appearance->isVisibleAt(pos.x - x, pos.y - y);
2014-06-05 19:57:43 +03:00
}
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))
2014-06-05 19:57:43 +03:00
ret.insert(int3(pos.x - w, pos.y - h, pos.z));
}
}
return ret;
}
std::set<int3> CGObjectInstance::getBlockedOffsets() const
{
return appearance->getBlockedOffsets();
2014-06-05 19:57:43 +03:00
}
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
if (ID == Obj::HERO)
{
//adjust for the prison offset
pos = visitablePos();
}
2014-06-05 19:57:43 +03:00
cb->gameState()->map->addBlockVisTiles(this);
}
void CGObjectInstance::initObj(CRandomGenerator & rand)
2014-06-05 19:57:43 +03:00
{
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)
2014-06-05 19:57:43 +03:00
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();
2014-06-05 19:57:43 +03:00
}
int CGObjectInstance::getSightRadius() const
2014-06-05 19:57:43 +03:00
{
return 3;
}
2014-06-05 19:57:43 +03:00
int3 CGObjectInstance::getVisitableOffset() const
{
return appearance->getVisitableOffset();
2014-06-05 19:57:43 +03:00
}
void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
{
GiveBonus gbonus;
gbonus.bonus.type = Bonus::NONE;
gbonus.id = heroID.getNum();
gbonus.bonus.duration = (Bonus::BonusDuration)duration;
2014-06-05 19:57:43 +03:00
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);
}
2014-06-05 19:57:43 +03:00
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);
2014-06-05 19:57:43 +03:00
}
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();
2014-06-05 19:57:43 +03:00
}
bool CGObjectInstance::passableFor(PlayerColor color) const
{
return false;
2014-06-05 19:57:43 +03:00
}
void CGObjectInstance::updateFrom(const JsonNode & data)
{
}
void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
{
//only save here, loading is handled by map loader
if(handler.saving)
{
handler.serializeString("type", typeName);
2016-02-22 22:43:57 +02:00
handler.serializeString("subtype", subTypeName);
handler.serializeInt("x", pos.x);
handler.serializeInt("y", pos.y);
handler.serializeInt("l", pos.z);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
JsonNode app;
appearance->writeJson(app, false);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
handler.serializeRaw("template",app, boost::none);
}
{
auto options = handler.enterStruct("options");
serializeJsonOptions(handler);
}
}
void CGObjectInstance::afterAddToMap(CMap * map)
{
//nothing here
}
2022-09-17 13:04:01 +02:00
void CGObjectInstance::afterRemoveFromMap(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);
}
BattleField CGObjectInstance::getBattlefield() const
{
return VLC->objtypeh->getHandlerFor(ID, subID)->getBattlefield();
}
2014-06-05 19:57:43 +03:00
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
2014-06-05 19:57:43 +03:00
{
if(tile->terType.isWater() && (!tile->blocked || tile->blockingObjects.front()->ID == Obj::BOAT)) //and is water and is not blocked or is blocked by boat
2014-06-05 19:57:43 +03:00
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());
2014-06-05 19:57:43 +03:00
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));
}