File: encounter.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (186 lines) | stat: -rw-r--r-- 4,976 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "mm/mm1/game/encounter.h"
#include "mm/mm1/data/monsters.h"
#include "mm/mm1/maps/maps.h"
#include "mm/mm1/events.h"
#include "mm/mm1/globals.h"
#include "mm/mm1/mm1.h"

namespace MM {
namespace MM1 {
namespace Game {

void Encounter::execute() {
	if (!g_globals->_encountersOn)
		return;

	Maps::Map &map = *g_maps->_currentMap;
	int comp, maxRand, maxVal;
	const Monster *monsterP;
	_bribeFleeCtr = _bribeAlignmentCtr = 0;
	_alignmentsChanged = 0;

	// In manual mode, the scripts have already set up
	// a list of monsters to use
	if (!_manual) {
		_monsterSummaries.clear();
		_levelIndex = 0;
	}

	_totalLevels = _highestLevel = 0;
	_levelOffset = _monsterImgNum = _maxLevelForImg = _fleeThreshold = 0;

	for (uint i = 0; i < g_globals->_party.size(); ++i) {
		const Character &c = g_globals->_party[i];
		_highestLevel = MAX(_highestLevel, (int)c._level._current);

		if (!(c._condition & (BAD_CONDITION | PARALYZED | UNCONSCIOUS)))
			_totalLevels = MIN(_totalLevels + c._level._current, 255);
	}

	_totalLevels /= 2;
	_highestLevel /= 2;

	bool firstLoop = !_manual;
	_manual = false;

	while (firstLoop || _levelIndex < _totalLevels) {
		randomAdjust();
		firstLoop = false;

		maxRand = _levelOffset + _highestLevel;
		if (maxRand >= 2) {
			int highestRand = map[Maps::MAP_33];
			maxRand = MIN(maxRand, highestRand);
			comp = getRandomNumber(maxRand);
		} else {
			comp = 1;
		}

		maxVal = map[Maps::MAP_47];
		if (comp < maxVal) {
			comp = MIN(maxVal, 10);
		}

		assert(_monsterSummaries.size() < MAX_COMBAT_MONSTERS);
		_monsterNum = getRandomNumber(16);
		_monsterSummaries.push_back(MonsterSummary(_monsterNum, comp));

		_monsterLevel = comp;
		_levelIndex += comp;

		if (_monsterSummaries.size() < MAX_COMBAT_MONSTERS) {
			if (_monsterSummaries.size() >= map[Maps::MAP_MAX_MONSTERS])
				goto exit_loop;

			monsterP = getMonster();
			maxVal = getRandomNumber(monsterP->_count);

			for (int i = 0; i < maxVal; ++i) {
				assert(!_monsterSummaries.empty());
				_monsterSummaries.push_back(_monsterSummaries.back());
				_levelIndex += _monsterSummaries.back()._level;

				if (_monsterSummaries.size() >= MAX_COMBAT_MONSTERS)
					goto exit_loop;

				if (_monsterSummaries.size() >= map[Maps::MAP_MAX_MONSTERS])
					goto exit_loop;
			}
		} else {
			goto exit_loop;
		}
	}

exit_loop:
	_monsterList.clear();

	for (uint i = 0; i < _monsterSummaries.size(); ++i) {
		maxVal = (_monsterSummaries[i]._level - 1) * 16 +
			_monsterSummaries[i]._num;

		if (_monsterSummaries[i]._level < 1 || _monsterSummaries[i]._level > 12
				|| maxVal >= 196) {
			_monsterSummaries[i]._level = 10;
			_monsterSummaries[i]._num = getRandomNumber(15);
		}

		// Add monster details to list
		_monsterLevel = _monsterSummaries[i]._level;
		const Monster &srcMons = (*g_globals->_monsters)[_monsterSummaries[i]._num];
		_monsterList.push_back(srcMons);
		Monster &mons = _monsterList.back();
		mons._level = _monsterSummaries[i]._level;

		if (_monsterLevel > _maxLevelForImg) {
			_maxLevelForImg = _monsterLevel;
			_fleeThreshold = mons._fleeThreshold;
			_monsterImgNum = mons._imgNum;
		}
	}

	g_events->addView("Encounter");
}

void Encounter::randomAdjust() {
	int rval = getRandomNumber(100);
	_levelOffset = 0;

	if (rval < 51) {
	} else if (rval < 71)
		_levelOffset += 1;
	else if (rval < 86)
		_levelOffset += 2;
	else if (rval < 96)
		_levelOffset += 3;
	else
		_levelOffset += 4;
}

const Monster *Encounter::getMonster() {
	assert(_monsterNum > 0 && _monsterLevel > 0);
	return &(*g_globals->_monsters)[_monsterNum + ((_monsterLevel - 1) * 16)];
}

bool Encounter::checkSurroundParty() const {
	return getRandomNumber(100) > _fleeThreshold;
}

void Encounter::changeCharAlignment(Alignment align) {
	if (g_globals->_currCharacter->_alignment != align) {
		g_globals->_currCharacter->_alignment = align;
		++_alignmentsChanged;
	}
}

void Encounter::clearMonsters() {
	_monsterSummaries.clear();
}

void Encounter::addMonster(byte id, byte level) {
	_monsterSummaries.push_back(MonsterSummary(id, level));
}

} // namespace Game
} // namespace MM1
} // namespace MM