script engine work

This commit is contained in:
austin 2025-05-16 14:09:37 -04:00
parent cae96992c0
commit f73f110aa8
14 changed files with 103 additions and 80 deletions

View File

@ -1,6 +1,7 @@
import { clientUIButtonHook } from "/Scripts/client/uihooks";
import { Engine } from "/Scripts/types/engine";
import { Hook } from "/Scripts/types/hook";
import { AreaSelection } from "/Scripts/types/host/client/client-area-utils";
/**
* The client-wide hooks
@ -69,7 +70,12 @@ export const clientHooks: Hook[] = [
case 'SelectFurniture': {
} break;
case 'SelectRoom': {
engine.classes.areaUtils.static.selectAreaRectangular()
const areaSelection: AreaSelection = engine.classes.areaUtils.static.selectAreaRectangular()
console.log(JSON.stringify(areaSelection))
console.log(areaSelection.getType())
console.log(JSON.stringify(areaSelection.getType()))
console.log(areaSelection.getRectStart())
console.log(JSON.stringify(areaSelection.getRectStart()))
} break;
case 'ShowFurniture': {
} break;

View File

@ -1,3 +1,20 @@
import { Vector } from "/Scripts/types/spatial";
/**
* An area selection
*/
export interface AreaSelection {
getType: () => string,
/**
* Gets the start rectangle of the area
*/
getRectStart: () => Vector,
/**
* Gets the end rectangle of the area
*/
getRectEnd: () => Vector,
}
/**
* Utilities for managing areas on the client
*/
@ -6,6 +23,6 @@ export interface ClientAreaUtils {
/**
* Selects a rectangular area
*/
readonly selectAreaRectangular: () => void
readonly selectAreaRectangular: () => AreaSelection
}

View File

@ -1,3 +1,3 @@
#maven.buildNumber.plugin properties file
#Thu May 15 17:55:57 EDT 2025
buildNumber=624
#Fri May 16 13:50:39 EDT 2025
buildNumber=625

View File

@ -1847,6 +1847,7 @@ Script recompilation work
HTML-defined buttons now directly eval in the js context instead of going through hook manager
Fix caching with deleted source files
Proof of concept of ui button calling engine code
Script engine direct access to joml vectors

View File

@ -13,7 +13,7 @@
<joml.version>1.9.19</joml.version>
<recast.version>1.5.7</recast.version>
<imgui.version>1.86.11</imgui.version>
<graalvm.version>23.1.3</graalvm.version>
<graalvm.version>24.2.1</graalvm.version>
<junit.version>5.10.3</junit.version>
</properties>

View File

