File: ObjectConfig.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (189 lines) | stat: -rw-r--r-- 5,040 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
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
/*
 * ObjectConfig.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 <boost/bimap.hpp>
#include <boost/assign.hpp>
#include "ObjectInfo.h"
#include "ObjectConfig.h"

#include "../VCMI_Lib.h"
#include "../mapObjectConstructors/CObjectClassesHandler.h"
#include "../mapObjectConstructors/AObjectTypeHandler.h"
#include "../serializer/JsonSerializeFormat.h"

VCMI_LIB_NAMESPACE_BEGIN

void ObjectConfig::addBannedObject(const CompoundMapObjectID & objid)
{
	// FIXME: We do not need to store the object info, just the id

	bannedObjects.push_back(objid);

	logGlobal->info("Banned object of type %d.%d", objid.primaryID, objid.secondaryID);
}

void ObjectConfig::addCustomObject(const ObjectInfo & object, const CompoundMapObjectID & objid)
{
	customObjects.push_back(object);
	auto & lastObject = customObjects.back();
	lastObject.setAllTemplates(objid.primaryID, objid.secondaryID);

	assert(lastObject.templates.size() > 0);
	logGlobal->info("Added custom object of type %d.%d", objid.primaryID, objid.secondaryID);
}

void ObjectConfig::serializeJson(JsonSerializeFormat & handler)
{
	// TODO: We need serializer utility for list of enum values

	static const boost::bimap<EObjectCategory, std::string> OBJECT_CATEGORY_STRINGS = boost::assign::list_of<boost::bimap<EObjectCategory, std::string>::relation>
		(EObjectCategory::OTHER, "other")
		(EObjectCategory::ALL, "all")
		(EObjectCategory::NONE, "none")
		(EObjectCategory::CREATURE_BANK, "creatureBank")
		(EObjectCategory::BONUS, "bonus")
		(EObjectCategory::DWELLING, "dwelling")
		(EObjectCategory::RESOURCE, "resource")
		(EObjectCategory::RESOURCE_GENERATOR, "resourceGenerator")
		(EObjectCategory::SPELL_SCROLL, "spellScroll")
		(EObjectCategory::RANDOM_ARTIFACT, "randomArtifact")
		(EObjectCategory::PANDORAS_BOX, "pandorasBox")
		(EObjectCategory::QUEST_ARTIFACT, "questArtifact")
		(EObjectCategory::SEER_HUT, "seerHut");


	// TODO: Separate into individual methods to enforce RAII destruction?
	{
		auto categories = handler.enterArray("bannedCategories");
		if (handler.saving)
		{
			for (const auto& category : bannedObjectCategories)
			{
				auto str = OBJECT_CATEGORY_STRINGS.left.at(category);
				categories.serializeString(categories.size(), str);
			}
		}
		else
		{
			std::vector<std::string> categoryNames;
			categories.serializeArray(categoryNames);

			for (const auto & categoryName : categoryNames)
			{
				auto it = OBJECT_CATEGORY_STRINGS.right.find(categoryName);
				if (it != OBJECT_CATEGORY_STRINGS.right.end())
				{
					bannedObjectCategories.push_back(it->second);
				}
			}
		}
	}

	// FIXME: Doesn't seem to use this field at all
	
	{
		auto bannedObjectData = handler.enterArray("bannedObjects");	
		if (handler.saving)
		{

			// FIXME: Do we even need to serialize / store banned objects?
			/*
			for (const auto & object : bannedObjects)
			{
				// TODO: Translate id back to string?


				JsonNode node;
				node.String() = VLC->objtypeh->getHandlerFor(object.primaryID, object.secondaryID);
				// TODO: Check if AI-generated code is right


			}
			// handler.serializeRaw("bannedObjects", node, std::nullopt);

			*/
		}
		else
		{
			std::vector<std::string> objectNames;
			bannedObjectData.serializeArray(objectNames);

			for (const auto & objectName : objectNames)
			{
				VLC->objtypeh->resolveObjectCompoundId(objectName,
					[this](CompoundMapObjectID objid)
					{
						addBannedObject(objid);
					}
				);
				
			}
		}
	}

	auto commonObjectData = handler.getCurrent()["commonObjects"].Vector();	
	if (handler.saving)
	{

		//TODO?
	}
	else
	{
		for (const auto & objectConfig : commonObjectData)
		{
			auto objectName = objectConfig["id"].String();
			auto rmg = objectConfig["rmg"].Struct();

			// TODO: Use common code with default rmg config
			auto objectValue = rmg["value"].Integer();
			auto objectProbability = rmg["rarity"].Integer();

			auto objectMaxPerZone = rmg["zoneLimit"].Integer();
			if (objectMaxPerZone == 0)
			{
				objectMaxPerZone = std::numeric_limits<int>::max();
			}

			VLC->objtypeh->resolveObjectCompoundId(objectName,

				[this, objectValue, objectProbability, objectMaxPerZone](CompoundMapObjectID objid)
				{
					ObjectInfo object(objid.primaryID, objid.secondaryID);
					
					// TODO: Configure basic generateObject function

					object.value = objectValue;
					object.probability = objectProbability;
					object.maxPerZone = objectMaxPerZone;
					addCustomObject(object, objid);
				}
			);
			
		}
	}
}

const std::vector<ObjectInfo> & ObjectConfig::getConfiguredObjects() const
{
	return customObjects;
}

const std::vector<CompoundMapObjectID> & ObjectConfig::getBannedObjects() const
{
	return bannedObjects;
}

const std::vector<ObjectConfig::EObjectCategory> & ObjectConfig::getBannedObjectCategories() const
{
	return bannedObjectCategories;
}

VCMI_LIB_NAMESPACE_END