File: MapObjectsEvaluator.cpp

package info (click to toggle)
vcmi 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: cpp: 181,738; sh: 220; python: 178; ansic: 69; objc: 66; xml: 59; makefile: 34
file content (140 lines) | stat: -rw-r--r-- 4,875 bytes parent folder | download
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
#include "StdInc.h"
#include "MapObjectsEvaluator.h"
#include "../../lib/GameConstants.h"
#include "../../lib/VCMI_Lib.h"
#include "../../lib/CCreatureHandler.h"
#include "../../lib/CHeroHandler.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/mapObjects/MiscObjects.h"
#include "../../lib/CRandomGenerator.h"
#include "../../lib/spells/CSpellHandler.h"

MapObjectsEvaluator & MapObjectsEvaluator::getInstance()
{
	static std::unique_ptr<MapObjectsEvaluator> singletonInstance;
	if(singletonInstance == nullptr)
		singletonInstance.reset(new MapObjectsEvaluator());

	return *(singletonInstance.get());
}

MapObjectsEvaluator::MapObjectsEvaluator()
{
	for(auto primaryID : VLC->objtypeh->knownObjects())
	{
		for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
		{
			auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
			if(handler && !handler->isStaticObject())
			{
				if(handler->getAiValue() != boost::none)
				{
					objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = handler->getAiValue().get();
				}
				else if(VLC->objtypeh->getObjGroupAiValue(primaryID) != boost::none) //if value is not initialized - fallback to default value for this object family if it exists
				{
					objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = VLC->objtypeh->getObjGroupAiValue(primaryID).get();
				}
				else //some default handling when aiValue not found, objects that require advanced properties (unavailable from handler) get their value calculated in getObjectValue
				{
					objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = 0;
				}
			}
		}
	}
}

boost::optional<int> MapObjectsEvaluator::getObjectValue(int primaryID, int secondaryID) const
{
	CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
	auto object = objectDatabase.find(internalIdentifier);
	if(object != objectDatabase.end())
		return object->second;

	logGlobal->trace("Unknown object for AI, ID: " + std::to_string(primaryID) + ", SubID: " + std::to_string(secondaryID));
	return boost::optional<int>();
}

boost::optional<int> MapObjectsEvaluator::getObjectValue(const CGObjectInstance * obj) const
{
	if(obj->ID == Obj::HERO)
	{
		//special case handling: in-game heroes have hero ID as object subID, but when reading configs available hero object subID's are hero classes
		auto hero = dynamic_cast<const CGHeroInstance*>(obj);
		return getObjectValue(obj->ID, hero->type->heroClass->getIndex());
	}
	else if(obj->ID == Obj::PRISON)
	{
		//special case: in-game prison subID is captured hero ID, but config has one subID with index 0 for normal prison - use that one
		return getObjectValue(obj->ID, 0);
	}
	else if(obj->ID == Obj::CREATURE_GENERATOR1 || obj->ID == Obj::CREATURE_GENERATOR4)
	{
		auto dwelling = dynamic_cast<const CGDwelling *>(obj);
		int aiValue = 0;
		for(auto & creLevel : dwelling->creatures)
		{
			for(auto & creatureID : creLevel.second)
			{
				auto creature = VLC->creh->objects[creatureID];
				aiValue += (creature->AIValue * creature->growth);
			}
		}
		return aiValue;
	}
	else if(obj->ID == Obj::ARTIFACT)
	{
		auto artifactObject = dynamic_cast<const CGArtifact *>(obj);
		switch(artifactObject->storedArtifact->artType->aClass)
		{
		case CArtifact::EartClass::ART_TREASURE:
			return 2000;
		case CArtifact::EartClass::ART_MINOR:
			return 5000;
		case CArtifact::EartClass::ART_MAJOR:
			return 10000;
		case CArtifact::EartClass::ART_RELIC:
			return 20000;
		case CArtifact::EartClass::ART_SPECIAL:
			return 20000;
		default:
			return 0; //invalid artifact class
		}
	}
	else if(obj->ID == Obj::SPELL_SCROLL)
	{
		auto scrollObject = dynamic_cast<const CGArtifact *>(obj);
		auto spell = scrollObject->storedArtifact->getGivenSpellID().toSpell();
		if(spell)
		{
			switch(spell->getLevel())
			{
			case 0: return 0; //scroll with creature ability? Let's assume it is useless
			case 1: return 1000;
			case 2: return 2000;
			case 3: return 5000;
			case 4: return 10000;
			case 5: return 20000;
			default: logAi->warn("AI detected spell scroll with spell level %s", spell->getLevel());
			}
		}
		else
			logAi->warn("AI found spell scroll with invalid spell ID: %s", scrollObject->storedArtifact->getGivenSpellID());
	}

	return getObjectValue(obj->ID, obj->subID);
}

void MapObjectsEvaluator::addObjectData(int primaryID, int secondaryID, int value) //by current design it updates value if already in AI database
{
	CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
	objectDatabase[internalIdentifier] = value;
}

void MapObjectsEvaluator::removeObjectData(int primaryID, int secondaryID)
{
	CompoundMapObjectID internalIdentifier = CompoundMapObjectID(primaryID, secondaryID);
	vstd::erase_if_present(objectDatabase, internalIdentifier);
}