more AI, divided ACCELBUFF and ACCELMULT_MOVEMENT

Former-commit-id: f3b4c390c363612dcc58c3d50cb7a47ba7452567
Former-commit-id: 95f71db97104cf55af7aba8e0289eb89efc078a4
This commit is contained in:
Song Minjae
2016-12-29 21:10:42 +09:00
parent 63bc018550
commit f7365ea47b
14 changed files with 72 additions and 39 deletions

View File

@@ -18,6 +18,8 @@ object AVKey {
*/
const val ACCEL = "accel"
const val ACCELBUFF = "$ACCEL$BUFF"
/** internal value used by ActorHumanoid to implement friction in walkfunction */
const val ACCELMULT_MOVEMENT = "__accelmultmvmt"
const val SCALE = "scale"
const val SCALEBUFF = "$SCALE$BUFF" // aka PHYSIQUE
/** pixels */

View File

@@ -220,14 +220,14 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
private fun updateMovementControl() {
if (!noClip) {
if (grounded) {
actorValue[AVKey.ACCELBUFF] = 1.0
actorValue[AVKey.ACCELMULT_MOVEMENT] = 1.0
}
else {
actorValue[AVKey.ACCELBUFF] = ACCEL_MULT_IN_FLIGHT
actorValue[AVKey.ACCELMULT_MOVEMENT] = ACCEL_MULT_IN_FLIGHT
}
}
else {
actorValue[AVKey.ACCELBUFF] = 1.0
actorValue[AVKey.ACCELMULT_MOVEMENT] = 1.0
}
}

View File

@@ -1165,6 +1165,7 @@ open class ActorWithBody : Actor() {
val avAcceleration: Double
get() = actorValue.getAsDouble(AVKey.ACCEL)!! *
actorValue.getAsDouble(AVKey.ACCELBUFF)!! *
(actorValue.getAsDouble(AVKey.ACCELMULT_MOVEMENT) ?: 1.0) *
scale.sqrt()
val avSpeedCap: Double
get() = actorValue.getAsDouble(AVKey.SPEED)!! *

View File

@@ -27,14 +27,18 @@ open class HumanoidNPC(luaScript: EncapsulatedString, born: GameDate) : ActorHum
/**
* Initialised in init block.
* Use function "function update(delta)" to step the AI.
* Use lua function "update(delta)" to step the AI.
*/
protected val luaInstance: LuaValue
private val aiLuaAPI: AILuaAPI
init {
luag["io"] = LuaValue.NIL
luag["os"] = LuaValue.NIL
luag["luajava"] = LuaValue.NIL
aiLuaAPI = AILuaAPI(luag, this)
// load the script and execute it (initialises target script)
luaInstance = luag.load(luaScript.getString(), luaScript.javaClass.simpleName)
luaInstance.call()
}
@@ -77,8 +81,8 @@ open class HumanoidNPC(luaScript: EncapsulatedString, born: GameDate) : ActorHum
override fun update(gc: GameContainer, delta: Int) {
super.update(gc, delta)
aiLuaAPI.update(delta)
// run "update()" function in the script
luag.get("update").call(delta.toLuaValue())
}

View File

@@ -14,7 +14,7 @@ import org.luaj.vm2.lib.ZeroArgFunction
/**
* Created by minjaesong on 16-10-24.
*/
internal class AILuaAPI(val g: Globals, actor: ActorWithBody) {
internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
init {
if (actor !is AIControlled)
@@ -36,10 +36,11 @@ internal class AILuaAPI(val g: Globals, actor: ActorWithBody) {
g["ai"]["moveRight"] = MoveRight(actor)
g["ai"]["moveTo"] = MoveTo(actor)
g["ai"]["jump"] = Jump(actor)
}
fun update(delta: Int) {
// set up variables
g["game"] = LuaValue.tableOf()
g["game"]["version"] = GameVersion()
g["game"]["versionRaw"] = GameVersionRaw()
}
companion object {
@@ -47,35 +48,43 @@ internal class AILuaAPI(val g: Globals, actor: ActorWithBody) {
* Reads arbitrary ActorWithBody and returns its information as Lua table
*/
fun composeActorObject(actor: ActorWithBody): LuaTable {
val t = LuaValue.tableOf()
val t: LuaTable = LuaValue.tableOf()
t["name"] = actor.actorValue.getAsString(AVKey.NAME)
t["posX"] = actor.hitbox.centeredX
t["posY"] = actor.hitbox.centeredY
t["name"] = actor.actorValue.getAsString(AVKey.NAME).toLua()
t["posX"] = actor.hitbox.centeredX.toLua()
t["posY"] = actor.hitbox.centeredY.toLua()
t["veloX"] = actor.veloX
t["veloY"] = actor.veloY
t["veloX"] = actor.veloX.toLua()
t["veloY"] = actor.veloY.toLua()
t["width"] = actor.hitbox.width
t["height"] = actor.hitbox.height
t["width"] = actor.hitbox.width.toLua()
t["height"] = actor.hitbox.height.toLua()
t["mass"] = actor.mass
t["mass"] = actor.mass.toLua()
t["collisionType"] = actor.collisionType
t["collisionType"] = actor.collisionType.toLua()
t["strength"] = actor.avStrength
t["strength"] = actor.avStrength.toLua()
val lumrgb: Int = actor.actorValue.getAsInt(AVKey.LUMINOSITY) ?: 0
val MUL_2 = LightmapRenderer.MUL_2
val MUL = LightmapRenderer.MUL
val CHMAX = LightmapRenderer.CHANNEL_MAX
t["luminosityRGB"] = lumrgb
t["luminosityRGB"] = lumrgb.toLua()
t["luminosity"] = (lumrgb.div(MUL_2).and(CHMAX).times(3) +
lumrgb.div(MUL).and(CHMAX).times(4) +
lumrgb.and(1023)) / 8 // quick luminosity calculation
lumrgb.and(1023)).div(8.0).toLua() // quick luminosity calculation
return t
}
fun Double.toLua() = LuaValue.valueOf(this)
fun Int.toLua() = LuaValue.valueOf(this)
fun String.toLua() = LuaValue.valueOf(this)
fun Double?.toLua() = if (this == null) LuaValue.NIL else this.toLua()
fun Int?.toLua() = if (this == null) LuaValue.NIL else this.toLua()
fun String?.toLua() = if (this == null) LuaValue.NIL else this.toLua()
}
class GetSelfActorInfo(val actor: ActorWithBody) : ZeroArgFunction() {
@@ -203,4 +212,18 @@ internal class AILuaAPI(val g: Globals, actor: ActorWithBody) {
}
}
class GameVersion() : ZeroArgFunction() {
override fun call(): LuaValue {
return Terrarum.VERSION_STRING.toLua()
}
}
class GameVersionRaw() : ZeroArgFunction() {
override fun call(): LuaValue {
return Terrarum.VERSION_RAW.toLua()
}
}
}

View File

@@ -1,6 +1,8 @@
package net.torvald.terrarum.gameactors.ai.scripts
/**
* Encapsulated text file
*
* Created by SKYHi14 on 2016-12-28.
*/
abstract class EncapsulatedString {