mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-18 22:44:04 +09:00
actor AI from Lua script
Former-commit-id: 184160efc59c4f846f1cc154fe3e60d21b301ee3 Former-commit-id: 4e228542975ea52945a597b7ca1bc06b407c3be7
This commit is contained in:
@@ -16,12 +16,17 @@ import org.luaj.vm2.lib.ZeroArgFunction
|
||||
internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
|
||||
|
||||
init {
|
||||
if (actor !is AIControlled) throw IllegalArgumentException("The actor is not AIControlled! $actor")
|
||||
if (actor !is AIControlled)
|
||||
throw IllegalArgumentException("The actor is not AIControlled! $actor")
|
||||
|
||||
// load things. WARNING: THIS IS MANUAL!
|
||||
g["ai"] = LuaValue.tableOf()
|
||||
|
||||
g["ai"]["getSelfActorInfo"] = GetSelfActorInfo(actor)
|
||||
|
||||
g["ai"]["getNearestActor"] = GetNearestActor()
|
||||
g["ai"]["getNearestPlayer"] = GetNearestPlayer()
|
||||
|
||||
g["ai"]["getX"] = GetX(actor)
|
||||
g["ai"]["getY"] = GetY(actor)
|
||||
g["ai"]["moveUp"] = MoveUp(actor)
|
||||
@@ -29,10 +34,14 @@ internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
|
||||
g["ai"]["moveLeft"] = MoveLeft(actor)
|
||||
g["ai"]["moveRight"] = MoveRight(actor)
|
||||
g["ai"]["moveTo"] = MoveTo(actor)
|
||||
g["ai"]["jump"] = Jump(actor)
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Reads arbitrary ActorWithBody and returns its information as Lua table
|
||||
*/
|
||||
fun composeActorObject(actor: ActorWithBody): LuaTable {
|
||||
val t = LuaValue.tableOf()
|
||||
|
||||
@@ -48,7 +57,7 @@ internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
|
||||
|
||||
t["mass"] = actor.mass
|
||||
|
||||
t["collision_type"] = actor.collisionType
|
||||
t["collisionType"] = actor.collisionType
|
||||
|
||||
t["strength"] = actor.actorValue.getAsInt(AVKey.STRENGTH) ?: 0
|
||||
|
||||
@@ -56,7 +65,7 @@ internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
|
||||
val MUL_2 = LightmapRenderer.MUL_2
|
||||
val MUL = LightmapRenderer.MUL
|
||||
val CHMAX = LightmapRenderer.CHANNEL_MAX
|
||||
t["luminosity_rgb"] = lumrgb
|
||||
t["luminosityRGB"] = lumrgb
|
||||
t["luminosity"] = (lumrgb.div(MUL_2).and(CHMAX).times(3) +
|
||||
lumrgb.div(MUL).and(CHMAX).times(4) +
|
||||
lumrgb.and(1023)) / 8 // quick luminosity calculation
|
||||
@@ -65,6 +74,12 @@ internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
|
||||
}
|
||||
}
|
||||
|
||||
class GetSelfActorInfo(val actor: ActorWithBody) : ZeroArgFunction() {
|
||||
override fun call(): LuaValue {
|
||||
return composeActorObject(actor)
|
||||
}
|
||||
}
|
||||
|
||||
/** ai.getNearestActor(nullable any criterion, nullable number range) */
|
||||
class GetNearestActor() : LuaFunction() {
|
||||
override fun call(): LuaValue {
|
||||
@@ -147,4 +162,11 @@ internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
class Jump(val actor: AIControlled) : ZeroArgFunction() {
|
||||
override fun call(): LuaValue {
|
||||
actor.moveJump()
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package net.torvald.terrarum.gameactors.ai.scripts
|
||||
|
||||
/**
|
||||
* Randomly roams around.
|
||||
*
|
||||
* Created by SKYHi14 on 2016-12-23.
|
||||
*/
|
||||
object PokemonNPCAI {
|
||||
operator fun invoke(): String = """
|
||||
ai.jump()
|
||||
ai.moveRight()
|
||||
|
||||
thisActorInfo = ai.getSelfActorInfo()
|
||||
print(thisActorInfo.strength)
|
||||
"""
|
||||
}
|
||||
Reference in New Issue
Block a user