@ -1,5 +1,6 @@
package electrosphere.client.interact.select;
import org.graalvm.polyglot.HostAccess.Export;
import org.joml.Vector3d;
import org.joml.Vector3i;
@ -354,6 +355,7 @@ public class AreaSelection {
* Gets the start point of the rectangular selection
* @return The start point
*/
@Export
public Vector3d getRectStart() {
return rectStart;
}
@ -362,9 +364,9 @@ public class AreaSelection {
* Gets the end point of the rectangular selection
* @return The end point
*/
@Export
public Vector3d getRectEnd() {
return rectEnd;
}
}

View File

@ -19,7 +19,7 @@ public class ScriptClientAreaUtils {
* Tries to select a rectangular area
*/
@Export
public static void selectAreaRectangular(){
public static AreaSelection selectAreaRectangular(){
// Vector3d blockCursorPos = Globals.cursorState.getBlockCursorPos();
Vector3d cursorPos = new Vector3d(EntityUtils.getPosition(Globals.cursorState.playerCursor));
Vector3i chunkPos = Globals.clientState.clientWorldData.convertRealToWorldSpace(cursorPos);
@ -27,6 +27,7 @@ public class ScriptClientAreaUtils {
AreaSelection selection = AreaSelection.selectRectangularBlockCavity(chunkPos, blockPos, AreaSelection.DEFAULT_SELECTION_RADIUS);
Globals.cursorState.selectRectangularArea(selection);
CursorState.makeAreaVisible();
return selection;
}
}

View File

@ -26,7 +26,6 @@ import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree;
import electrosphere.net.synchronization.enums.BehaviorTreeIdEnums;
import electrosphere.net.synchronization.enums.FieldIdEnums;
import electrosphere.renderer.anim.Animation;
import electrosphere.script.utils.AccessTransforms;
import electrosphere.server.datacell.utils.DataCellSearchUtils;
import electrosphere.server.utils.ServerScriptUtils;
import electrosphere.util.math.SpatialMathUtils;
@ -306,7 +305,7 @@ public class ServerEditorMovementTree implements BehaviorTree {
);
//tell script engine we moved
ServerScriptUtils.fireSignalOnEntity(parent, "entityGroundMove", AccessTransforms.getVector(position));
ServerScriptUtils.fireSignalOnEntity(parent, "entityGroundMove", position);
} break;
case SLOWDOWN: {
CreatureUtils.setFacingVector(parent, facingVector);

View File

@ -30,7 +30,6 @@ import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree;
import electrosphere.net.synchronization.enums.BehaviorTreeIdEnums;
import electrosphere.net.synchronization.enums.FieldIdEnums;
import electrosphere.renderer.anim.Animation;
import electrosphere.script.utils.AccessTransforms;
import electrosphere.server.ai.AI;
import electrosphere.server.datacell.utils.DataCellSearchUtils;
import electrosphere.server.utils.ServerScriptUtils;
@ -359,7 +358,7 @@ public class ServerGroundMovementTree implements BehaviorTree {
);
//tell script engine we moved
ServerScriptUtils.fireSignalOnEntity(parent, "entityGroundMove", AccessTransforms.getVector(position));
ServerScriptUtils.fireSignalOnEntity(parent, "entityGroundMove", position);
} break;
case SLOWDOWN: {
CreatureUtils.setFacingVector(parent, facingVector);

View File

@ -3,6 +3,8 @@ package electrosphere.script;
import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
@ -19,6 +21,7 @@ import org.graalvm.polyglot.Value;
import electrosphere.engine.Globals;
import electrosphere.logger.LoggerInterface;
import electrosphere.script.access.FieldEnumerator;
import electrosphere.util.FileUtils;
/**
@ -96,9 +99,15 @@ public class ScriptContext {
.build();
//Create the rules for guest accessing the host environment
HostAccess accessRules = HostAccess.newBuilder(HostAccess.EXPLICIT)
.allowArrayAccess(true)
.build();
HostAccess.Builder builder = HostAccess.newBuilder(HostAccess.EXPLICIT);
builder.allowArrayAccess(true);
for(Field field : FieldEnumerator.getFields()){
builder.allowAccess(field);
}
for(Method method : FieldEnumerator.getMethods()){
builder.allowAccess(method);
}
HostAccess accessRules = builder.build();
//create context
context = Context.newBuilder("js")

View File

@ -0,0 +1,47 @@
package electrosphere.script.access;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import org.joml.Vector3d;
/**
* Enumerates the fields to allow access to
*/
public class FieldEnumerator {
/**
* Gets the list of fields to allow to script context
* @return The list of fields
*/
public static List<Field> getFields(){
List<Field> rVal = new LinkedList<Field>();
try {
rVal.add(Vector3d.class.getField("x"));
rVal.add(Vector3d.class.getField("y"));
rVal.add(Vector3d.class.getField("z"));
} catch (NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
return rVal;
}
/**
* Gets the list of methods to allow to script context
* @return The list of methods
*/
public static List<Method> getMethods(){
List<Method> rVal = new LinkedList<Method>();
try {
rVal.add(Vector3d.class.getMethod("x"));
rVal.add(Vector3d.class.getMethod("y"));
rVal.add(Vector3d.class.getMethod("z"));
} catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
return rVal;
}
}

View File

@ -1,31 +0,0 @@
package electrosphere.script.access;
import org.graalvm.polyglot.HostAccess.Export;
/**
* A script's view of a 3d vector
*/
public class Vector {
@Export
public double x;
@Export
public double y;
@Export
public double z;
/**
* Creates a vector
* @param x the x coordinate
* @param y the y coordinate
* @param z the z coordinate
*/
public Vector(double x, double y, double z){
this.x = x;
this.y = y;
this.z = z;
}
}

View File

@ -1,11 +1,11 @@
package electrosphere.script.translation;
import org.joml.Vector3d;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.ServerEntityUtils;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.script.access.Vector;
import electrosphere.script.utils.AccessTransforms;
/**
* Server utilities provided to the js context
@ -16,9 +16,9 @@ public class JSServerUtils {
* Spawns a creature
* @param creatureType The creature
*/
public static void spawnCreature(int sceneInstanceId, String creatureType, Vector position){
public static void spawnCreature(int sceneInstanceId, String creatureType, Vector3d position){
//TODO: find realm from scene id
CreatureUtils.serverSpawnBasicCreature(null, AccessTransforms.getVector(position), creatureType, null);
CreatureUtils.serverSpawnBasicCreature(null, position, creatureType, null);
}
/**
@ -26,8 +26,8 @@ public class JSServerUtils {
* @param entity The entity
* @return The position of the entity
*/
public static Vector getPosition(Entity entity){
return AccessTransforms.getVector(EntityUtils.getPosition(entity));
public static Vector3d getPosition(Entity entity){
return EntityUtils.getPosition(entity);
}
/**
@ -35,8 +35,8 @@ public class JSServerUtils {
* @param entity The entity
* @param vector THe new position of the entity
*/
public static void setPosition(Entity entity, Vector vector){
ServerEntityUtils.repositionEntity(entity, AccessTransforms.getVector(vector));
public static void setPosition(Entity entity, Vector3d vector){
ServerEntityUtils.repositionEntity(entity, vector);
}
}

View File

@ -1,27 +0,0 @@
package electrosphere.script.utils;
/**
* Used for transforming datastructures to and from the script-access forms
* ie, converting a "Vector3d" into a "Vector" and vice versa
*/
public class AccessTransforms {
/**
* Converts a JOML vector to an access vector
* @param source The JOML vector
* @return The access vector
*/
public static electrosphere.script.access.Vector getVector(org.joml.Vector3d source){
return new electrosphere.script.access.Vector(source.x, source.y, source.z);
}
/**
* Converts an access vector into a JOML vector
* @param source The access vectpr
* @return The JOML vector
*/
public static org.joml.Vector3d getVector(electrosphere.script.access.Vector source){
return new org.joml.Vector3d(source.x,source.y,source.z);
}
}