mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-16 21:44:05 +09:00
gdx 1.10/lwjgl3 migration; removing old Lua stuffs
This commit is contained in:
@@ -1,398 +0,0 @@
|
||||
package net.torvald.terrarum.gameactors.ai
|
||||
|
||||
import org.luaj.vm2.LuaTable
|
||||
import org.luaj.vm2.LuaValue
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2016-10-24.
|
||||
*/
|
||||
/*internal class AILuaAPI(g: Globals, actor: ActorWBMovable) {
|
||||
|
||||
// FIXME when actor jumps, the actor releases left/right stick
|
||||
|
||||
init {
|
||||
if (actor !is AIControlled)
|
||||
throw IllegalArgumentException("The actor is not AIControlled! $actor")
|
||||
|
||||
// load functions and set up constants
|
||||
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)
|
||||
g["ai"]["moveDown"] = MoveDown(actor)
|
||||
g["ai"]["moveLeft"] = MoveLeft(actor)
|
||||
g["ai"]["moveRight"] = MoveRight(actor)
|
||||
g["ai"]["moveTo"] = MoveTo(actor)
|
||||
g["ai"]["jump"] = Jump(actor)
|
||||
|
||||
g["ai"]["getNearbyTiles"] = GetNearbyTiles(actor)
|
||||
g["ai"]["getFloorsHeight"] = GetFloorsHeight(actor)
|
||||
g["ai"]["getCeilingsHeight"] = GetCeilingsHeight(actor)
|
||||
g["ai"]["getLedgesHeight"] = GetLedgesHeight(actor)
|
||||
|
||||
g["game"] = LuaValue.tableOf()
|
||||
g["game"]["version"] = GameVersion()
|
||||
g["game"]["versionRaw"] = GameVersionRaw()
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Reads arbitrary ActorWBMovable and returns its information as Lua table
|
||||
*/
|
||||
fun composeActorObject(actor: ActorWBMovable): LuaTable {
|
||||
val t: LuaTable = LuaTable()
|
||||
val moveDelta = actor.externalV + actor.controllerV
|
||||
|
||||
t["name"] = actor.actorValue.getAsString(AVKey.NAME).toLua()
|
||||
t["startX"] = actor.hitbox.centeredX.toLua()
|
||||
t["startY"] = actor.hitbox.centeredY.toLua()
|
||||
|
||||
t["veloX"] = moveDelta.x.toLua()
|
||||
t["veloY"] = moveDelta.y.toLua()
|
||||
|
||||
t["width"] = actor.hitbox.width.toLua()
|
||||
t["height"] = actor.hitbox.height.toLua()
|
||||
|
||||
t["mass"] = actor.mass.toLua()
|
||||
|
||||
t["collisionType"] = actor.collisionType.toLua()
|
||||
|
||||
t["strength"] = actor.avStrength.toLua()
|
||||
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
operator fun LuaTable.set(index: Int, value: Int) { this[index] = value.toLua() }
|
||||
}
|
||||
|
||||
class GetSelfActorInfo(val actor: ActorWBMovable) : ZeroArgFunction() {
|
||||
override fun call(): LuaValue {
|
||||
return composeActorObject(actor)
|
||||
}
|
||||
}
|
||||
|
||||
/** ai.getNearestActor(nullable any criterion, nullable number range) */
|
||||
class GetNearestActor() : LuaFunction() {
|
||||
override fun call(): LuaValue {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
override fun call(crit: LuaValue): LuaValue {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
override fun call(crit: LuaValue, range: LuaValue): LuaValue {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
/** ai.getNearestPlayer(nullable any criterion, nullable number range) */
|
||||
class GetNearestPlayer() : LuaFunction() {
|
||||
override fun call(): LuaValue {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
override fun call(crit: LuaValue): LuaValue {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
override fun call(crit: LuaValue, range: LuaValue): LuaValue {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
class GetX(val actor: ActorWBMovable) : ZeroArgFunction() {
|
||||
override fun call(): LuaValue {
|
||||
return LuaValue.valueOf(actor.hitbox.centeredX)
|
||||
}
|
||||
}
|
||||
|
||||
class GetY(val actor: ActorWBMovable) : ZeroArgFunction() {
|
||||
override fun call(): LuaValue {
|
||||
return LuaValue.valueOf(actor.hitbox.centeredY)
|
||||
}
|
||||
}
|
||||
|
||||
class MoveLeft(val actor: AIControlled) : LuaFunction() {
|
||||
override fun call(): LuaValue { // hard key press
|
||||
actor.moveLeft()
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
/** @param amount [0.0 - 1.0] */
|
||||
override fun call(amount: LuaValue): LuaValue { // stick tilt
|
||||
actor.moveLeft(amount.checkdouble().toFloat())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
class MoveRight(val actor: AIControlled) : LuaFunction() {
|
||||
override fun call(): LuaValue { // hard key press
|
||||
actor.moveRight()
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
/** @param amount [0.0 - 1.0] */
|
||||
override fun call(amount: LuaValue): LuaValue { // stick tilt
|
||||
actor.moveRight(amount.checkdouble().toFloat())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
class MoveUp(val actor: AIControlled) : LuaFunction() {
|
||||
override fun call(): LuaValue { // hard key press
|
||||
actor.moveUp()
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
/** @param amount [0.0 - 1.0] */
|
||||
override fun call(amount: LuaValue): LuaValue { // stick tilt
|
||||
actor.moveUp(amount.checkdouble().toFloat())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
class MoveDown(val actor: AIControlled) : LuaFunction() {
|
||||
override fun call(): LuaValue { // hard key press
|
||||
actor.moveDown()
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
/** @param amount [0.0 - 1.0] */
|
||||
override fun call(amount: LuaValue): LuaValue { // stick tilt
|
||||
actor.moveDown(amount.checkdouble().toFloat())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
class MoveTo(val actor: AIControlled) : LuaFunction() {
|
||||
override fun call(bearing: LuaValue): LuaValue {
|
||||
actor.moveTo(bearing.checkdouble())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
override fun call(toX: LuaValue, toY: LuaValue): LuaValue {
|
||||
actor.moveTo(toX.checkdouble(), toY.checkdouble())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
class Jump(val actor: AIControlled) : LuaFunction() {
|
||||
override fun call(): LuaValue {
|
||||
actor.moveJump()
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
/** @param amount [0.0 - 1.0] */
|
||||
override fun call(amount: LuaValue): LuaValue { // stick tilt
|
||||
actor.moveJump(amount.checkdouble().toFloat())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
class GetNearbyTiles(val actor: ActorWBMovable) : OneArgFunction() {
|
||||
/** @param radius
|
||||
*
|
||||
* 3 will return 7x7 array, 0 will return 1x1, 1 will return 3x3
|
||||
*
|
||||
* Index: [-3: y][-3: x] ... [0: y][0: x] ... [3: y][3: x] for radius 3
|
||||
* Return value: bitset (int 0-7)
|
||||
* 1 -- solidity
|
||||
* 2 -- liquidity
|
||||
* 3 -- gravity
|
||||
*/
|
||||
override fun call(arg: LuaValue): LuaValue {
|
||||
val radius = arg.checkint()
|
||||
|
||||
if (radius < 0) {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
else if (radius > 8) {
|
||||
throw IllegalArgumentException("Radius too large -- must be 8 or less")
|
||||
}
|
||||
else {
|
||||
val luatable = LuaTable()
|
||||
val feetTilePos = actor.feetPosTile
|
||||
for (y in feetTilePos[1] - radius..feetTilePos[1] + radius) {
|
||||
luatable[y - feetTilePos[1]] = LuaTable()
|
||||
|
||||
for (x in feetTilePos[0] - radius..feetTilePos[0] + radius) {
|
||||
val tile = BlockCodex[(Terrarum.ingame!!.world).getTileFromTerrain(x, y) ?: Block.NULL]
|
||||
val solidity = tile.isSolid.toInt()
|
||||
val liquidity = tile.isFluid.toInt()
|
||||
val gravity = tile.maxSupport.toInt()
|
||||
val tileFlag: Int = gravity.shl(2) + liquidity.shl(1) + solidity
|
||||
|
||||
luatable[y - feetTilePos[1]][x - feetTilePos[0]] = tileFlag.toLua()
|
||||
}
|
||||
}
|
||||
|
||||
return luatable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GetFloorsHeight(val actor: ActorWBMovable) : OneArgFunction() {
|
||||
/** @param radius
|
||||
*
|
||||
* 3 will return len:7 array, 0 will return len:1, 1 will return len:3
|
||||
*
|
||||
* Index: [-3] .. [0] .. [3] for radius
|
||||
* Return value: floor height
|
||||
* 0: body tile (legs area)
|
||||
* 1: tile you can stand on
|
||||
* 2+: tiles down there
|
||||
*/
|
||||
override fun call(arg: LuaValue): LuaValue {
|
||||
val radius = arg.checkint()
|
||||
|
||||
val searchDownLimit = 12
|
||||
|
||||
if (radius < 0) {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
else if (radius > 8) {
|
||||
throw IllegalArgumentException("Radius too large -- must be 8 or less")
|
||||
}
|
||||
else {
|
||||
val luatable = LuaTable()
|
||||
val feetTilePos = actor.feetPosTile
|
||||
for (x in feetTilePos[0] - radius..feetTilePos[0] + radius) {
|
||||
// search down
|
||||
var searchDownCounter = 0
|
||||
while (true) {
|
||||
val tile = (Terrarum.ingame!!.world).getTileFromTerrain(x, feetTilePos[1] + searchDownCounter) ?: Block.STONE
|
||||
if (BlockCodex[tile].isSolid || searchDownCounter >= searchDownLimit) {
|
||||
luatable[x - feetTilePos[0]] = searchDownCounter
|
||||
break
|
||||
}
|
||||
searchDownCounter++
|
||||
}
|
||||
}
|
||||
|
||||
return luatable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GetCeilingsHeight(val actor: ActorWBMovable) : OneArgFunction() {
|
||||
/** @param arg radius
|
||||
*
|
||||
* 3 will return 7x7 array, 0 will return 1x1, 1 will return 3x3
|
||||
*
|
||||
* Index: [-3] .. [0] .. [3] for radius
|
||||
* Return value: floor height
|
||||
* 0: body tile (legs area)
|
||||
* 1: body tile (may be vary depend on the size of the actor)
|
||||
* 2+: tiles up there
|
||||
*/
|
||||
override fun call(arg: LuaValue): LuaValue {
|
||||
val radius = arg.checkint()
|
||||
|
||||
val searchUpLimit = 12
|
||||
|
||||
if (radius < 0) {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
else if (radius > 8) {
|
||||
throw IllegalArgumentException("Radius too large -- must be 8 or less")
|
||||
}
|
||||
else {
|
||||
val luatable = LuaTable()
|
||||
val feetTilePos = actor.feetPosTile
|
||||
for (x in feetTilePos[0] - radius..feetTilePos[0] + radius) {
|
||||
// search up
|
||||
var searchUpCounter = 0
|
||||
while (true) {
|
||||
val tile = (Terrarum.ingame!!.world).getTileFromTerrain(x, feetTilePos[1] - searchUpCounter) ?: Block.STONE
|
||||
if (BlockCodex[tile].isSolid || searchUpCounter >= searchUpLimit) {
|
||||
luatable[x - feetTilePos[0]] = searchUpCounter
|
||||
break
|
||||
}
|
||||
searchUpCounter++
|
||||
}
|
||||
}
|
||||
|
||||
return luatable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GetLedgesHeight(val actor: ActorWBMovable) : OneArgFunction() {
|
||||
/** @param arg radius
|
||||
* ==
|
||||
* <- (non-solid found)
|
||||
* ==
|
||||
* ==
|
||||
* ==
|
||||
* == @ -> ledge height: 4
|
||||
* =================
|
||||
*/
|
||||
override fun call(arg: LuaValue): LuaValue {
|
||||
val radius = arg.checkint()
|
||||
|
||||
val searchUpLimit = 12
|
||||
|
||||
if (radius < 0) {
|
||||
return LuaValue.NONE
|
||||
}
|
||||
else if (radius > 8) {
|
||||
throw IllegalArgumentException("Radius too large -- must be 8 or less")
|
||||
}
|
||||
else {
|
||||
val luatable = LuaTable()
|
||||
val feetTilePos = actor.feetPosTile
|
||||
for (x in feetTilePos[0] - radius..feetTilePos[0] + radius) {
|
||||
// search up
|
||||
var searchUpCounter = 0
|
||||
while (true) {
|
||||
val tile = (Terrarum.ingame!!.world).getTileFromTerrain(x, feetTilePos[1] - searchUpCounter) ?: Block.STONE
|
||||
if (!BlockCodex[tile].isSolid || searchUpCounter >= searchUpLimit) {
|
||||
luatable[x - feetTilePos[0]] = searchUpCounter
|
||||
break
|
||||
}
|
||||
searchUpCounter++
|
||||
}
|
||||
}
|
||||
|
||||
return luatable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
class GameVersion : ZeroArgFunction() {
|
||||
override fun call(): LuaValue {
|
||||
return AppLoader.getVERSION_STRING().toLua()
|
||||
}
|
||||
}
|
||||
|
||||
class GameVersionRaw : ZeroArgFunction() {
|
||||
override fun call(): LuaValue {
|
||||
return AppLoader.VERSION_RAW.toLua()
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
fun Double.toLua() = LuaValue.valueOf(this)
|
||||
fun Int.toLua() = LuaValue.valueOf(this)
|
||||
fun String.toLua() = LuaValue.valueOf(this)
|
||||
fun Boolean.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()
|
||||
fun Boolean?.toLua() = if (this == null) LuaValue.NIL else this.toLua()
|
||||
fun luaTableOf(vararg luaValues: LuaValue): LuaTable {
|
||||
val t = LuaTable.tableOf()
|
||||
luaValues.forEachIndexed { index, luaValue -> t[index + 1] = luaValue }
|
||||
return t
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package net.torvald.terrarum.gameactors.ai
|
||||
|
||||
import net.torvald.terrarum.gameactors.Actor
|
||||
import org.luaj.vm2.Globals
|
||||
import org.luaj.vm2.LuaError
|
||||
import org.luaj.vm2.LuaInteger
|
||||
import org.luaj.vm2.LuaValue
|
||||
import org.luaj.vm2.lib.jse.JsePlatform
|
||||
import java.io.InputStreamReader
|
||||
import java.io.Reader
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2017-02-04.
|
||||
*/
|
||||
/*class LuaAIWrapper(private val scriptPath: String) : ActorAI {
|
||||
|
||||
protected val luag: Globals = JsePlatform.standardGlobals()
|
||||
|
||||
/**
|
||||
* Initialised in init block.
|
||||
* Use lua function "update(delta)" to step the AI.
|
||||
*/
|
||||
protected lateinit var luaInstance: LuaValue
|
||||
|
||||
private lateinit var aiLuaAPI: AILuaAPI
|
||||
|
||||
private lateinit var targetActor: Actor
|
||||
|
||||
/**
|
||||
* The initialiser
|
||||
*
|
||||
* Use ```(p.ai as LuaAIWrapper).attachActor(p)```
|
||||
*/
|
||||
fun attachActor(actor: Actor) {
|
||||
targetActor = actor
|
||||
|
||||
luag["io"] = LuaValue.NIL
|
||||
luag["os"] = LuaValue.NIL
|
||||
luag["luajava"] = LuaValue.NIL
|
||||
aiLuaAPI = AILuaAPI(luag, targetActor)
|
||||
// load the script and execute it (initialises target script)
|
||||
val inputStream = javaClass.getResourceAsStream(scriptPath)
|
||||
luaInstance = luag.load(InputStreamReader(inputStream), scriptPath.split(Regex("[\\/]")).last())
|
||||
luaInstance.call()
|
||||
}
|
||||
|
||||
override fun update(actor: Actor, delta: Float) {
|
||||
// run "update()" function in the script
|
||||
luag.get("update").call(delta.toLua())
|
||||
}
|
||||
|
||||
lateinit var currentExecutionThread: Thread
|
||||
var threadRun = false
|
||||
|
||||
fun runCommand(reader: Reader, filename: String) {
|
||||
if (!threadRun && !targetActor.flagDespawn) {
|
||||
currentExecutionThread = Thread(ThreadRunCommand(luag, reader, filename))
|
||||
currentExecutionThread.start()
|
||||
threadRun = true
|
||||
}
|
||||
}
|
||||
|
||||
fun runCommand(script: String) {
|
||||
if (!threadRun && !targetActor.flagDespawn) {
|
||||
currentExecutionThread = Thread(ThreadRunCommand(luag, script, ""))
|
||||
currentExecutionThread.start()
|
||||
threadRun = true
|
||||
}
|
||||
}
|
||||
|
||||
class ThreadRunCommand : Runnable {
|
||||
|
||||
val mode: Int
|
||||
val arg1: Any
|
||||
val arg2: String
|
||||
val lua: Globals
|
||||
|
||||
constructor(luaInstance: Globals, line: String, env: String) {
|
||||
mode = 0
|
||||
arg1 = line
|
||||
arg2 = env
|
||||
lua = luaInstance
|
||||
}
|
||||
|
||||
constructor(luaInstance: Globals, reader: Reader, filename: String) {
|
||||
mode = 1
|
||||
arg1 = reader
|
||||
arg2 = filename
|
||||
lua = luaInstance
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
try {
|
||||
val chunk: LuaValue
|
||||
if (mode == 0)
|
||||
chunk = lua.load(arg1 as String, arg2)
|
||||
else if (mode == 1)
|
||||
chunk = lua.load(arg1 as Reader, arg2)
|
||||
else
|
||||
throw IllegalArgumentException("Unsupported mode: $mode")
|
||||
|
||||
|
||||
chunk.call()
|
||||
}
|
||||
catch (e: LuaError) {
|
||||
e.printStackTrace(System.err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Float.toLua(): LuaValue = LuaInteger.valueOf(this.toDouble())
|
||||
}*/
|
||||
@@ -1,83 +0,0 @@
|
||||
--
|
||||
-- Created by minjaesong on 2017-01-06.
|
||||
--
|
||||
timeCounter = 0
|
||||
countMax = 0
|
||||
moveMode = math.random() >= 0.5 and "left" or "right"
|
||||
currentMode = "move"
|
||||
jumpheight = 6 -- lol
|
||||
|
||||
function generateCountMax()
|
||||
local function generateTurn()
|
||||
return 4600 + 1250 * math.random()
|
||||
end
|
||||
|
||||
local function generateWalk()
|
||||
return 1645 + 402 * math.random()
|
||||
end
|
||||
|
||||
return (currentMode == "move") and generateWalk() or generateTurn()
|
||||
end
|
||||
|
||||
function moveToDirection(delta)
|
||||
local pits = ai.getFloorsHeight(2)
|
||||
local ledges = ai.getLedgesHeight(1)
|
||||
|
||||
if moveMode == "left" then
|
||||
if pits[-1] == 1 then
|
||||
ai.moveLeft(0.8)
|
||||
if ledges[-1] <= jumpheight then -- no futile jumps
|
||||
ai.jump()
|
||||
end
|
||||
else
|
||||
ai.moveLeft(0.5)
|
||||
end
|
||||
elseif moveMode == "right" then
|
||||
if pits[1] == 1 then
|
||||
ai.moveRight(0.8)
|
||||
if ledges[1] <= jumpheight then -- no futile jumps
|
||||
ai.jump()
|
||||
end
|
||||
else
|
||||
ai.moveRight(0.5)
|
||||
end
|
||||
end
|
||||
|
||||
timeCounter = timeCounter + delta
|
||||
end
|
||||
|
||||
function toggleCurrentMode()
|
||||
currentMode = (currentMode == "move") and "turn" or "move"
|
||||
end
|
||||
|
||||
function toggleMoveMode()
|
||||
moveMode = (moveMode == "left") and "right" or "left"
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
countMax = generateCountMax()
|
||||
|
||||
function toggleCondition()
|
||||
local floorsheight = ai.getFloorsHeight(1)
|
||||
return timeCounter >= countMax or
|
||||
-- avoid great falls
|
||||
(timeCounter > 150 and (floorsheight[-1] > jumpheight or floorsheight[1] > jumpheight))
|
||||
end
|
||||
|
||||
function update(delta)
|
||||
if currentMode == "move" then
|
||||
moveToDirection(delta)
|
||||
else
|
||||
timeCounter = timeCounter + delta -- no countup when jumping
|
||||
end
|
||||
|
||||
if toggleCondition() then
|
||||
timeCounter = 0
|
||||
toggleCurrentMode()
|
||||
countMax = generateCountMax()
|
||||
if currentMode == "turn" then
|
||||
toggleMoveMode()
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user