The open source repository for the action RPG game in development by Sig Productions titled 'Adventures in Lestoria'!
https://forums.lestoria.net
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
353 lines
15 KiB
353 lines
15 KiB
#pragma region License
|
|
/*
|
|
License (OLC-3)
|
|
~~~~~~~~~~~~~~~
|
|
|
|
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions or derivations of source code must retain the above copyright
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions or derivative works in binary form must reproduce the above
|
|
copyright notice. This list of conditions and the following disclaimer must be
|
|
reproduced in the documentation and/or other materials provided with the distribution.
|
|
|
|
3. Neither the name of the copyright holder nor the names of its contributors may
|
|
be used to endorse or promote products derived from this software without specific
|
|
prior written permission.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
SUCH DAMAGE.
|
|
|
|
Portions of this software are copyright © 2024 The FreeType
|
|
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
|
|
All rights reserved.
|
|
*/
|
|
#pragma endregion
|
|
|
|
#include "olcUTIL_DataFile.h"
|
|
#include "ItemEnchant.h"
|
|
#include "DEFINES.h"
|
|
#include "Class.h"
|
|
#include <unordered_set>
|
|
#include <algorithm>
|
|
#include "util.h"
|
|
#include "AdventuresInLestoria.h"
|
|
#include <ranges>
|
|
|
|
INCLUDE_game
|
|
INCLUDE_DATA
|
|
|
|
const Pixel ItemEnchantInfo::enchantAttributeCol{0x00DFE2};
|
|
std::unordered_map<std::string,ItemEnchantInfo>ItemEnchantInfo::ENCHANT_LIST;
|
|
std::unordered_map<ItemEnchantInfo::ItemEnchantCategory,ItemEnchantInfo::ItemEnchantCategoryData>ItemEnchantInfo::ENCHANT_CATEGORIES;
|
|
std::unordered_map<ItemEnchantInfo::ItemEnchantCategory,Pixel>ItemEnchantInfo::enchantTextDisplayCol;
|
|
|
|
const ItemEnchantInfo&operator ""_ENC(const char*key,std::size_t len){
|
|
return ItemEnchantInfo::GetEnchant(std::string(key));
|
|
}
|
|
|
|
const float ItemEnchantInfo::operator[](const std::string& name)const{
|
|
return config.at(name);
|
|
}
|
|
|
|
void ItemEnchantInfo::Initialize(){
|
|
ENCHANT_LIST.clear();
|
|
ENCHANT_CATEGORIES.clear();
|
|
|
|
float minCategoryRollWeight{};
|
|
float maxCategoryRollWeight{};
|
|
for(const auto&[key,size]:DATA["Item Enchants"]){
|
|
ItemEnchantCategory enchantCategory{};
|
|
if(key=="General Enchants"){
|
|
enchantCategory=ItemEnchantCategory::GENERAL;
|
|
}else
|
|
if(key=="Class Enchants"){
|
|
enchantCategory=ItemEnchantCategory::CLASS;
|
|
}else
|
|
if(key=="Unique Enchants"){
|
|
enchantCategory=ItemEnchantCategory::UNIQUE;
|
|
}else ERR(std::format("WARNING! Enchant type {} is not supported! THIS SHOULD NOT BE HAPPENING! Please check ItemEnchants.txt",key));
|
|
|
|
enchantTextDisplayCol[enchantCategory]=DATA["Item Enchants"][key]["Enchant Display Color"].GetPixel();
|
|
|
|
datafile&enchantData=DATA["Item Enchants"][key];
|
|
|
|
ItemEnchantCategoryData categoryData;
|
|
|
|
minCategoryRollWeight=maxCategoryRollWeight;
|
|
maxCategoryRollWeight+=enchantData["Percent Chance"].GetReal();
|
|
|
|
categoryData.displayCol=enchantData["Enchant Display Color"].GetPixel();
|
|
categoryData.weightedRollRange={minCategoryRollWeight,maxCategoryRollWeight};
|
|
|
|
ENCHANT_CATEGORIES.insert({enchantCategory,categoryData});
|
|
|
|
for(const auto&[key,size]:enchantData){
|
|
if(key=="Percent Chance"||key=="Enchant Display Color")continue;
|
|
|
|
const auto MakeEnchant=[enchantCategory](const std::string_view keyName,datafile&enchant){
|
|
ItemEnchantInfo newEnchant;
|
|
newEnchant.category=enchantCategory;
|
|
newEnchant.name=keyName;
|
|
std::string enchantDescription{enchant["Description"].GetString()};
|
|
using enum AbilitySlot;
|
|
const std::unordered_map<std::string,AbilitySlot>affectSlots{
|
|
{"Auto Attack",AUTO_ATTACK},
|
|
{"Right Click Ability",RIGHT_CLICK},
|
|
{"Ability 1",ABILITY_1},
|
|
{"Ability 2",ABILITY_2},
|
|
{"Ability 3",ABILITY_3},
|
|
};
|
|
|
|
if(enchant.HasProperty("Affects")){
|
|
std::string affectStr{enchant["Affects"].GetString()};
|
|
if(!affectSlots.count(affectStr))ERR(std::format("WARNING! Could not find translate ability affect slot name {} to a valid slot! Valid slot names are: \"Auto Attack, Right Click Ability, Ability 1, Ability 2, Ability 3\"",affectStr));
|
|
newEnchant.abilitySlot=affectSlots.at(affectStr);
|
|
}
|
|
|
|
auto IsRequiredKey=[](const std::string_view key){return key=="Description"||key=="Affects"||key.starts_with("Stat Modifier[");};
|
|
|
|
for(auto&[key,size]:enchant){
|
|
if(IsRequiredKey(key))continue;
|
|
const auto&result{newEnchant.config.insert({key,enchant[key].GetReal()})};
|
|
const bool InsertFailed{!result.second};
|
|
if(InsertFailed)ERR(std::format("WARNING! Enchant {} already had an extra property named {}. Duplicates not allowed!",keyName,key));
|
|
}
|
|
|
|
for(const auto&[configName,val]:newEnchant.config){
|
|
const std::string wrappedConfigStr{util::vformat("{{{}}}",configName)};
|
|
size_t configValInd{enchantDescription.find(wrappedConfigStr)};
|
|
if(configValInd==std::string::npos)continue;
|
|
std::string formattedFloat{std::format("{}{}#FFFFFF",ItemEnchantInfo::enchantAttributeCol.toHTMLColorCode(),val)};
|
|
enchantDescription=enchantDescription.replace(configValInd,wrappedConfigStr.length(),formattedFloat);
|
|
}
|
|
|
|
size_t statModifierInd{};
|
|
while(enchant.HasProperty(std::format("Stat Modifier[{}]",statModifierInd))){
|
|
const datafile&stat{enchant[std::format("Stat Modifier[{}]",statModifierInd)]};
|
|
newEnchant.minStatModifiers.A(stat.GetString(0))=stat.GetReal(1);
|
|
newEnchant.maxStatModifiers.A(stat.GetString(0))=stat.GetReal(2);
|
|
statModifierInd++;
|
|
}
|
|
|
|
newEnchant.description=enchantDescription;
|
|
|
|
return newEnchant;
|
|
};
|
|
|
|
using enum ItemEnchantCategory;
|
|
if(enchantCategory==CLASS){
|
|
Class itemEnchantClass{classutils::StringToClass(key)};
|
|
datafile&classEnchantData{enchantData[key]};
|
|
for(const auto&[key,size]:classEnchantData){
|
|
ItemEnchantInfo newEnchant{MakeEnchant(key,classEnchantData[key])};
|
|
newEnchant.abilityClass=itemEnchantClass;
|
|
const auto&result{ENCHANT_LIST.insert({key,newEnchant})};
|
|
const bool InsertFailed{!result.second};
|
|
if(InsertFailed)ERR(std::format("WARNING! Enchant {} already existed in Enchant List! Duplicates are not allowed!",key));
|
|
}
|
|
}else{
|
|
ItemEnchantInfo newEnchant{MakeEnchant(key,enchantData[key])};
|
|
const auto&result{ENCHANT_LIST.insert({key,newEnchant})};
|
|
const bool InsertFailed{!result.second};
|
|
if(InsertFailed)ERR(std::format("WARNING! Enchant {} already existed in Enchantment List! Duplicates are not allowed!",key));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ItemEnchant::ItemEnchant(const std::string_view enchantName)
|
|
:enchantName(std::string(enchantName)),description(ItemEnchantInfo::ENCHANT_LIST.at(this->enchantName).Description()){
|
|
for(const auto&[attr,val]:ItemEnchantInfo::ENCHANT_LIST.at(this->enchantName).minStatModifiers){
|
|
float minVal=ItemEnchantInfo::ENCHANT_LIST.at(this->enchantName).minStatModifiers.A_Read(attr);
|
|
float maxVal=ItemEnchantInfo::ENCHANT_LIST.at(this->enchantName).maxStatModifiers.A_Read(attr);
|
|
if(minVal==maxVal)SetAttribute(attr.ActualName(),minVal);
|
|
else{
|
|
const auto&randRange{std::ranges::iota_view(int(minVal),int(maxVal+1))};
|
|
SetAttribute(attr.ActualName(),randRange[util::random()%randRange.size()]);
|
|
}
|
|
}
|
|
}
|
|
|
|
const ItemEnchantInfo&ItemEnchantInfo::GetEnchant(const std::string_view enchantName){
|
|
return ENCHANT_LIST.at(std::string(enchantName));
|
|
}
|
|
|
|
const std::string ItemEnchantInfo::Name(TextStyle style)const{
|
|
using enum TextStyle;
|
|
switch(style){
|
|
case NORMAL:{
|
|
return name;
|
|
}break;
|
|
case COLOR_CODES:{
|
|
return ENCHANT_CATEGORIES.at(category).displayCol.toHTMLColorCode()+name;
|
|
}break;
|
|
}
|
|
return{};
|
|
}
|
|
const std::string_view ItemEnchantInfo::Description()const{
|
|
return description;
|
|
}
|
|
const float ItemEnchantInfo::GetConfigValue(const std::string_view keyName)const{
|
|
return config.at(std::string(keyName));
|
|
}
|
|
const std::optional<Class>&ItemEnchantInfo::GetClass()const{
|
|
return abilityClass;
|
|
}
|
|
const ItemEnchantInfo::ItemEnchantCategory&ItemEnchantInfo::Category()const{
|
|
return category;
|
|
}
|
|
|
|
const ItemEnchantInfo&ItemEnchant::GetEnchantInfo()const{
|
|
return ItemEnchantInfo::ENCHANT_LIST.at(enchantName);
|
|
}
|
|
const std::string ItemEnchant::Name(ItemEnchantInfo::TextStyle style)const{
|
|
return GetEnchantInfo().Name(style);
|
|
}
|
|
const std::string_view ItemEnchant::Description()const{
|
|
return description;
|
|
}
|
|
const ItemEnchantInfo::ItemEnchantCategory&ItemEnchant::Category()const{
|
|
return GetEnchantInfo().Category();
|
|
}
|
|
const std::optional<ItemEnchantInfo::AbilitySlot>&ItemEnchant::AbilitySlot()const{
|
|
return GetEnchantInfo().abilitySlot;
|
|
}
|
|
const std::unordered_map<std::string,ItemEnchantInfo>&ItemEnchantInfo::GetEnchants(){
|
|
return ENCHANT_LIST;
|
|
}
|
|
|
|
const std::vector<ItemEnchantInfo>ItemEnchant::GetAvailableEnchants(){
|
|
std::vector<ItemEnchantInfo>filteredEnchants;
|
|
|
|
std::for_each(ItemEnchantInfo::GetEnchants().begin(),ItemEnchantInfo::GetEnchants().end(),[&filteredEnchants](const std::pair<std::string,ItemEnchantInfo>&data){
|
|
using enum ItemEnchantInfo::ItemEnchantCategory;
|
|
const ItemEnchantInfo&enchant=data.second;
|
|
|
|
const auto enchantAvailableForCurrentClass=enchant.Category()!=CLASS||enchant.GetClass().has_value()&&enchant.GetClass().value()==game->GetPlayer()->GetClass();
|
|
|
|
if(enchantAvailableForCurrentClass){
|
|
filteredEnchants.emplace_back(enchant);
|
|
}
|
|
});
|
|
|
|
return filteredEnchants;
|
|
}
|
|
|
|
const ItemEnchant ItemEnchant::RollRandomEnchant(const std::optional<ItemEnchant>previousEnchant){
|
|
const std::vector<ItemEnchantInfo>filteredEnchants{GetAvailableEnchants()};
|
|
|
|
int randomRoll{int(util::random_range(0,100))};
|
|
const std::pair<int,int>generalEnchantRange{0,"Item Enchants.General Enchants.Percent Chance"_I};
|
|
const std::pair<int,int>classEnchantRange{generalEnchantRange.second,generalEnchantRange.second+"Item Enchants.Class Enchants.Percent Chance"_I};
|
|
const std::pair<int,int>uniqueEnchantRange{classEnchantRange.second,classEnchantRange.second+"Item Enchants.Unique Enchants.Percent Chance"_I};
|
|
std::vector<ItemEnchantInfo>remainingAvailableEnchants;
|
|
|
|
if(randomRoll>=generalEnchantRange.first&&randomRoll<generalEnchantRange.second){
|
|
std::copy_if(filteredEnchants.begin(),filteredEnchants.end(),std::back_inserter(remainingAvailableEnchants),[](const ItemEnchantInfo&enchant){return enchant.Category()==ItemEnchantInfo::ItemEnchantCategory::GENERAL;});
|
|
}else if(randomRoll>=classEnchantRange.first&&randomRoll<classEnchantRange.second){
|
|
std::copy_if(filteredEnchants.begin(),filteredEnchants.end(),std::back_inserter(remainingAvailableEnchants),[](const ItemEnchantInfo&enchant){return enchant.Category()==ItemEnchantInfo::ItemEnchantCategory::CLASS;});
|
|
}else if(randomRoll>=uniqueEnchantRange.first&&randomRoll<uniqueEnchantRange.second){
|
|
std::copy_if(filteredEnchants.begin(),filteredEnchants.end(),std::back_inserter(remainingAvailableEnchants),[](const ItemEnchantInfo&enchant){return enchant.Category()==ItemEnchantInfo::ItemEnchantCategory::UNIQUE;});
|
|
}else ERR(std::format("WARNING! Somehow did not pick a value in any of the given ranges. Rolled value was: {}. Ranges were {}-{}, {}-{}, {}-{}",randomRoll,generalEnchantRange.first,generalEnchantRange.second,classEnchantRange.first,classEnchantRange.second,uniqueEnchantRange.first,uniqueEnchantRange.second));
|
|
|
|
const auto AtLeastOneAttributeIncreased=[](const ItemEnchant&oldEnchant,const ItemEnchant&newEnchant){
|
|
if(oldEnchant.Name()!=newEnchant.Name())ERR(std::format("WARNING! Not intended to be used with enchants of varying names!! Old:{} New:{}",oldEnchant.Name(),newEnchant.Name()));
|
|
for(const auto&[attr,val]:oldEnchant){
|
|
if(newEnchant.GetAttribute(attr.ActualName())>val)return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
for(;;){
|
|
ItemEnchantInfo&randomEnchant{remainingAvailableEnchants[util::random()%remainingAvailableEnchants.size()]};
|
|
ItemEnchant newEnchant{randomEnchant.Name()};
|
|
if(!previousEnchant||
|
|
previousEnchant.value().Name()!=newEnchant.Name()||
|
|
AtLeastOneAttributeIncreased(previousEnchant.value(),newEnchant))return newEnchant;
|
|
}
|
|
}
|
|
|
|
void ItemEnchant::UpdateDescription(){
|
|
description=ItemEnchantInfo::ENCHANT_LIST.at(this->enchantName).Description();
|
|
for(const auto&[attr,val]:ItemEnchantInfo::ENCHANT_LIST.at(this->enchantName).minStatModifiers){
|
|
const std::string wrappedConfigStr{util::vformat("{{{}}}",attr.ActualName())};
|
|
size_t configValInd{description.find(wrappedConfigStr)};
|
|
if(configValInd==std::string::npos)continue;
|
|
std::string formattedFloat{std::format("{}{}#FFFFFF",ItemEnchantInfo::enchantAttributeCol.toHTMLColorCode(),GetAttribute(attr.ActualName()))};
|
|
description=description.replace(configValInd,wrappedConfigStr.length(),formattedFloat);
|
|
}
|
|
}
|
|
|
|
void ItemEnchant::SetAttribute(const std::string_view attr,const float val){
|
|
stats.A(attr)=val;
|
|
UpdateDescription();
|
|
}
|
|
const float&ItemEnchant::GetAttribute(const std::string_view attr)const{
|
|
return stats.A_Read(attr);
|
|
}
|
|
|
|
std::map<ItemAttribute,float>::const_iterator ItemEnchant::begin()const{
|
|
return stats.begin();
|
|
}
|
|
std::map<ItemAttribute,float>::const_iterator ItemEnchant::end()const{
|
|
return stats.end();
|
|
}
|
|
|
|
const Pixel&ItemEnchant::DisplayCol()const{
|
|
return ItemEnchantInfo::enchantTextDisplayCol.at(Category());
|
|
}
|
|
|
|
const std::optional<ItemEnchantInfo::AbilitySlot>&ItemEnchantInfo::GetAbilitySlot()const{
|
|
return abilitySlot;
|
|
}
|
|
|
|
const std::optional<Ability*>ItemEnchantInfo::GetAbility()const{
|
|
if(!GetAbilitySlot()||!GetClass())return {};
|
|
|
|
#pragma region Generate Abilities for all classes
|
|
#define GENERATE_ABILITY_LIST_FOR_CLASS(class,staticClass) \
|
|
{class,{std::optional<Ability*>{},&staticClass::rightClickAbility,&staticClass::ability1,&staticClass::ability2,&staticClass::ability3}},
|
|
#define END_ABILITY_LIST };
|
|
|
|
std::unordered_map<Class,std::array<std::optional<Ability*>,5>>enchantAbilitiesList{
|
|
GENERATE_ABILITY_LIST_FOR_CLASS(WARRIOR,Warrior)
|
|
GENERATE_ABILITY_LIST_FOR_CLASS(RANGER,Ranger)
|
|
GENERATE_ABILITY_LIST_FOR_CLASS(WIZARD,Wizard)
|
|
GENERATE_ABILITY_LIST_FOR_CLASS(THIEF,Thief)
|
|
GENERATE_ABILITY_LIST_FOR_CLASS(TRAPPER,Trapper)
|
|
GENERATE_ABILITY_LIST_FOR_CLASS(WITCH,Witch)
|
|
END_ABILITY_LIST;
|
|
#pragma endregion
|
|
|
|
return enchantAbilitiesList[*GetClass()][int(*GetAbilitySlot())];
|
|
}
|
|
|
|
const std::optional<std::reference_wrapper<Ability>>ItemEnchant::Ability()const{
|
|
if(GetEnchantInfo().GetAbility())return **GetEnchantInfo().GetAbility();
|
|
return {};
|
|
}
|
|
|
|
const Pixel&ItemEnchantInfo::DisplayCol()const{
|
|
return ItemEnchantInfo::enchantTextDisplayCol.at(Category());
|
|
}
|
|
|
|
const bool ItemEnchant::HasAttributes()const{
|
|
return stats.size()>0;
|
|
}
|
|
|
|
const std::optional<Class>&ItemEnchant::GetClass()const{
|
|
return ItemEnchantInfo::GetEnchant(Name()).GetClass();
|
|
} |