From 6a06b0fb618709380ab78029d55f046800d21f3a Mon Sep 17 00:00:00 2001 From: replaceitem <40722305+replaceitem@users.noreply.github.com> Date: Sun, 9 May 2021 20:14:05 +0200 Subject: [PATCH 1/2] Modify look (#848) * Added modify(e,'look',... * Added modify(e,'look',... * Not change yaw and pitch if vector length is 0 or straight up/down * Added to docs * Update docs/scarpet/api/Entities.md Co-authored-by: gnembon <41132274+gnembon@users.noreply.github.com> --- docs/scarpet/api/Entities.md | 6 ++++++ .../java/carpet/script/value/EntityValue.java | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/scarpet/api/Entities.md b/docs/scarpet/api/Entities.md index 9d8c4473..0c38a2ca 100644 --- a/docs/scarpet/api/Entities.md +++ b/docs/scarpet/api/Entities.md @@ -589,6 +589,12 @@ Changes entity's location in the specified direction. Changes entity's pitch or yaw angle. +### `modify(e, 'look', x, y, z), modify(e, 'look', [x,y,z] )` + +Sets entity's 3d vector where the entity is looking. +For cases where the vector has a length of 0, yaw and pitch won't get changed. +When pointing straight up or down, yaw will stay the same. + ### `modify(e, 'head_yaw', angle)`, `modify(e, 'body_yaw', angle)` For living entities, controls their head and body yaw angle. diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 0bce0ae6..bf125311 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -1034,7 +1034,22 @@ public class EntityValue extends Value updatePosition(e, e.getX(), e.getY(), e.getZ(), e.yaw, MathHelper.clamp((float)NumericValue.asNumber(v).getDouble(), -90, 90)); }); - //"look" + put("look", (e, v) -> { + if (!(v instanceof ListValue)) throw new InternalExpressionException("Expected a list of 3 parameters as a second argument"); + List vec = ((ListValue)v).getItems(); + float x = NumericValue.asNumber(vec.get(0)).getFloat(); + float y = NumericValue.asNumber(vec.get(1)).getFloat(); + float z = NumericValue.asNumber(vec.get(2)).getFloat(); + float l = MathHelper.sqrt(x*x + y*y + z*z); + if(l==0) return; + x /= l; + y /= l; + z /= l; + float pitch = (float) -Math.asin(y) / 0.017453292F; + float yaw = (float) (x==0 && z==0 ? e.yaw : MathHelper.atan2(-x,z) / 0.017453292F); + updatePosition(e, e.getX(), e.getY(), e.getZ(), yaw, pitch); + }); + //"turn" //"nod" From 451b35b6c5b13ecfa4f5886132794c3ee94bf2dc Mon Sep 17 00:00:00 2001 From: gnembon Date: Mon, 10 May 2021 00:49:04 -0300 Subject: [PATCH 2/2] % is a proper modulus operator, not reminder. #fixes 842, #fixes 843 --- docs/scarpet/language/Operators.md | 13 ++++++++++--- src/main/java/carpet/script/language/Operators.java | 2 +- src/main/java/carpet/script/value/NumericValue.java | 10 ++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/scarpet/language/Operators.md b/docs/scarpet/language/Operators.md index b403d4d7..00e19403 100644 --- a/docs/scarpet/language/Operators.md +++ b/docs/scarpet/language/Operators.md @@ -143,6 +143,9 @@ in the output map. Functional forms of `-` and `/` have less intuitive multi-nary interpretation, but they might be useful in some situations. `x-y-z` resolves to `difference(x, y, z)`. +`/` always produces a properly accurate result, fully reversible with `*` operator. To obtain a integer 'div' result, use +`floor(x/y)`. + Examples:
@@ -160,11 +163,15 @@ b = [100,63,100]; b+[10,0,10]  => [110,63,110]
 
 ### Just Operators `%`, `^`
 
-The modulo and exponent (power) operators work only if both operands are numbers
+The modulo and exponent (power) operators work only if both operands are numbers. `%` is a proper (and useful) 'modulus' operator,
+not a useless 'reminder' operator that you would expect from anything that touches Java. While typically modulus is reserved
+to integer numbers, scarpet expands them to floats with as much sense as possible.
 
 
pi^pi%euler  => 1.124....
--9 % 4  => -1
-9 % -4  => 0 ¯\_(ツ)_/¯ Java
+-9 % 4  => 3
+9 % -4  => -3
+9.1 % -4.2  => -3.5
+9.1 % 4.2  => 0.7
 -3 ^ 2  => 9
 -3 ^ pi => // Error
 
diff --git a/src/main/java/carpet/script/language/Operators.java b/src/main/java/carpet/script/language/Operators.java index f0e12bcd..6f18f456 100644 --- a/src/main/java/carpet/script/language/Operators.java +++ b/src/main/java/carpet/script/language/Operators.java @@ -80,7 +80,7 @@ public class Operators { expression.addFunctionalEquivalence("/", "quotient"); expression.addBinaryOperator("%", precedence.get("multiplication*/%"), true, (v1, v2) -> - new NumericValue(NumericValue.asNumber(v1).getDouble() % NumericValue.asNumber(v2).getDouble())); + NumericValue.asNumber(v1).mod(NumericValue.asNumber(v2))); expression.addBinaryOperator("^", precedence.get("exponent^"), false, (v1, v2) -> new NumericValue(java.lang.Math.pow(NumericValue.asNumber(v1).getDouble(), NumericValue.asNumber(v2).getDouble()))); diff --git a/src/main/java/carpet/script/value/NumericValue.java b/src/main/java/carpet/script/value/NumericValue.java index 00a506f4..27830682 100644 --- a/src/main/java/carpet/script/value/NumericValue.java +++ b/src/main/java/carpet/script/value/NumericValue.java @@ -313,4 +313,14 @@ public class NumericValue extends Value { return longValue!= null || getDouble() == (double)getLong(); } + + public Value mod(NumericValue n2) + { + if (this.longValue != null && n2.longValue != null) + return new NumericValue(Math.floorMod(longValue, n2.longValue)); + double x = value; + double y = n2.value; + if (y == 0) throw new ArithmeticException("Division by zero"); + return new NumericValue( x - Math.floor(x / y) * y); + } }