Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add math operator #3316

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions proto/apl.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ message APLValue {
APLValueOr or = 3;
APLValueNot not = 4;
APLValueCompare cmp = 5;
APLValueMath math = 38;

// Encounter values
APLValueCurrentTime current_time = 7;
Expand Down Expand Up @@ -175,6 +176,19 @@ message APLValueCompare {
APLValue lhs = 2;
APLValue rhs = 3;
}
message APLValueMath {
enum MathOperator {
OpUnknown = 0;
OpAdd = 1; // Add
OpSub = 2; // Subtract
OpMul = 3; // Multiply
OpDiv = 4; // Divide
}
MathOperator op = 1;

APLValue lhs = 2;
APLValue rhs = 3;
}

message APLValueCurrentTime {}
message APLValueCurrentTimePercent {}
Expand Down
2 changes: 2 additions & 0 deletions sim/core/apl_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (rot *APLRotation) newAPLValue(config *proto.APLValue) APLValue {
return rot.newValueNot(config.GetNot())
case *proto.APLValue_Cmp:
return rot.newValueCompare(config.GetCmp())
case *proto.APLValue_Math:
return rot.newValueMath(config.GetMath())

// Encounter
case *proto.APLValue_CurrentTime:
Expand Down
94 changes: 94 additions & 0 deletions sim/core/apl_values_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,100 @@ func (value *APLValueCompare) GetBool(sim *Simulation) bool {
return false
}

type APLValueMath struct {
defaultAPLValueImpl
op proto.APLValueMath_MathOperator
lhs APLValue
rhs APLValue
}

func (rot *APLRotation) newValueMath(config *proto.APLValueMath) APLValue {
lhs, rhs := rot.newAPLValue(config.Lhs), rot.newAPLValue(config.Rhs)
if lhs == nil || rhs == nil {
return nil
}

if lhs.Type() == proto.APLValueType_ValueTypeBool || rhs.Type() == proto.APLValueType_ValueTypeBool {
rot.validationWarning("Bool types not allowed in Math Operations!")
return nil
}

if lhs.Type() == proto.APLValueType_ValueTypeString || rhs.Type() == proto.APLValueType_ValueTypeString {
rot.validationWarning("String types not allowed in Math Operations!")
return nil
}

return &APLValueMath{
op: config.Op,
lhs: lhs,
rhs: rhs,
}
}
func (value *APLValueMath) Type() proto.APLValueType {
return value.lhs.Type()
}
func (value *APLValueMath) GetInt(sim *Simulation) int32 {
switch value.op {
case proto.APLValueMath_OpAdd:
return value.lhs.GetInt(sim) + value.rhs.GetInt(sim)
case proto.APLValueMath_OpSub:
return value.lhs.GetInt(sim) - value.rhs.GetInt(sim)
case proto.APLValueMath_OpMul:
return value.lhs.GetInt(sim) * value.rhs.GetInt(sim)
case proto.APLValueMath_OpDiv:
return value.lhs.GetInt(sim) / value.rhs.GetInt(sim)
}
return 0
}
func (value *APLValueMath) GetFloat(sim *Simulation) float64 {
switch value.op {
case proto.APLValueMath_OpAdd:
return value.lhs.GetFloat(sim) + value.rhs.GetFloat(sim)
case proto.APLValueMath_OpSub:
return value.lhs.GetFloat(sim) - value.rhs.GetFloat(sim)
case proto.APLValueMath_OpMul:
return value.lhs.GetFloat(sim) * value.rhs.GetFloat(sim)
case proto.APLValueMath_OpDiv:
return value.lhs.GetFloat(sim) / value.rhs.GetFloat(sim)
}
return 0
}
func (value *APLValueMath) GetDuration(sim *Simulation) time.Duration {
switch value.op {
case proto.APLValueMath_OpAdd:
return value.lhs.GetDuration(sim) + value.rhs.GetDuration(sim)
case proto.APLValueMath_OpSub:
return value.lhs.GetDuration(sim) - value.rhs.GetDuration(sim)
case proto.APLValueMath_OpMul:
left := value.lhs.GetDuration(sim)
right := value.rhs.GetDuration(sim)

switch value.lhs.Type() {
case proto.APLValueType_ValueTypeInt:
left = time.Duration(value.lhs.GetInt(sim))
case proto.APLValueType_ValueTypeFloat:
left = time.Duration(value.lhs.GetFloat(sim))
}

switch value.rhs.Type() {
case proto.APLValueType_ValueTypeInt:
right = time.Duration(value.rhs.GetInt(sim))
case proto.APLValueType_ValueTypeFloat:
right = time.Duration(value.rhs.GetFloat(sim))
}
return left * right
case proto.APLValueMath_OpDiv:
divider := value.rhs.GetDuration(sim)
if value.rhs.Type() == proto.APLValueType_ValueTypeFloat {
divider = time.Duration(value.rhs.GetFloat(sim))
} else if value.rhs.Type() == proto.APLValueType_ValueTypeInt {
divider = time.Duration(value.rhs.GetInt(sim))
}
return value.lhs.GetDuration(sim) / divider
}
return 0
}

type APLValueAnd struct {
defaultAPLValueImpl
vals []APLValue
Expand Down
31 changes: 31 additions & 0 deletions ui/core/components/individual_sim_ui/apl_values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
APLValueNot,
APLValueCompare,
APLValueCompare_ComparisonOperator as ComparisonOperator,
APLValueMath_MathOperator as MathOperator,
APLValueConst,
APLValueCurrentTime,
APLValueCurrentTimePercent,
Expand Down Expand Up @@ -38,6 +39,7 @@ import {
APLValueCurrentNonDeathRuneCount,
APLValueSpellTravelTime,
APLValueSpellChannelTime,
APLValueMath,
} from '../../proto/apl.js';

import { EventID, TypedEvent } from '../../typed_event.js';
Expand Down Expand Up @@ -244,6 +246,24 @@ function comparisonOperatorFieldConfig(field: string): AplHelpers.APLPickerBuild
};
}

function mathOperatorFieldConfig(field: string): AplHelpers.APLPickerBuilderFieldConfig<any, any> {
return {
field: field,
newValue: () => MathOperator.OpAdd,
factory: (parent, player, config) => new TextDropdownPicker(parent, player, {
...config,
defaultLabel: 'None',
equals: (a, b) => a == b,
values: [
{ value: MathOperator.OpAdd, label: '+' },
{ value: MathOperator.OpSub, label: '-' },
{ value: MathOperator.OpMul, label: '*' },
{ value: MathOperator.OpDiv, label: '/' },
],
}),
};
}

export function valueFieldConfig(field: string, options?: Partial<AplHelpers.APLPickerBuilderFieldConfig<any, any>>): AplHelpers.APLPickerBuilderFieldConfig<any, any> {
return {
field: field,
Expand Down Expand Up @@ -323,6 +343,17 @@ const valueTypeFactories: Record<NonNullable<APLValueType>, ValueTypeConfig<any>
valueFieldConfig('rhs'),
],
}),
['math']: inputBuilder({
label: 'Math',
submenu: ['Logic'],
shortDescription: 'Do basic math on two values.',
newValue: APLValueMath.create,
fields: [
valueFieldConfig('lhs'),
mathOperatorFieldConfig('op'),
valueFieldConfig('rhs'),
],
}),
['and']: inputBuilder({
label: 'All of',
submenu: ['Logic'],
Expand Down