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 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
|
/*
* CCreatureHandler.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 "CCreatureHandler.h"
#include "CGeneralTextHandler.h"
#include "filesystem/Filesystem.h"
#include "VCMI_Lib.h"
#include "CGameState.h"
#include "CTownHandler.h"
#include "CModHandler.h"
#include "StringConstants.h"
#include "mapObjects/CObjectClassesHandler.h"
int CCreature::getQuantityID(const int & quantity)
{
if (quantity<5)
return 1;
if (quantity<10)
return 2;
if (quantity<20)
return 3;
if (quantity<50)
return 4;
if (quantity<100)
return 5;
if (quantity<250)
return 6;
if (quantity<500)
return 7;
if (quantity<1000)
return 8;
return 9;
}
int CCreature::estimateCreatureCount(ui32 countID)
{
static const int creature_count[] = { 0, 3, 8, 15, 35, 75, 175, 375, 750, 2500 };
if(countID > 9)
{
logGlobal->error("Wrong countID %d!", countID);
return 0;
}
else
return creature_count[countID];
}
bool CCreature::isDoubleWide() const
{
return doubleWide;
}
bool CCreature::isFlying() const
{
return hasBonusOfType(Bonus::FLYING);
}
bool CCreature::isShooting() const
{
return hasBonusOfType(Bonus::SHOOTER);
}
bool CCreature::isUndead() const
{
return hasBonusOfType(Bonus::UNDEAD);
}
/**
* Determines if the creature is of a good alignment.
* @return true if the creture is good, false otherwise.
*/
bool CCreature::isGood () const
{
return VLC->townh->factions[faction]->alignment == EAlignment::GOOD;
}
/**
* Determines if the creature is of an evil alignment.
* @return true if the creature is evil, false otherwise.
*/
bool CCreature::isEvil () const
{
return VLC->townh->factions[faction]->alignment == EAlignment::EVIL;
}
si32 CCreature::maxAmount(const std::vector<si32> &res) const //how many creatures can be bought
{
int ret = 2147483645;
int resAmnt = std::min(res.size(),cost.size());
for(int i=0;i<resAmnt;i++)
if(cost[i])
ret = std::min(ret,(int)(res[i]/cost[i]));
return ret;
}
CCreature::CCreature()
{
setNodeType(CBonusSystemNode::CREATURE);
faction = 0;
level = 0;
fightValue = AIValue = growth = hordeGrowth = ammMin = ammMax = 0;
doubleWide = false;
special = true;
iconIndex = -1;
}
void CCreature::addBonus(int val, Bonus::BonusType type, int subtype)
{
auto added = std::make_shared<Bonus>(Bonus::PERMANENT, type, Bonus::CREATURE_ABILITY, val, idNumber, subtype, Bonus::BASE_NUMBER);
addNewBonus(added);
}
bool CCreature::isMyUpgrade(const CCreature *anotherCre) const
{
//TODO upgrade of upgrade?
return vstd::contains(upgrades, anotherCre->idNumber);
}
bool CCreature::valid() const
{
return this == VLC->creh->creatures[idNumber];
}
std::string CCreature::nodeName() const
{
return "\"" + namePl + "\"";
}
bool CCreature::isItNativeTerrain(int terrain) const
{
return VLC->townh->factions[faction]->nativeTerrain == terrain;
}
void CCreature::setId(CreatureID ID)
{
idNumber = ID;
for(auto bonus : getExportedBonusList())
{
if(bonus->source == Bonus::CREATURE_ABILITY)
bonus->sid = ID;
}
CBonusSystemNode::treeHasChanged();
}
void CCreature::fillWarMachine()
{
switch (idNumber)
{
case CreatureID::CATAPULT: //Catapult
warMachine = ArtifactID::CATAPULT;
break;
case CreatureID::BALLISTA: //Ballista
warMachine = ArtifactID::BALLISTA;
break;
case CreatureID::FIRST_AID_TENT: //First Aid tent
warMachine = ArtifactID::FIRST_AID_TENT;
break;
case CreatureID::AMMO_CART: //Ammo cart
warMachine = ArtifactID::AMMO_CART;
break;
}
warMachine = ArtifactID::NONE; //this creature is not artifact
}
static void AddAbility(CCreature *cre, const JsonVector &ability_vec)
{
auto nsf = std::make_shared<Bonus>();
std::string type = ability_vec[0].String();
auto it = bonusNameMap.find(type);
if (it == bonusNameMap.end()) {
if (type == "DOUBLE_WIDE")
cre->doubleWide = true;
else if (type == "ENEMY_MORALE_DECREASING") {
cre->addBonus(-1, Bonus::MORALE);
cre->getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
}
else if (type == "ENEMY_LUCK_DECREASING") {
cre->addBonus(-1, Bonus::LUCK);
cre->getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
} else
logGlobal->error("Error: invalid ability type %s in creatures config", type);
return;
}
nsf->type = it->second;
JsonUtils::parseTypedBonusShort(ability_vec,nsf);
nsf->source = Bonus::CREATURE_ABILITY;
nsf->sid = cre->idNumber;
cre->addNewBonus(nsf);
}
CCreatureHandler::CCreatureHandler()
: expAfterUpgrade(0)
{
VLC->creh = this;
allCreatures.setDescription("All creatures");
creaturesOfLevel[0].setDescription("Creatures of unnormalized tier");
for(int i = 1; i < ARRAY_COUNT(creaturesOfLevel); i++)
creaturesOfLevel[i].setDescription("Creatures of tier " + boost::lexical_cast<std::string>(i));
loadCommanders();
}
const CCreature * CCreatureHandler::getCreature(const std::string & scope, const std::string & identifier) const
{
boost::optional<si32> index = VLC->modh->identifiers.getIdentifier(scope, "creature", identifier);
if(!index)
throw std::runtime_error("Creature not found "+identifier);
return creatures[*index];
}
void CCreatureHandler::loadCommanders()
{
JsonNode data(ResourceID("config/commanders.json"));
data.setMeta("core"); // assume that commanders are in core mod (for proper bonuses resolution)
const JsonNode & config = data; // switch to const data accessors
for (auto bonus : config["bonusPerLevel"].Vector())
{
commanderLevelPremy.push_back(JsonUtils::parseBonus(bonus.Vector()));
}
int i = 0;
for (auto skill : config["skillLevels"].Vector())
{
skillLevels.push_back (std::vector<ui8>());
for (auto skillLevel : skill["levels"].Vector())
{
skillLevels[i].push_back (skillLevel.Float());
}
++i;
}
for (auto ability : config["abilityRequirements"].Vector())
{
std::pair <std::shared_ptr<Bonus>, std::pair <ui8, ui8> > a;
a.first = JsonUtils::parseBonus (ability["ability"].Vector());
a.second.first = ability["skills"].Vector()[0].Float();
a.second.second = ability["skills"].Vector()[1].Float();
skillRequirements.push_back (a);
}
}
void CCreatureHandler::loadBonuses(JsonNode & creature, std::string bonuses)
{
auto makeBonusNode = [&](std::string type) -> JsonNode
{
JsonNode ret;
ret["type"].String() = type;
return ret;
};
static const std::map<std::string, JsonNode> abilityMap =
{
{"FLYING_ARMY", makeBonusNode("FLYING")},
{"SHOOTING_ARMY", makeBonusNode("SHOOTER")},
{"SIEGE_WEAPON", makeBonusNode("SIEGE_WEAPON")},
{"const_free_attack", makeBonusNode("BLOCKS_RETALIATION")},
{"IS_UNDEAD", makeBonusNode("UNDEAD")},
{"const_no_melee_penalty", makeBonusNode("NO_MELEE_PENALTY")},
{"const_jousting", makeBonusNode("JOUSTING")},
{"KING_1", makeBonusNode("KING1")},
{"KING_2", makeBonusNode("KING2")},
{"KING_3", makeBonusNode("KING3")},
{"const_no_wall_penalty", makeBonusNode("NO_WALL_PENALTY")},
{"CATAPULT", makeBonusNode("CATAPULT")},
{"MULTI_HEADED", makeBonusNode("ATTACKS_ALL_ADJACENT")},
{"IMMUNE_TO_MIND_SPELLS", makeBonusNode("MIND_IMMUNITY")},
{"HAS_EXTENDED_ATTACK", makeBonusNode("TWO_HEX_ATTACK_BREATH")}
};
auto hasAbility = [&](const std::string name) -> bool
{
return boost::algorithm::find_first(bonuses, name);
};
for(auto a : abilityMap)
{
if(hasAbility(a.first))
creature["abilities"][a.first] = a.second;
}
if(hasAbility("DOUBLE_WIDE"))
creature["doubleWide"].Bool() = true;
if(hasAbility("const_raises_morale"))
{
JsonNode node = makeBonusNode("MORALE");
node["val"].Float() = 1;
node["propagator"].String() = "HERO";
creature["abilities"]["const_raises_morale"] = node;
}
if(hasAbility("const_lowers_morale"))
{
JsonNode node = makeBonusNode("MORALE");
node["val"].Float() = -1;
node["effectRange"].String() = "ONLY_ENEMY_ARMY";
creature["abilities"]["const_lowers_morale"] = node;
}
}
std::vector<JsonNode> CCreatureHandler::loadLegacyData(size_t dataSize)
{
creatures.resize(dataSize);
std::vector<JsonNode> h3Data;
h3Data.reserve(dataSize);
CLegacyConfigParser parser("DATA/CRTRAITS.TXT");
parser.endLine(); // header
// this file is a bit different in some of Russian localisations:
//ENG: Singular Plural Wood ...
//RUS: Singular Plural Plural2 Wood ...
// Try to detect which version this is by header
// TODO: use 3rd name? Stand for "whose", e.g. pikemans'
size_t namesCount;
{
if ( parser.readString() != "Singular" || parser.readString() != "Plural" )
throw std::runtime_error("Incorrect format of CrTraits.txt");
if (parser.readString() == "Plural2")
namesCount = 3;
else
namesCount = 2;
parser.endLine();
}
for (size_t i=0; i<dataSize; i++)
{
//loop till non-empty line
while (parser.isNextEntryEmpty())
parser.endLine();
JsonNode data;
data["name"]["singular"].String() = parser.readString();
if (namesCount == 3)
parser.readString();
data["name"]["plural"].String() = parser.readString();
for(int v=0; v<7; ++v)
data["cost"][GameConstants::RESOURCE_NAMES[v]].Float() = parser.readNumber();
data["fightValue"].Float() = parser.readNumber();
data["aiValue"].Float() = parser.readNumber();
data["growth"].Float() = parser.readNumber();
data["horde"].Float() = parser.readNumber();
data["hitPoints"].Float() = parser.readNumber();
data["speed"].Float() = parser.readNumber();
data["attack"].Float() = parser.readNumber();
data["defense"].Float() = parser.readNumber();
data["damage"]["min"].Float() = parser.readNumber();
data["damage"]["max"].Float() = parser.readNumber();
if (float shots = parser.readNumber())
data["shots"].Float() = shots;
if (float spells = parser.readNumber())
data["spellPoints"].Float() = spells;
data["advMapAmount"]["min"].Float() = parser.readNumber();
data["advMapAmount"]["max"].Float() = parser.readNumber();
data["abilityText"].String() = parser.readString();
loadBonuses(data, parser.readString()); //Attributes
h3Data.push_back(data);
}
loadAnimationInfo(h3Data);
return h3Data;
}
void CCreatureHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
{
auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
object->setId(CreatureID(creatures.size()));
object->iconIndex = object->idNumber + 2;
creatures.push_back(object);
VLC->modh->identifiers.requestIdentifier(scope, "object", "monster", [=](si32 index)
{
JsonNode conf;
conf.setMeta(scope);
VLC->objtypeh->loadSubObject(object->identifier, conf, Obj::MONSTER, object->idNumber.num);
if (!object->advMapDef.empty())
{
JsonNode templ;
templ["animation"].String() = object->advMapDef;
VLC->objtypeh->getHandlerFor(Obj::MONSTER, object->idNumber.num)->addTemplate(templ);
}
// object does not have any templates - this is not usable object (e.g. pseudo-creature like Arrow Tower)
if (VLC->objtypeh->getHandlerFor(Obj::MONSTER, object->idNumber.num)->getTemplates().empty())
VLC->objtypeh->removeSubObject(Obj::MONSTER, object->idNumber.num);
});
registerObject(scope, "creature", name, object->idNumber);
for(auto node : data["extraNames"].Vector())
{
registerObject(scope, "creature", node.String(), object->idNumber);
}
}
void CCreatureHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
{
auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
object->setId(CreatureID(index));
object->iconIndex = object->idNumber + 2;
if(data["hasDoubleWeek"].Bool()) //
{
doubledCreatures.insert (object->idNumber); //we need to have id (or identifier) before it is inserted
}
assert(creatures[index] == nullptr); // ensure that this id was not loaded before
creatures[index] = object;
VLC->modh->identifiers.requestIdentifier(scope, "object", "monster", [=](si32 index)
{
JsonNode conf;
conf.setMeta(scope);
VLC->objtypeh->loadSubObject(object->identifier, conf, Obj::MONSTER, object->idNumber.num);
if (!object->advMapDef.empty())
{
JsonNode templ;
templ["animation"].String() = object->advMapDef;
VLC->objtypeh->getHandlerFor(Obj::MONSTER, object->idNumber.num)->addTemplate(templ);
}
// object does not have any templates - this is not usable object (e.g. pseudo-creature like Arrow Tower)
if (VLC->objtypeh->getHandlerFor(Obj::MONSTER, object->idNumber.num)->getTemplates().empty())
VLC->objtypeh->removeSubObject(Obj::MONSTER, object->idNumber.num);
});
registerObject(scope, "creature", name, object->idNumber);
for(auto & node : data["extraNames"].Vector())
{
registerObject(scope, "creature", node.String(), object->idNumber);
}
}
std::vector<bool> CCreatureHandler::getDefaultAllowed() const
{
std::vector<bool> ret;
for(const CCreature * crea : creatures)
{
ret.push_back(crea ? !crea->special : false);
}
return ret;
}
void CCreatureHandler::loadCrExpBon()
{
if (VLC->modh->modules.STACK_EXP) //reading default stack experience bonuses
{
CLegacyConfigParser parser("DATA/CREXPBON.TXT");
Bonus b; //prototype with some default properties
b.source = Bonus::STACK_EXPERIENCE;
b.duration = Bonus::PERMANENT;
b.valType = Bonus::ADDITIVE_VALUE;
b.effectRange = Bonus::NO_LIMIT;
b.additionalInfo = 0;
b.turnsRemain = 0;
BonusList bl;
parser.endLine();
parser.readString(); //ignore index
loadStackExp(b, bl, parser);
for(auto b : bl)
addBonusForAllCreatures(b); //health bonus is common for all
parser.endLine();
for (int i = 1; i < 7; ++i)
{
for (int j = 0; j < 4; ++j) //four modifiers common for tiers
{
parser.readString(); //ignore index
bl.clear();
loadStackExp(b, bl, parser);
for(auto b : bl)
addBonusForTier(i, b);
parser.endLine();
}
}
for (int j = 0; j < 4; ++j) //tier 7
{
parser.readString(); //ignore index
bl.clear();
loadStackExp(b, bl, parser);
for(auto b : bl)
{
addBonusForTier(7, b);
creaturesOfLevel[0].addNewBonus(b); //bonuses from level 7 are given to high-level creatures
}
parser.endLine();
}
do //parse everything that's left
{
auto sid = parser.readNumber(); //id = this particular creature ID
b.sid = sid;
bl.clear();
loadStackExp(b, bl, parser);
for(auto b : bl)
{
creatures[sid]->addNewBonus(b); //add directly to CCreature Node
}
}
while (parser.endLine());
//Calculate rank exp values, formula appears complicated bu no parsing needed
expRanks.resize(8);
int dif = 0;
int it = 8000; //ignore name of this variable
expRanks[0].push_back(it);
for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
{
expRanks[0].push_back(expRanks[0][j-1] + it + dif);
dif += it/5;
}
for (int i = 1; i < 8; ++i)
{
dif = 0;
it = 1000 * i;
expRanks[i].push_back(it);
for (int j = 1; j < 10; ++j)
{
expRanks[i].push_back(expRanks[i][j-1] + it + dif);
dif += it/5;
}
}
CLegacyConfigParser expBonParser("DATA/CREXPMOD.TXT");
expBonParser.endLine(); //header
maxExpPerBattle.resize(8);
for (int i = 1; i < 8; ++i)
{
expBonParser.readString(); //index
expBonParser.readString(); //float multiplier -> hardcoded
expBonParser.readString(); //ignore upgrade mod? ->hardcoded
expBonParser.readString(); //already calculated
maxExpPerBattle[i] = expBonParser.readNumber();
expRanks[i].push_back(expRanks[i].back() + expBonParser.readNumber());
expBonParser.endLine();
}
//skeleton gets exp penalty
creatures[56].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
creatures[57].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
//exp for tier >7, rank 11
expRanks[0].push_back(147000);
expAfterUpgrade = 75; //percent
maxExpPerBattle[0] = maxExpPerBattle[7];
}//end of Stack Experience
}
void CCreatureHandler::loadAnimationInfo(std::vector<JsonNode> &h3Data)
{
CLegacyConfigParser parser("DATA/CRANIM.TXT");
parser.endLine(); // header
parser.endLine();
for(int dd=0; dd<VLC->modh->settings.data["textData"]["creature"].Float(); ++dd)
{
while (parser.isNextEntryEmpty() && parser.endLine()) // skip empty lines
;
loadUnitAnimInfo(h3Data[dd]["graphics"], parser);
parser.endLine();
}
}
void CCreatureHandler::loadUnitAnimInfo(JsonNode & graphics, CLegacyConfigParser & parser)
{
graphics["timeBetweenFidgets"].Float() = parser.readNumber();
JsonNode & animationTime = graphics["animationTime"];
animationTime["walk"].Float() = parser.readNumber();
animationTime["attack"].Float() = parser.readNumber();
animationTime["flight"].Float() = parser.readNumber();
animationTime["idle"].Float() = 10.0;
JsonNode & missile = graphics["missile"];
JsonNode & offsets = missile["offset"];
offsets["upperX"].Float() = parser.readNumber();
offsets["upperY"].Float() = parser.readNumber();
offsets["middleX"].Float() = parser.readNumber();
offsets["middleY"].Float() = parser.readNumber();
offsets["lowerX"].Float() = parser.readNumber();
offsets["lowerY"].Float() = parser.readNumber();
for(int i=0; i<12; i++)
{
JsonNode entry;
entry.Float() = parser.readNumber();
missile["frameAngles"].Vector().push_back(entry);
}
graphics["troopCountLocationOffset"].Float() = parser.readNumber();
missile["attackClimaxFrame"].Float() = parser.readNumber();
// assume that creature is not a shooter and should not have whole missile field
if (missile["frameAngles"].Vector()[0].Float() == 0 &&
missile["attackClimaxFrame"].Float() == 0)
graphics.Struct().erase("missile");
}
CCreature * CCreatureHandler::loadFromJson(const JsonNode & node, const std::string & identifier)
{
auto cre = new CCreature();
const JsonNode & name = node["name"];
cre->identifier = identifier;
cre->nameSing = name["singular"].String();
cre->namePl = name["plural"].String();
cre->cost = Res::ResourceSet(node["cost"]);
cre->fightValue = node["fightValue"].Float();
cre->AIValue = node["aiValue"].Float();
cre->growth = node["growth"].Float();
cre->hordeGrowth = node["horde"].Float(); // Needed at least until configurable buildings
cre->addBonus(node["hitPoints"].Float(), Bonus::STACK_HEALTH);
cre->addBonus(node["speed"].Float(), Bonus::STACKS_SPEED);
cre->addBonus(node["attack"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
cre->addBonus(node["defense"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
cre->addBonus(node["damage"]["min"].Float(), Bonus::CREATURE_DAMAGE, 1);
cre->addBonus(node["damage"]["max"].Float(), Bonus::CREATURE_DAMAGE, 2);
assert(node["damage"]["min"].Float() <= node["damage"]["max"].Float());
cre->ammMin = node["advMapAmount"]["min"].Float();
cre->ammMax = node["advMapAmount"]["max"].Float();
assert(cre->ammMin <= cre->ammMax);
if (!node["shots"].isNull())
cre->addBonus(node["shots"].Float(), Bonus::SHOTS);
if (node["spellPoints"].isNull())
cre->addBonus(node["spellPoints"].Float(), Bonus::CASTS);
cre->doubleWide = node["doubleWide"].Bool();
loadStackExperience(cre, node["stackExperience"]);
loadJsonAnimation(cre, node["graphics"]);
loadCreatureJson(cre, node);
return cre;
}
void CCreatureHandler::loadJsonAnimation(CCreature * cre, const JsonNode & graphics)
{
cre->animation.timeBetweenFidgets = graphics["timeBetweenFidgets"].Float();
cre->animation.troopCountLocationOffset = graphics["troopCountLocationOffset"].Float();
const JsonNode & animationTime = graphics["animationTime"];
cre->animation.walkAnimationTime = animationTime["walk"].Float();
cre->animation.idleAnimationTime = animationTime["idle"].Float();
cre->animation.attackAnimationTime = animationTime["attack"].Float();
cre->animation.flightAnimationDistance = animationTime["flight"].Float(); //?
const JsonNode & missile = graphics["missile"];
const JsonNode & offsets = missile["offset"];
cre->animation.upperRightMissleOffsetX = offsets["upperX"].Float();
cre->animation.upperRightMissleOffsetY = offsets["upperY"].Float();
cre->animation.rightMissleOffsetX = offsets["middleX"].Float();
cre->animation.rightMissleOffsetY = offsets["middleY"].Float();
cre->animation.lowerRightMissleOffsetX = offsets["lowerX"].Float();
cre->animation.lowerRightMissleOffsetY = offsets["lowerY"].Float();
cre->animation.attackClimaxFrame = missile["attackClimaxFrame"].Float();
cre->animation.missleFrameAngles = missile["frameAngles"].convertTo<std::vector<double> >();
cre->advMapDef = graphics["map"].String();
cre->smallIconName = graphics["iconSmall"].String();
cre->largeIconName = graphics["iconLarge"].String();
}
void CCreatureHandler::loadCreatureJson(CCreature * creature, const JsonNode & config)
{
creature->level = config["level"].Float();
creature->animDefName = config["graphics"]["animation"].String();
//FIXME: MOD COMPATIBILITY
if (config["abilities"].getType() == JsonNode::JsonType::DATA_STRUCT)
{
for(auto &ability : config["abilities"].Struct())
{
if (!ability.second.isNull())
{
auto b = JsonUtils::parseBonus(ability.second);
b->source = Bonus::CREATURE_ABILITY;
b->duration = Bonus::PERMANENT;
creature->addNewBonus(b);
}
}
}
else
{
for(const JsonNode &ability : config["abilities"].Vector())
{
if (ability.getType() == JsonNode::JsonType::DATA_VECTOR)
{
assert(0); // should be unused now
AddAbility(creature, ability.Vector()); // used only for H3 creatures
}
else
{
auto b = JsonUtils::parseBonus(ability);
b->source = Bonus::CREATURE_ABILITY;
b->duration = Bonus::PERMANENT;
creature->addNewBonus(b);
}
}
}
VLC->modh->identifiers.requestIdentifier("faction", config["faction"], [=](si32 faction)
{
creature->faction = faction;
});
for(const JsonNode &value : config["upgrades"].Vector())
{
VLC->modh->identifiers.requestIdentifier("creature", value, [=](si32 identifier)
{
creature->upgrades.insert(CreatureID(identifier));
});
}
creature->animation.projectileImageName = config["graphics"]["missile"]["projectile"].String();
creature->special = config["special"].Bool() || config["disabled"].Bool();
const JsonNode & sounds = config["sound"];
#define GET_SOUND_VALUE(value_name) creature->sounds.value_name = sounds[#value_name].String()
GET_SOUND_VALUE(attack);
GET_SOUND_VALUE(defend);
GET_SOUND_VALUE(killed);
GET_SOUND_VALUE(move);
GET_SOUND_VALUE(shoot);
GET_SOUND_VALUE(wince);
GET_SOUND_VALUE(startMoving);
GET_SOUND_VALUE(endMoving);
#undef GET_SOUND_VALUE
}
void CCreatureHandler::loadStackExperience(CCreature * creature, const JsonNode & input)
{
for (const JsonNode &exp : input.Vector())
{
auto bonus = JsonUtils::parseBonus (exp["bonus"]);
bonus->source = Bonus::STACK_EXPERIENCE;
bonus->duration = Bonus::PERMANENT;
const JsonVector &values = exp["values"].Vector();
int lowerLimit = 1;//, upperLimit = 255;
if (values[0].getType() == JsonNode::JsonType::DATA_BOOL)
{
for (const JsonNode &val : values)
{
if (val.Bool() == true)
{
bonus->limiter = std::make_shared<RankRangeLimiter>(RankRangeLimiter(lowerLimit));
creature->addNewBonus (std::make_shared<Bonus>(*bonus)); //bonuses must be unique objects
break; //TODO: allow bonuses to turn off?
}
++lowerLimit;
}
}
else
{
int lastVal = 0;
for (const JsonNode &val : values)
{
if (val.Float() != lastVal)
{
bonus->val = val.Float() - lastVal;
bonus->limiter.reset (new RankRangeLimiter(lowerLimit));
creature->addNewBonus (std::make_shared<Bonus>(*bonus));
}
lastVal = val.Float();
++lowerLimit;
}
}
}
}
void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser & parser) //help function for parsing CREXPBON.txt
{
bool enable = false; //some bonuses are activated with values 2 or 1
std::string buf = parser.readString();
std::string mod = parser.readString();
switch (buf[0])
{
case 'H':
b.type = Bonus::STACK_HEALTH;
b.valType = Bonus::PERCENT_TO_BASE;
break;
case 'A':
b.type = Bonus::PRIMARY_SKILL;
b.subtype = PrimarySkill::ATTACK;
break;
case 'D':
b.type = Bonus::PRIMARY_SKILL;
b.subtype = PrimarySkill::DEFENSE;
break;
case 'M': //Max damage
b.type = Bonus::CREATURE_DAMAGE;
b.subtype = 2;
break;
case 'm': //Min damage
b.type = Bonus::CREATURE_DAMAGE;
b.subtype = 1;
break;
case 'S':
b.type = Bonus::STACKS_SPEED; break;
case 'O':
b.type = Bonus::SHOTS; break;
case 'b':
b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
case 'C':
b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
case 'd':
b.type = Bonus::DEFENSIVE_STANCE; break;
case 'e':
b.type = Bonus::DOUBLE_DAMAGE_CHANCE;
b.subtype = 0;
break;
case 'E':
b.type = Bonus::DEATH_STARE;
b.subtype = 0; //Gorgon
break;
case 'F':
b.type = Bonus::FEAR; break;
case 'g':
b.type = Bonus::SPELL_DAMAGE_REDUCTION;
b.subtype = -1; //all magic schools
break;
case 'P':
b.type = Bonus::CASTS; break;
case 'R':
b.type = Bonus::ADDITIONAL_RETALIATION; break;
case 'W':
b.type = Bonus::MAGIC_RESISTANCE;
b.subtype = 0; //otherwise creature window goes crazy
break;
case 'f': //on-off skill
enable = true; //sometimes format is: 2 -> 0, 1 -> 1
switch (mod[0])
{
case 'A':
b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
case 'b':
b.type = Bonus::RETURN_AFTER_STRIKE; break;
case 'B':
b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
case 'c':
b.type = Bonus::JOUSTING; break;
case 'D':
b.type = Bonus::ADDITIONAL_ATTACK; break;
case 'f':
b.type = Bonus::FEARLESS; break;
case 'F':
b.type = Bonus::FLYING; break;
case 'm':
b.type = Bonus::SELF_MORALE; break;
case 'M':
b.type = Bonus::NO_MORALE; break;
case 'p': //Mind spells
case 'P':
b.type = Bonus::MIND_IMMUNITY; break;
case 'r':
b.type = Bonus::REBIRTH; //on/off? makes sense?
b.subtype = 0;
b.val = 20; //arbitrary value
break;
case 'R':
b.type = Bonus::BLOCKS_RETALIATION; break;
case 's':
b.type = Bonus::FREE_SHOOTING; break;
case 'u':
b.type = Bonus::SPELL_RESISTANCE_AURA; break;
case 'U':
b.type = Bonus::UNDEAD; break;
default:
logGlobal->trace("Not parsed bonus %s %s", buf, mod);
return;
break;
}
break;
case 'w': //specific spell immunities, enabled/disabled
enable = true;
switch (mod[0])
{
case 'B': //Blind
b.type = Bonus::SPELL_IMMUNITY;
b.subtype = SpellID::BLIND;
b.additionalInfo = 0;//normal immunity
break;
case 'H': //Hypnotize
b.type = Bonus::SPELL_IMMUNITY;
b.subtype = SpellID::HYPNOTIZE;
b.additionalInfo = 0;//normal immunity
break;
case 'I': //Implosion
b.type = Bonus::SPELL_IMMUNITY;
b.subtype = SpellID::IMPLOSION;
b.additionalInfo = 0;//normal immunity
break;
case 'K': //Berserk
b.type = Bonus::SPELL_IMMUNITY;
b.subtype = SpellID::BERSERK;
b.additionalInfo = 0;//normal immunity
break;
case 'M': //Meteor Shower
b.type = Bonus::SPELL_IMMUNITY;
b.subtype = SpellID::METEOR_SHOWER;
b.additionalInfo = 0;//normal immunity
break;
case 'N': //dispell beneficial spells
b.type = Bonus::SPELL_IMMUNITY;
b.subtype = SpellID::DISPEL_HELPFUL_SPELLS;
b.additionalInfo = 0;//normal immunity
break;
case 'R': //Armageddon
b.type = Bonus::SPELL_IMMUNITY;
b.subtype = SpellID::ARMAGEDDON;
b.additionalInfo = 0;//normal immunity
break;
case 'S': //Slow
b.type = Bonus::SPELL_IMMUNITY;
b.subtype = SpellID::SLOW;
b.additionalInfo = 0;//normal immunity
break;
case '6':
case '7':
case '8':
case '9':
b.type = Bonus::LEVEL_SPELL_IMMUNITY;
b.val = std::atoi(mod.c_str()) - 5;
break;
case ':':
b.type = Bonus::LEVEL_SPELL_IMMUNITY;
b.val = GameConstants::SPELL_LEVELS; //in case someone adds higher level spells?
break;
case 'F':
b.type = Bonus::FIRE_IMMUNITY;
b.subtype = 1; //not positive
break;
case 'O':
b.type = Bonus::FIRE_IMMUNITY;
b.subtype = 2; //only direct damage
break;
case 'f':
b.type = Bonus::FIRE_IMMUNITY;
b.subtype = 0; //all
break;
case 'C':
b.type = Bonus::WATER_IMMUNITY;
b.subtype = 1; //not positive
break;
case 'W':
b.type = Bonus::WATER_IMMUNITY;
b.subtype = 2; //only direct damage
break;
case 'w':
b.type = Bonus::WATER_IMMUNITY;
b.subtype = 0; //all
break;
case 'E':
b.type = Bonus::EARTH_IMMUNITY;
b.subtype = 2; //only direct damage
break;
case 'e':
b.type = Bonus::EARTH_IMMUNITY;
b.subtype = 0; //all
break;
case 'A':
b.type = Bonus::AIR_IMMUNITY;
b.subtype = 2; //only direct damage
break;
case 'a':
b.type = Bonus::AIR_IMMUNITY;
b.subtype = 0; //all
break;
case 'D':
b.type = Bonus::DIRECT_DAMAGE_IMMUNITY;
break;
case '0':
b.type = Bonus::RECEPTIVE;
break;
case 'm':
b.type = Bonus::MIND_IMMUNITY;
break;
default:
logGlobal->trace("Not parsed bonus %s %s", buf, mod);
return;
}
break;
case 'i':
enable = true;
b.type = Bonus::NO_DISTANCE_PENALTY;
break;
case 'o':
enable = true;
b.type = Bonus::NO_WALL_PENALTY;
break;
case 'a':
case 'c':
case 'K':
case 'k':
b.type = Bonus::SPELL_AFTER_ATTACK;
b.subtype = stringToNumber(mod);
break;
case 'h':
b.type= Bonus::HATE;
b.subtype = stringToNumber(mod);
break;
case 'p':
case 'J':
b.type = Bonus::SPELL_BEFORE_ATTACK;
b.subtype = stringToNumber(mod);
b.additionalInfo = 3; //always expert?
break;
case 'r':
b.type = Bonus::HP_REGENERATION;
b.val = stringToNumber(mod);
break;
case 's':
b.type = Bonus::ENCHANTED;
b.subtype = stringToNumber(mod);
b.valType = Bonus::INDEPENDENT_MAX;
break;
default:
logGlobal->trace("Not parsed bonus %s %s", buf, mod);
return;
break;
}
switch (mod[0])
{
case '+':
case '=': //should we allow percent values to stack or pick highest?
b.valType = Bonus::ADDITIVE_VALUE;
break;
}
//limiters, range
si32 lastVal, curVal, lastLev = 0;
if (enable) //0 and 2 means non-active, 1 - active
{
if (b.type != Bonus::REBIRTH)
b.val = 0; //on-off ability, no value specified
curVal = parser.readNumber();// 0 level is never active
for (int i = 1; i < 11; ++i)
{
curVal = parser.readNumber();
if (curVal == 1)
{
b.limiter.reset (new RankRangeLimiter(i));
bl.push_back(std::make_shared<Bonus>(b));
break; //never turned off it seems
}
}
}
else
{
lastVal = parser.readNumber();
if (b.type == Bonus::HATE)
lastVal *= 10; //odd fix
//FIXME: value for zero level should be stored in our config files (independent of stack exp)
for (int i = 1; i < 11; ++i)
{
curVal = parser.readNumber();
if (b.type == Bonus::HATE)
curVal *= 10; //odd fix
if (curVal > lastVal) //threshold, add new bonus
{
b.val = curVal - lastVal;
lastVal = curVal;
b.limiter.reset (new RankRangeLimiter(i));
bl.push_back(std::make_shared<Bonus>(b));
lastLev = i; //start new range from here, i = previous rank
}
else if (curVal < lastVal)
{
b.val = lastVal;
b.limiter.reset (new RankRangeLimiter(lastLev, i));
}
}
}
}
int CCreatureHandler::stringToNumber(std::string & s)
{
boost::algorithm::replace_first(s,"#",""); //drop hash character
return std::atoi(s.c_str());
}
CCreatureHandler::~CCreatureHandler()
{
for(auto & creature : creatures)
creature.dellNull();
for(auto & p : skillRequirements)
p.first = nullptr;
}
CreatureID CCreatureHandler::pickRandomMonster(CRandomGenerator & rand, int tier) const
{
int r = 0;
if(tier == -1) //pick any allowed creature
{
do
{
r = (*RandomGeneratorUtil::nextItem(creatures, rand))->idNumber;
} while (VLC->creh->creatures[r] && VLC->creh->creatures[r]->special); // find first "not special" creature
}
else
{
assert(vstd::iswithin(tier, 1, 7));
std::vector<CreatureID> allowed;
for(const CBonusSystemNode *b : creaturesOfLevel[tier].getChildrenNodes())
{
assert(b->getNodeType() == CBonusSystemNode::CREATURE);
const CCreature * crea = dynamic_cast<const CCreature*>(b);
if(crea && !crea->special)
allowed.push_back(crea->idNumber);
}
if(!allowed.size())
{
logGlobal->warn("Cannot pick a random creature of tier %d!", tier);
return CreatureID::NONE;
}
return *RandomGeneratorUtil::nextItem(allowed, rand);
}
assert (r >= 0); //should always be, but it crashed
return CreatureID(r);
}
void CCreatureHandler::addBonusForTier(int tier, std::shared_ptr<Bonus> b)
{
assert(vstd::iswithin(tier, 1, 7));
creaturesOfLevel[tier].addNewBonus(b);
}
void CCreatureHandler::addBonusForAllCreatures(std::shared_ptr<Bonus> b)
{
allCreatures.addNewBonus(b);
}
void CCreatureHandler::buildBonusTreeForTiers()
{
for(CCreature *c : creatures)
{
if(vstd::isbetween(c->level, 0, ARRAY_COUNT(creaturesOfLevel)))
c->attachTo(&creaturesOfLevel[c->level]);
else
c->attachTo(&creaturesOfLevel[0]);
}
for(CBonusSystemNode &b : creaturesOfLevel)
b.attachTo(&allCreatures);
}
void CCreatureHandler::afterLoadFinalization()
{
}
void CCreatureHandler::deserializationFix()
{
buildBonusTreeForTiers();
}
|