All files / src/lib/events eventVariableMath.js

51.85% Statements 28/54
52.63% Branches 20/38
50% Functions 1/2
51.85% Lines 28/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 2231x   1x 1x 1x         1x                                                                   1x                                                                                                                                                                                                                                         1x               11x   11x 11x 11x 11x               11x 1x 10x 1x     11x 6x 1x 5x 2x   3x     5x 5x   5x               5x 5x         1x                  
const l10n = require("../helpers/l10n").default;
 
const id = "EVENT_VARIABLE_MATH";
const groups = ["EVENT_GROUP_MATH", "EVENT_GROUP_VARIABLES"];
const subGroups = {
  EVENT_GROUP_MATH: "EVENT_GROUP_VARIABLES",
  EVENT_GROUP_VARIABLES: "EVENT_GROUP_MATH",
};
 
const autoLabel = (fetchArg, input) => {
  const variable = fetchArg("vectorX");
  const op = input.operation;
  const other = input.other;
 
  let operation = "";
  if (op === "add") {
    operation = `${variable} +`;
  } else if (op === "sub") {
    operation = `${variable} -`;
  } else if (op === "mul") {
    operation = `${variable} *`;
  } else if (op === "div") {
    operation = `${variable} /`;
  } else Iif (op === "mod") {
    operation = `${variable} %`;
  }
 
  let value = fetchArg("value");
  if (other === "true") {
    value = l10n("FIELD_TRUE");
  } else if (other === "false") {
    value = l10n("FIELD_FALSE");
  } else if (other === "var") {
    value = fetchArg("vectorY");
  } else Iif (other === "rnd") {
    value = `${l10n("FIELD_RANDOM")}(${fetchArg("minValue")},${fetchArg(
      "maxValue"
    )})`;
  }
 
  return l10n("EVENT_VARIABLE_MATH_LABEL", { variable, operation, value });
};
 
const fields = [
  {
    key: "vectorX",
    label: l10n("FIELD_VARIABLE"),
    description: l10n("FIELD_VARIABLE_DESC"),
    type: "variable",
    defaultValue: "LAST_VARIABLE",
  },
  {
    key: "operation",
    label: l10n("FIELD_OPERATION"),
    description: l10n("FIELD_OPERATION_MATH_FUNC_DESC"),
    type: "select",
    options: [
      ["set", l10n("FIELD_SET_TO")],
      ["add", l10n("FIELD_ADD_VALUE")],
      ["sub", l10n("FIELD_SUB_VALUE")],
      ["mul", l10n("FIELD_MUL_VARIABLE")],
      ["div", l10n("FIELD_DIV_VARIABLE")],
      ["mod", l10n("FIELD_MOD_VARIABLE")],
    ],
    defaultValue: "set",
    width: "50%",
  },
  {
    key: "other",
    label: l10n("FIELD_VALUE"),
    description: l10n("FIELD_VALUE_MATH_FUNC_DESC"),
    type: "select",
    options: [
      ["true", l10n("FIELD_TRUE")],
      ["false", l10n("FIELD_FALSE")],
      ["var", l10n("FIELD_VARIABLE")],
      ["val", l10n("FIELD_VALUE")],
      ["rnd", l10n("FIELD_RANDOM")],
    ],
    defaultValue: "true",
    width: "50%",
  },
  {
    key: "vectorY",
    type: "variable",
    conditions: [
      {
        key: "other",
        eq: "var",
      },
    ],
    defaultValue: "LAST_VARIABLE",
  },
  {
    key: "value",
    type: "number",
    conditions: [
      {
        key: "other",
        eq: "val",
      },
    ],
    min: -32768,
    max: 32767,
    defaultValue: "0",
  },
  {
    type: "group",
    fields: [
      {
        key: "minValue",
        type: "number",
        conditions: [
          {
            key: "other",
            eq: "rnd",
          },
        ],
        min: -32768,
        max: 32767,
        label: l10n("FIELD_MIN_VALUE"),
        description: l10n("FIELD_MIN_VALUE_RAND_DESC"),
        hideFromDocs: true,
        defaultValue: "0",
        width: "50%",
      },
      {
        key: "maxValue",
        type: "number",
        conditions: [
          {
            key: "other",
            eq: "rnd",
          },
        ],
        min: -32768,
        max: 32767,
        label: l10n("FIELD_MAX_VALUE"),
        description: l10n("FIELD_MAX_VALUE_RAND_DESC"),
        hideFromDocs: true,
        defaultValue: "32767",
        width: "50%",
      },
    ],
  },
  {
    key: "clamp",
    type: "checkbox",
    label: l10n("FIELD_CLAMP"),
    hideFromDocs: true,
    conditions: [
      {
        key: "operation",
        in: ["add", "sub", "mul"],
      },
    ],
    defaultValue: false,
  },
];
 
const compile = (input, helpers) => {
  const {
    variableSetToValue,
    variableCopy,
    variableSetToRandom,
    variablesOperation,
    variableRandomOperation,
    variableValueOperation,
  } = helpers;
 
  let value = input.value || 0;
  const min = input.minValue || 0;
  const range = Math.min(32767, Math.max(0, (input.maxValue || 0) - min)) + 1;
  const operationLookup = {
    add: ".ADD",
    sub: ".SUB",
    mul: ".MUL",
    div: ".DIV",
    mod: ".MOD",
  };
 
  if (input.other === "true") {
    value = 1;
  } else if (input.other === "false") {
    value = 0;
  }
 
  if (!input.operation || input.operation === "set") {
    if (input.other === "var") {
      variableCopy(input.vectorX, input.vectorY);
    } else if (input.other === "rnd") {
      variableSetToRandom(input.vectorX, min, range);
    } else {
      variableSetToValue(input.vectorX, value);
    }
  } else {
    const operation = operationLookup[input.operation];
    Iif (input.other === "var") {
      variablesOperation(input.vectorX, operation, input.vectorY, input.clamp);
    } else Iif (input.other === "rnd") {
      variableRandomOperation(
        input.vectorX,
        operation,
        min,
        range,
        input.clamp
      );
    } else if (value !== 0) {
      variableValueOperation(input.vectorX, operation, value, input.clamp);
    }
  }
};
 
module.exports = {
  id,
  description: l10n("EVENT_VARIABLE_MATH_DESC"),
  autoLabel,
  groups,
  subGroups,
  fields,
  compile,
};