void URPGAttributeComponent::ModifyAttribute(FModdableAttributes AttributeMod, TEnumAsByte<EAttributeOperation> OperationType) { float AttributeValue = 0; AttributeValue = GetNumericValue(AttributeMod.AttributeName); switch (OperationType) { case EAttributeOperation::Attribute_Add: AttributeValue += AttributeMod.ModValue; SetNumericValue(AttributeValue, AttributeMod.AttributeName); return; case EAttributeOperation::Attribute_Subtract: AttributeValue -= AttributeMod.ModValue; SetNumericValue(AttributeValue, AttributeMod.AttributeName); return; case EAttributeOperation::Attribute_Multiply: AttributeValue *= AttributeMod.ModValue; SetNumericValue(AttributeValue, AttributeMod.AttributeName); return; case EAttributeOperation::Attribute_Divide: AttributeValue = (AttributeValue / AttributeMod.ModValue); SetNumericValue(AttributeValue, AttributeMod.AttributeName); return; case EAttributeOperation::Attribute_Set: AttributeValue = AttributeMod.ModValue; SetNumericValue(AttributeValue, AttributeMod.AttributeName); return; default: return; } }
void URPGAttributeComponent::ChangeAttribute(FName AttributeName, float ModValue, TEnumAsByte<EAttributeOperation> OperationType) { float AttributeValue = GetNumericValue(AttributeName); switch (OperationType) { case EAttributeOperation::Attribute_Add: AttributeValue += ModValue; SetNumericValue(AttributeValue, AttributeName); return; case EAttributeOperation::Attribute_Subtract: AttributeValue -= ModValue; SetNumericValue(AttributeValue, AttributeName); return; case EAttributeOperation::Attribute_Multiply: AttributeValue *= ModValue; SetNumericValue(AttributeValue, AttributeName); return; case EAttributeOperation::Attribute_Divide: AttributeValue = (AttributeValue / ModValue); SetNumericValue(AttributeValue, AttributeName); return; case EAttributeOperation::Attribute_Set: AttributeValue = ModValue; SetNumericValue(AttributeValue, AttributeName); return; default: return; } }
bool OptionsList::SetNumericValueIfUnset(const std::string& tag, Number value, bool allow_clobber, /* = true */ bool dont_print /* = false */) { Number val; bool found = GetNumericValue(tag, val, ""); if (!found) { return SetNumericValue(tag, value, allow_clobber, dont_print); } return true; }
void URPGAttributeComponent::ModifyAttributeList(TArray<FModdableAttributes> Attributes, TEnumAsByte<EAttributeOperation> OperationType) { if (Attributes.Num() > 0) { for (int32 Index = 0; Index < Attributes.Num(); Index++) { float AttributeValue = 0; AttributeValue = GetNumericValue(Attributes[Index].AttributeName); switch (OperationType) { case EAttributeOperation::Attribute_Add: AttributeValue += Attributes[Index].ModValue; SetNumericValue(AttributeValue, Attributes[Index].AttributeName); break; case EAttributeOperation::Attribute_Subtract: AttributeValue -= Attributes[Index].ModValue; SetNumericValue(AttributeValue, Attributes[Index].AttributeName); break; case EAttributeOperation::Attribute_Multiply: AttributeValue *= Attributes[Index].ModValue; SetNumericValue(AttributeValue, Attributes[Index].AttributeName); break; case EAttributeOperation::Attribute_Divide: AttributeValue = (AttributeValue / Attributes[Index].ModValue); SetNumericValue(AttributeValue, Attributes[Index].AttributeName); break; case EAttributeOperation::Attribute_Set: AttributeValue = Attributes[Index].ModValue; SetNumericValue(AttributeValue, Attributes[Index].AttributeName); break; default: return; } } } }
bool OptionsList::ReadFromStream(const Journalist& jnlst, std::istream& is) { jnlst.Printf(J_DETAILED, J_MAIN, "Start reading options from stream.\n"); while (true) { std::string tag; std::string value; if (!readnexttoken(is, tag)) { // That's it - end of file reached. jnlst.Printf(J_DETAILED, J_MAIN, "Finished reading options from file.\n"); return true; } if (!readnexttoken(is, value)) { // Can't read value for a given tag jnlst.Printf(J_ERROR, J_MAIN, "Error reading value for tag %s from file.\n", tag.c_str()); return false; } // Now add the value for the options list jnlst.Printf(J_DETAILED, J_MAIN, "Adding option \"%s\" with value \"%s\" to OptionsList.\n", tag.c_str(), value.c_str()); if (IsValid(reg_options_)) { SmartPtr<const RegisteredOption> option = reg_options_->GetOption(tag); if (IsNull(option)) { std::string msg = "Read Option: "; msg += tag; msg += ". It is not a valid option. Check the list of available options."; THROW_EXCEPTION(OPTION_INVALID, msg); } if (option->Type() == OT_String) { bool result = SetStringValue(tag, value, false); ASSERT_EXCEPTION(result, OPTION_INVALID, "Error setting string value read from option file."); } else if (option->Type() == OT_Number) { char* p_end; Number retval = strtod(value.c_str(), &p_end); if (*p_end!='\0' && !isspace(*p_end)) { std::string msg = "Option \"" + tag + "\": Double value expected, but non-numeric option value \"" + value + "\" found.\n"; THROW_EXCEPTION(OPTION_INVALID, msg); } bool result = SetNumericValue(tag, retval, false); ASSERT_EXCEPTION(result, OPTION_INVALID, "Error setting numeric value read from file."); } else if (option->Type() == OT_Integer) { char* p_end; Index retval = strtol(value.c_str(), &p_end, 10); if (*p_end!='\0' && !isspace(*p_end)) { std::string msg = "Option \"" + tag + "\": Integer value expected, but non-integer option value \"" + value + "\" found.\n"; if (IsValid(jnlst_)) { option->OutputDescription(*jnlst_); } THROW_EXCEPTION(OPTION_INVALID, msg); } bool result = SetIntegerValue(tag, retval, false); ASSERT_EXCEPTION(result, OPTION_INVALID, "Error setting integer value read from option file."); } else { DBG_ASSERT(false && "Option Type: Unknown"); } } else { bool result = SetStringValue(tag, value, false); ASSERT_EXCEPTION(result, OPTION_INVALID, "Error setting value read from option file."); } } }
bool OptionsList::ReadFromStream(const Journalist& jnlst, std::istream& is) { jnlst.Printf(J_DETAILED, J_MAIN, "Start reading options from stream.\n"); while (true) { std::string tag; std::string value; if (!readnexttoken(is, tag)) { // That's it - end of file reached. jnlst.Printf(J_DETAILED, J_MAIN, "Finished reading options from file.\n"); return true; } if (!readnexttoken(is, value)) { // Can't read value for a given tag jnlst.Printf(J_ERROR, J_MAIN, "Error reading value for tag %s from file.\n", tag.c_str()); return false; } // Now add the value for the options list jnlst.Printf(J_DETAILED, J_MAIN, "Adding option \"%s\" with value \"%s\" to OptionsList.\n", tag.c_str(), value.c_str()); if (IsValid(reg_options_)) { SmartPtr<const RegisteredOption> option = reg_options_->GetOption(tag); if (IsNull(option)) { std::string msg = "Read Option: \""; msg += tag; msg += "\". It is not a valid option. Check the list of available options."; THROW_EXCEPTION(OPTION_INVALID, msg); } if (option->Type() == OT_String) { bool result = SetStringValue(tag, value, false); ASSERT_EXCEPTION(result, OPTION_INVALID, "Error setting string value read from option file."); } else if (option->Type() == OT_Number) { // Some people like to use 'd' instead of 'e' in floating // point numbers. Therefore, we change a 'd' to an 'e' char* buffer = new char[value.length()+1]; strcpy(buffer, value.c_str()); for (int i=0; i<(int)value.length(); ++i) { if (buffer[i]=='d' || buffer[i]=='D') { buffer[i] = 'e'; } } char* p_end; Number retval = strtod(buffer, &p_end); if (*p_end!='\0' && !isspace(*p_end)) { delete [] buffer; std::string msg = "Option \"" + tag + "\": Double value expected, but non-numeric option value \"" + value + "\" found.\n"; THROW_EXCEPTION(OPTION_INVALID, msg); } delete [] buffer; bool result = SetNumericValue(tag, retval, false); ASSERT_EXCEPTION(result, OPTION_INVALID, "Error setting numeric value read from file."); } else if (option->Type() == OT_Integer) { char* p_end; size_t retval = strtol(value.c_str(), &p_end, 10); if (*p_end!='\0' && !isspace(*p_end)) { std::string msg = "Option \"" + tag + "\": Integer value expected, but non-integer option value \"" + value + "\" found.\n"; if (IsValid(jnlst_)) { option->OutputDescription(*jnlst_); } THROW_EXCEPTION(OPTION_INVALID, msg); } bool result = SetIntegerValue(tag, static_cast<Index>(retval), false); ASSERT_EXCEPTION(result, OPTION_INVALID, "Error setting integer value read from option file."); } else { DBG_ASSERT(false && "Option Type: Unknown"); } } else { bool result = SetStringValue(tag, value, false); ASSERT_EXCEPTION(result, OPTION_INVALID, "Error setting value read from option file."); } } }