Skip to content

Commit

Permalink
scripting cont
Browse files Browse the repository at this point in the history
  • Loading branch information
NeumimTo committed Nov 27, 2021
1 parent ee27e38 commit e5ecd36
Show file tree
Hide file tree
Showing 20 changed files with 296 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Author("NeumimTo")
@Website("https://github.com/Sponge-RPG-dev/NT-RPG")
@LogPrefix("NTRPG")
@ApiVersion(ApiVersion.Target.v1_14)
@ApiVersion(ApiVersion.Target.v1_17)
@SoftDependsOn(
value = {
@SoftDependency("PlaceholderAPI"),
Expand Down
Binary file modified Common/lib-cp/NT-Script-0.1.9.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.neumimto.rpg.common.events.damage;

import cz.neumimto.rpg.common.entity.IEntity;
import cz.neumimto.rpg.common.items.RpgItemStack;

import java.util.Optional;
Expand All @@ -10,4 +11,7 @@ public interface IEntityWeaponDamageEarlyEvent extends DamageIEntityEarlyEvent {

void setWeapon(RpgItemStack weapon);

void setDamager(IEntity attacker);

IEntity getDamager();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.inject.Key;
import cz.neumimto.nts.NTScript;
import cz.neumimto.nts.annotations.ScriptMeta;
import cz.neumimto.rpg.common.Rpg;
import cz.neumimto.rpg.common.assets.AssetService;
import cz.neumimto.rpg.common.effects.IEffect;
import cz.neumimto.rpg.common.logging.Log;
Expand All @@ -14,6 +15,7 @@
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
Expand Down Expand Up @@ -55,18 +57,18 @@ public NTScript scriptContextForSkills(Consumer<NTScript.Builder> builder, Class
}

NTScript.Builder n = NTScript.builder()
.implementingType(ScriptSkill.class)
.implementingType(type)
.classAnnotations(new Class[]{Singleton.class})
.fieldAnnotation(new Class[]{Inject.class})
.macro(macrosMap)
.withEnum(SkillResult.class)
.implementingType(type)
.debugOutput(Rpg.get().getWorkingDirectory() + File.separator + "/compiled-scripts")
.add(getStl())
.debugOutput("/tmp")
.logging(Log::warn)
.package_("cz.neumimto.rpg.script.skills")
.setClassNamePattern(type.getSimpleName());

n.add(STL);
builder.accept(n);

return n.build();
Expand All @@ -87,25 +89,6 @@ public List<Object> getStl() {
.forEach(list::add);

} catch (Throwable t) {
Log.info("Java service provider bug still present, doing it the stupid way");

try {
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.BleedingEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.FeatherFall"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.Maim"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.ManaShieldEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.NoAutohealEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.PiggifyEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.SlowEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.StunEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.UnhealEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.UnlimtedFoodLevelEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.WebEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.FlickerEffect"));
list.add(Class.forName("cz.neumimto.rpg.spigot.effects.common.InvisibilityEffect"));
} catch (Exception e) {

}
}

list.addAll(STL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SkillResult castOnTarget(@NamedParam("caster") IActiveCharacter caster,

interface Passive extends SkillScriptHandlers {
@ScriptTarget
SkillResult init(@NamedParam("caster") IActiveCharacter caster,
void init(@NamedParam("caster") IActiveCharacter caster,
@NamedParam("context") PlayerSkillContext context,
@NamedParam("this_skill")ISkill iSkill);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cz.neumimto.rpg.common.skills.PlayerSkillContext;
import jdk.jfr.Name;

import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Logger;

@AutoService(NTScriptProxy.class)
Expand All @@ -32,6 +33,12 @@ public boolean exists(@NamedParam("o|obj|var") Object o) {
return o != null;
}

@Handler
@Function("roll")
public boolean roll(@NamedParam("pass") double min, @NamedParam("maxVal|max") double max) {
return ThreadLocalRandom.current().nextDouble(max) <= min;
}

@Handler
@Function("print")
public void print(@NamedParam("o|obj|var") Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.stream.Stream;

import static cz.neumimto.rpg.common.logging.Log.*;
Expand Down Expand Up @@ -70,6 +71,8 @@ public SkillService() {
Stream.of(SkillType.values()).forEach(this::registerSkillType);
}

public abstract Consumer<NTScript.Builder> getNTSBuilderContext();

public void load() {
skillTrees.clear();
reloadSkills();
Expand Down Expand Up @@ -170,9 +173,6 @@ public void registerSkillAlternateName(String name, ISkill skill) {
skillByNames.put(name, skill);
}


public abstract NTScript getNtScriptCompilerFor(Class<? extends SkillScriptHandlers> c);

public ISkill skillDefinitionToSkill(ScriptSkillModel scriptSkillModel, ClassLoader classLoader) {

if (scriptSkillModel.handlerId.equalsIgnoreCase("nts")) {
Expand All @@ -190,14 +190,30 @@ public ISkill skillDefinitionToSkill(ScriptSkillModel scriptSkillModel, ClassLoa

Log.info("Compiling nts script " + scriptSkillModel.id + " as " + c.getSimpleName());

Class<? extends SkillScriptHandlers> generate = getNtScriptCompilerFor(c).compile(scriptSkillModel.script);
Class<? extends SkillScriptHandlers> generate = ntScriptEngine.prepareCompiler(getNTSBuilderContext(),c)
.compile(scriptSkillModel.script);
if (generate == null) {
Log.error("Unable to generate script " + scriptSkillModel.id);
}
SkillScriptHandlers instance = injector.getInstance(generate);
ScriptSkill ss = getSkillByHandlerType(instance);
ss.setModel(scriptSkillModel);
ss.setHandler(instance);
if (ss instanceof PassiveScriptSkill p) {
p.setRelevantEffectName(scriptSkillModel.relevantEffectName);
}

if (scriptSkillModel.skillTypes != null) {
for (String skillType : scriptSkillModel.skillTypes) {
SkillType st = SkillType.byId(skillType);
if (st == null) {
Log.warn("Unknown skill type " + st);
} else {
ss.addSkillType(st);
}
}
}

injector.injectMembers(ss);
injectCatalogId((ISkill) ss, scriptSkillModel.id);
return (ISkill) ss;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public ClassVisitor wrap(TypeDescription instrumentedType, ClassVisitor classVis

}

// bb.make().saveIn(new File("/tmp"));
Class<? extends EffectBase> loaded = bb.make()
.load(classLoader, INJECTION)
.getLoaded();
Expand Down Expand Up @@ -202,7 +201,7 @@ private static ScriptEffectBase.Handler generateHandler(String script, ClassLoad
.getLoaded();

Class compile = Rpg.get().getScriptEngine()
.prepareCompiler(builder -> {}, loaded1)
.prepareCompiler(Rpg.get().getSkillService().getNTSBuilderContext(), loaded1)
.compile(script);
try {
Object o = compile.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public static Class from(ScriptListenerModel model, ClassLoader classLoader) {
.make()
.load(classLoader, ClassLoadingStrategy.Default.INJECTION)
.getLoaded();
Class compile = Rpg.get().getScriptEngine().prepareCompiler(builder -> {
}, bb).compile(model.script);

Class compile = Rpg.get().getScriptEngine().prepareCompiler(Rpg.get().getSkillService().getNTSBuilderContext(), bb).compile(model.script);
return compile;
} catch (Throwable t) {
t.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class ScriptSkillModel {
@Path("Script")
public String script;

@Optional
@Path("RelevantEffectName")
public String relevantEffectName;

@Optional
@Path("SuperType")
public String superType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ public String getId() {
public String getTranslationKey() {
return name;
}

public static SkillType byId(String id) {
for (SkillType value : values()) {
if (value.id.equalsIgnoreCase(id)) {
return value;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package cz.neumimto.rpg.common.skills.types;

import cz.neumimto.rpg.common.Rpg;
import cz.neumimto.rpg.common.entity.players.IActiveCharacter;
import cz.neumimto.rpg.common.logging.Log;
import cz.neumimto.rpg.common.scripting.SkillScriptHandlers;
import cz.neumimto.rpg.common.skills.ISkillType;
import cz.neumimto.rpg.common.skills.PlayerSkillContext;
import cz.neumimto.rpg.common.skills.scripting.ScriptSkillModel;

import javax.script.Bindings;
import javax.script.SimpleBindings;
import java.util.List;
import java.util.Optional;

/**
* Created by NeumimTo on 7.10.2018.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package cz.neumimto.rpg.common.skills.types;

import cz.neumimto.nts.annotations.ScriptMeta.NamedParam;
import cz.neumimto.nts.annotations.ScriptMeta.ScriptTarget;
import cz.neumimto.rpg.common.effects.EffectService;
import cz.neumimto.rpg.common.entity.players.IActiveCharacter;
import cz.neumimto.rpg.common.inventory.InventoryService;
Expand All @@ -10,14 +12,13 @@
import cz.neumimto.rpg.common.skills.PlayerSkillContext;
import cz.neumimto.rpg.common.skills.SkillExecutionType;
import cz.neumimto.rpg.common.skills.SkillResult;
import cz.neumimto.rpg.common.skills.scripting.JsBinding;

import javax.inject.Inject;

/**
* Created by NeumimTo on 6.8.2015.
*/
@JsBinding(JsBinding.Type.CLASS)

public abstract class PassiveSkill extends AbstractSkill<IActiveCharacter> {

public static enum Type {
Expand Down Expand Up @@ -53,6 +54,7 @@ public SkillResult onPreUse(IActiveCharacter character, PlayerSkillContext esi)
protected void update(IActiveCharacter IActiveCharacter) {
inventoryService.initializeCharacterInventory(IActiveCharacter);
PlayerSkillContext skill = IActiveCharacter.getSkill(getId());
effectService.removeEffect(relevantEffectName, IActiveCharacter, this);
applyEffect(skill, IActiveCharacter);
}

Expand All @@ -79,10 +81,20 @@ public void skillRefund(IActiveCharacter IActiveCharacter, PlayerSkillContext co
}
}

public abstract void applyEffect(PlayerSkillContext info, IActiveCharacter character);
@ScriptTarget
public abstract void applyEffect(@NamedParam("c|context") PlayerSkillContext info,
@NamedParam("caster") IActiveCharacter character);

@Override
public SkillExecutionType getSkillExecutionType() {
return SkillExecutionType.PASSIVE;
}

public String getRelevantEffectName() {
return relevantEffectName;
}

public void setRelevantEffectName(String relevantEffectName) {
this.relevantEffectName = relevantEffectName;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.neumimto.rpg.common.utils;

import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -27,6 +28,10 @@ public static double round(float value, int precision) {
return Math.round(value * scale) / scale;
}

public static double randomInRange(double min, double max) {
return min + (max - min) * ThreadLocalRandom.current().nextDouble();
}

public static boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
Expand Down
Loading

0 comments on commit e5ecd36

Please sign in to comment.