-
Notifications
You must be signed in to change notification settings - Fork 121
/
chargenchoices.cpp
410 lines (321 loc) · 10.8 KB
/
chargenchoices.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos 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.
*
* xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file
* The character choices in the character generator.
*/
#include "src/common/util.h"
#include "src/aurora/2dareg.h"
#include "src/aurora/2dafile.h"
#include "src/aurora/gff3file.h"
#include "src/engines/nwn/creature.h"
#include "src/engines/nwn/item.h"
#include "src/engines/nwn/module.h"
#include "src/engines/nwn/gui/chargen/chargenchoices.h"
namespace Engines {
namespace NWN {
CharGenChoices::CharGenChoices() : _characterUsed(false) {
_creature = new Creature();
init();
}
CharGenChoices::~CharGenChoices() {
if (!_characterUsed)
delete _creature;
}
void CharGenChoices::init() {
_classId = 0;
_goodness = 101;
_loyalty = 101;
_abilities.assign(6, 8);
_racialAbilities.assign(6, 0);
_package = UINT8_MAX;
_skills.assign(28, 0);
_spellSchool = UINT8_MAX;
_domain1 = UINT8_MAX;
_domain2 = UINT8_MAX;
}
void CharGenChoices::applyChoices() {
// Rise level
_creature->changeClassLevel(_classId, 1);
// Set Alignment
_creature->setGoodEvil(_goodness);
_creature->setLawChaos(_loyalty);
// Set Abilities
size_t ab = 0;
for (;ab < 6; ++ab) {
Ability ability = static_cast<Ability>(ab);
_creature->setAbility(ability, _abilities[ab] + _racialAbilities[ab]);
}
// Set package
if (_package != UINT8_MAX)
_creature->setStartingPackage(_package);
//TODO Apply package
// Set skills
size_t skillID = 0;
for (std::vector<uint8>::iterator s = _skills.begin(); s != _skills.end(); ++s, ++skillID)
_creature->setSkillRank(skillID, *s);
// Set feats
for (std::vector<uint32>::iterator f = _classFeats.begin(); f != _classFeats.end(); ++f)
_creature->setFeat(*f);
for (std::vector<uint32>::iterator f = _racialFeats.begin(); f != _racialFeats.end(); ++f)
_creature->setFeat(*f);
for (std::vector<uint32>::iterator f = _normalFeats.begin(); f != _normalFeats.end(); ++f)
_creature->setFeat(*f);
// Set domains if any.
if (_domain1 != UINT8_MAX && _domain2 != UINT8_MAX)
_creature->setDomains(_classId, _domain1, _domain2);
// Set spells if any.
if (!_spells.empty()) {
for (size_t lvl = 0; lvl < _spells.size(); ++lvl) {
for (size_t s = 0; s < _spells[lvl].size(); ++s)
_creature->setKnownSpell(_classId, lvl, _spells[lvl][s]);
}
if (_spellSchool != UINT8_MAX)
_creature->setSchool(_classId, _spellSchool);
}
// Set sound set.
_creature->setSoundSet(_soundSet);
// Set appearance.
// TODO: Apply choices from user and not default values.
_creature->setAppearance(_creature->getRace());
Aurora::GFF3File gff = Aurora::GFF3File("NW_CLOTH001", Aurora::kFileTypeUTI);
Item *armor = new Item(gff.getTopLevel());
_creature->addEquippedItem(armor);
_creature->setPhenotype(0);
_creature->setColorSkin(0);
_creature->setColorHair(0);
_creature->setColorTatto1(0);
_creature->setColorTatto2(0);
_creature->setHead(1);
}
void CharGenChoices::reset() {
delete _creature;
_creature = new Creature();
init();
_racialFeats.clear();
_normalFeats.clear();
_classFeats.clear();
}
const Creature &CharGenChoices::getCharacter() {
return *_creature;
}
void CharGenChoices::setGender(Gender gender) {
_creature->setGender(gender);
}
void CharGenChoices::setRace(uint32 race) {
if (race == kRaceInvalid) {
warning("Setting invalid race: %d", race);
return;
}
_creature->setRace(race);
_racialFeats.clear();
const Aurora::TwoDAFile &twodaRace = TwoDAReg.get2DA("racialtypes");
const Aurora::TwoDAFile &twodaFeatRace = TwoDAReg.get2DA(
twodaRace.getRow(race).getString("FeatsTable"));
for (size_t it = 0; it < twodaFeatRace.getRowCount(); ++it) {
const Aurora::TwoDARow &rowFeatRace = twodaFeatRace.getRow(it);
_racialFeats.push_back(rowFeatRace.getInt("FeatIndex"));
}
}
void CharGenChoices::setPortrait(const Common::UString &portrait) {
_creature->setPortrait(portrait);
}
void CharGenChoices::setClass(uint32 classId) {
_classId = classId;
// Add granted class feats.
_classFeats.clear();
const Aurora::TwoDAFile &twodaClasses = TwoDAReg.get2DA("classes");
const Aurora::TwoDAFile &twodaClsFeat = TwoDAReg.get2DA(twodaClasses.getRow(classId).getString("FeatsTable"));
for (size_t it = 0; it < twodaClsFeat.getRowCount(); ++it) {
const Aurora::TwoDARow &rowFeat = twodaClsFeat.getRow(it);
if (rowFeat.getInt("List") != 3)
continue;
if (rowFeat.getInt("GrantedOnLevel") != _creature->getHitDice() + 1)
continue;
if (!hasFeat(rowFeat.getInt("FeatIndex")))
_classFeats.push_back(rowFeat.getInt("FeatIndex"));
}
}
void CharGenChoices::setAlign(uint8 goodness, uint8 loyalty) {
_goodness = goodness;
_loyalty = loyalty;
}
void CharGenChoices::setAbilities(std::vector<uint8> abilities,
std::vector<uint8> racialAbilities) {
_abilities = abilities;
_racialAbilities = racialAbilities;
// Exchange intelligence and wisdom to be consistent with kAbility.
_abilities[4] = abilities[3];
_abilities[3] = abilities[4];
}
void CharGenChoices::setPackage(uint8 package) {
_package = package;
}
void CharGenChoices::setSkill(size_t skillIndex, uint8 rank) {
_skills[skillIndex] = rank;
}
void CharGenChoices::setNotUsedSkills(uint8 notUsedSkills) {
_notUsedSkills = notUsedSkills;
}
void CharGenChoices::setFeat(uint32 feat) {
_normalFeats.push_back(feat);
}
void CharGenChoices::setSpellSchool(uint8 spellSchool) {
_spellSchool = spellSchool;
}
void CharGenChoices::setDomains(uint8 domain1, uint8 domain2) {
_domain1 = domain1;
_domain2 = domain2;
}
void CharGenChoices::setSpell(size_t spellLevel, uint16 spell) {
if (_spells.size() < spellLevel + 1)
_spells.resize(spellLevel + 1);
_spells[spellLevel].push_back(spell);
}
void CharGenChoices::setSoundSet(uint32 soundSetID) {
_soundSet = soundSetID;
}
void CharGenChoices::useCharacter(Module *module) {
module->usePC(_creature);
_characterUsed = true;
}
bool CharGenChoices::hasFeat(uint32 featId) const {
for (std::vector<uint32>::const_iterator f = _normalFeats.begin(); f != _normalFeats.end(); ++f)
if (*f == featId)
return true;
for (std::vector<uint32>::const_iterator f = _racialFeats.begin(); f != _racialFeats.end(); ++f)
if (*f == featId)
return true;
for (std::vector<uint32>::const_iterator f = _classFeats.begin(); f != _classFeats.end(); ++f)
if (*f == featId)
return true;
return _creature->hasFeat(featId);
}
bool CharGenChoices::hasPrereqFeat(uint32 featId, bool isClassFeat) {
const Aurora::TwoDAFile &twodaFeats = TwoDAReg.get2DA("feat");
const Aurora::TwoDARow &row = twodaFeats.getRow(featId);
// Some feats have been removed. Check if it's the case.
if (row.empty("FEAT"))
return false;
if (!row.getInt("ALLCLASSESCANUSE") && !isClassFeat)
return false;
// Check abilities.
if ((uint32) row.getInt("MINSTR") > getAbility(kAbilityStrength))
return false;
if ((uint32) row.getInt("MINDEX") > getAbility(kAbilityDexterity))
return false;
if ((uint32) row.getInt("MININT") > getAbility(kAbilityIntelligence))
return false;
if ((uint32) row.getInt("MINWIS") > getAbility(kAbilityWisdom))
return false;
if ((uint32) row.getInt("MINCHA") > getAbility(kAbilityCharisma))
return false;
if ((uint32) row.getInt("MINCON") > getAbility(kAbilityConstitution))
return false;
// Check if the character has the prerequisite feats.
if (!row.empty("PREREQFEAT1") && !hasFeat(row.getInt("PREREQFEAT1")))
return false;
if (!row.empty("PREREQFEAT2") && !hasFeat(row.getInt("PREREQFEAT2")))
return false;
if (!row.empty("OrReqFeat0")) {
bool OrReqFeat = hasFeat(row.getInt("OrReqFeat0"));
if (!row.empty("OrReqFeat1")) {
OrReqFeat = OrReqFeat || hasFeat(row.getInt("OrReqFeat1"));
if (!row.empty("OrReqFeat2")) {
OrReqFeat = OrReqFeat || hasFeat(row.getInt("OrReqFeat2"));
if (!row.empty("OrReqFeat3")) {
OrReqFeat = OrReqFeat || hasFeat(row.getInt("OrReqFeat3"));
if (!row.empty("OrReqFeat4"))
OrReqFeat = OrReqFeat || hasFeat(row.getInt("OrReqFeat4"));
}
}
}
if (!OrReqFeat)
return false;
}
// TODO Check base bonus attack
if (row.getInt("PreReqEpic") > 0 && _creature->getHitDice() < 21)
return false;
// Check minimun level.
if ((_creature->getClassLevel(row.getInt("MinLevelClass")) < row.getInt("MinLevel")) && !row.empty("MinLevel"))
return false;
// Check maximum level.
if ((_creature->getHitDice() >= row.getInt("MaxLevel")) && !row.empty("MaxLevel"))
return false;
// Check skill rank.
if (!row.empty("REQSKILL")) {
if (_skills[row.getInt("REQSKILL")] == 0)
return false;
if ((row.getInt("ReqSkillMinRanks") > _skills[row.getInt("REQSKILL")]) && !row.empty("ReqSkillMinRanks"))
return false;
}
if (!row.empty("REQSKILL2")) {
if (_skills[row.getInt("REQSKILL2")] == 0)
return false;
if ((row.getInt("ReqSkillMinRanks2") > _skills[row.getInt("REQSKILL2")]) && !row.empty("ReqSkillMinRanks2"))
return false;
}
// Check if the character already has the feat.
if (hasFeat(featId)) {
if (!row.getInt("GAINMULTIPLE"))
return false;
}
// TODO Check spell level
// TODO Check saving throw
return true;
}
uint32 CharGenChoices::getClass() const {
return _classId;
}
uint32 CharGenChoices::getRace() const {
return _creature->getRace();
}
bool CharGenChoices::getAlign(uint8 &goodness, uint8 &loyalty) const {
// Check if alignment has been previously set.
if (_goodness > 100)
return false;
goodness = _goodness;
loyalty = _loyalty;
return true;
}
uint8 CharGenChoices::getAbility(Ability ability) const {
return _abilities[ability];
}
uint8 CharGenChoices::getTotalAbility(Ability ability) const {
return _abilities[ability] + _racialAbilities[ability];
}
int8 CharGenChoices::getAbilityModifier(Ability ability) {
uint8 totalAbility = getTotalAbility(ability);
int8 modifier = (totalAbility - totalAbility % 2) / 2;
modifier -= 5;
return modifier;
}
uint8 CharGenChoices::getPackage() const {
return _package;
}
uint8 CharGenChoices::getSpellSchool() const {
return _spellSchool;
}
std::vector<uint32> CharGenChoices::getFeats() {
std::vector<uint32> allFeats(_racialFeats);
allFeats.insert(allFeats.end(), _classFeats.begin(), _classFeats.end());
return allFeats;
}
} // End of namespace NWN
} // End of namespace Engines