File: BattleLayout.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 (81 lines) | stat: -rw-r--r-- 2,932 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
/*
 * BattleLayout.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 "BattleLayout.h"

#include "../GameSettings.h"
#include "../IGameCallback.h"
#include "../VCMI_Lib.h"
#include "../json/JsonNode.h"
#include "../mapObjects/CArmedInstance.h"

VCMI_LIB_NAMESPACE_BEGIN

BattleLayout BattleLayout::createDefaultLayout(IGameCallback * cb, const CArmedInstance * attacker, const CArmedInstance * defender)
{
	return createLayout(cb, "default", attacker, defender);
}

BattleLayout BattleLayout::createLayout(IGameCallback * cb, const std::string & layoutName, const CArmedInstance * attacker, const CArmedInstance * defender)
{
	const auto & loadHex = [](const JsonNode & node)
	{
		if (node.isNull())
			return BattleHex();
		else
			return BattleHex(node.Integer());
	};

	const auto & loadUnits = [](const JsonNode & node)
	{
		UnitsArrayType::value_type result;
		for (size_t i = 0; i < GameConstants::ARMY_SIZE; ++i)
		{
			if (!node[i].isNull())
				result[i] = BattleHex(node[i].Integer());
		}
		return result;
	};

	const JsonNode & configRoot = cb->getSettings().getValue(EGameSettings::COMBAT_LAYOUTS);
	const JsonNode & config = configRoot[layoutName];

	BattleLayout result;

	result.commanders[BattleSide::ATTACKER] = loadHex(config["attackerCommander"]);
	result.commanders[BattleSide::DEFENDER] = loadHex(config["defenderCommander"]);

	for (size_t i = 0; i < 4; ++i)
		result.warMachines[BattleSide::ATTACKER][i] = loadHex(config["attackerWarMachines"][i]);

	for (size_t i = 0; i < 4; ++i)
		result.warMachines[BattleSide::DEFENDER][i] = loadHex(config["defenderWarMachines"][i]);

	if (attacker->formation == EArmyFormation::LOOSE && !config["attackerUnitsLoose"].isNull())
		result.units[BattleSide::ATTACKER] = loadUnits(config["attackerUnitsLoose"][attacker->stacksCount() - 1]);
	else if (attacker->formation == EArmyFormation::TIGHT && !config["attackerUnitsTight"].isNull())
		result.units[BattleSide::ATTACKER] = loadUnits(config["attackerUnitsTight"][attacker->stacksCount() - 1]);
	else
		result.units[BattleSide::ATTACKER] = loadUnits(config["attackerUnits"]);

	if (defender->formation == EArmyFormation::LOOSE && !config["defenderUnitsLoose"].isNull())
		result.units[BattleSide::DEFENDER] = loadUnits(config["defenderUnitsLoose"][defender->stacksCount() - 1]);
	else if (defender->formation == EArmyFormation::TIGHT && !config["defenderUnitsTight"].isNull())
		result.units[BattleSide::DEFENDER] = loadUnits(config["defenderUnitsTight"][defender->stacksCount() - 1]);
	else
		result.units[BattleSide::DEFENDER] = loadUnits(config["defenderUnits"]);

	result.obstaclesAllowed = config["obstaclesAllowed"].Bool();
	result.tacticsAllowed = config["tacticsAllowed"].Bool();

	return result;
}

VCMI_LIB_NAMESPACE_END