Make entity query Lua binding have less overhead
This commit is contained in:
parent
6ed51b6ae9
commit
72d6db0a17
@ -43,7 +43,6 @@ import ru.dbotthepony.kstarbound.world.entities.HumanoidActorEntity
|
|||||||
import ru.dbotthepony.kstarbound.world.entities.ItemDropEntity
|
import ru.dbotthepony.kstarbound.world.entities.ItemDropEntity
|
||||||
import ru.dbotthepony.kstarbound.world.entities.api.InspectableEntity
|
import ru.dbotthepony.kstarbound.world.entities.api.InspectableEntity
|
||||||
import ru.dbotthepony.kstarbound.world.entities.api.InteractiveEntity
|
import ru.dbotthepony.kstarbound.world.entities.api.InteractiveEntity
|
||||||
import ru.dbotthepony.kstarbound.world.entities.api.LoungeableEntity
|
|
||||||
import ru.dbotthepony.kstarbound.world.entities.api.ScriptedEntity
|
import ru.dbotthepony.kstarbound.world.entities.api.ScriptedEntity
|
||||||
import ru.dbotthepony.kstarbound.world.entities.player.PlayerEntity
|
import ru.dbotthepony.kstarbound.world.entities.player.PlayerEntity
|
||||||
import ru.dbotthepony.kstarbound.world.entities.tile.ContainerObject
|
import ru.dbotthepony.kstarbound.world.entities.tile.ContainerObject
|
||||||
@ -59,11 +58,24 @@ private enum class EntityBoundMode(override val jsonName: String) : IStringSeria
|
|||||||
POSITION("Position")
|
POSITION("Position")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val callScriptStr = ByteString.of("callScript")
|
||||||
|
private val callScriptArgsStr = ByteString.of("callScriptArgs")
|
||||||
|
private val callScriptResultStr = ByteString.of("callScriptResult")
|
||||||
|
private val lineStr = ByteString.of("line")
|
||||||
|
private val polyStr = ByteString.of("poly")
|
||||||
|
private val rectStr = ByteString.of("rect")
|
||||||
|
private val withoutEntityIdStr = ByteString.of("withoutEntityId")
|
||||||
|
private val includedTypesStr = ByteString.of("includedTypes")
|
||||||
|
private val radiusStr = ByteString.of("radius")
|
||||||
|
private val centerStr = ByteString.of("center")
|
||||||
|
private val boundModeStr = ByteString.of("boundMode")
|
||||||
|
private val orderStr = ByteString.of("order")
|
||||||
|
|
||||||
private fun ExecutionContext.entityQueryImpl(self: World<*, *>, options: Table, predicate: Predicate<AbstractEntity> = Predicate { true }): Table {
|
private fun ExecutionContext.entityQueryImpl(self: World<*, *>, options: Table, predicate: Predicate<AbstractEntity> = Predicate { true }): Table {
|
||||||
val withoutEntityId = (indexNoYield(options, "withoutEntityId") as Number?)?.toInt()
|
val withoutEntityId = (indexNoYield(options, withoutEntityIdStr) as Number?)?.toInt()
|
||||||
|
|
||||||
val includedTypes = EnumSet.allOf(EntityType::class.java)
|
val includedTypes = EnumSet.allOf(EntityType::class.java)
|
||||||
val getIncludedTypes = indexNoYield(options, "includedTypes") as Table?
|
val getIncludedTypes = indexNoYield(options, includedTypesStr) as Table?
|
||||||
|
|
||||||
if (getIncludedTypes != null) {
|
if (getIncludedTypes != null) {
|
||||||
includedTypes.clear()
|
includedTypes.clear()
|
||||||
@ -92,24 +104,24 @@ private fun ExecutionContext.entityQueryImpl(self: World<*, *>, options: Table,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val callScript = (indexNoYield(options, "callScript") as ByteString?)?.decode()
|
val callScript = (indexNoYield(options, callScriptStr) as ByteString?)?.decode()
|
||||||
val callScriptArgs = (indexNoYield(options, "callScriptArgs") as Table?)?.unpackAsArray() ?: arrayOf()
|
val callScriptArgs = (indexNoYield(options, callScriptArgsStr) as Table?)?.unpackAsArray() ?: arrayOf()
|
||||||
val callScriptResult = indexNoYield(options, "callScriptResult") ?: true
|
val callScriptResult = indexNoYield(options, callScriptResultStr) ?: true
|
||||||
|
|
||||||
val lineQuery = (indexNoYield(options, "line") as Table?)?.let { toLine2d(it) }
|
val lineQuery = (indexNoYield(options, lineStr) as Table?)?.let { toLine2d(it) }
|
||||||
val polyQuery = (indexNoYield(options, "poly") as Table?)?.let { toPoly(it) }
|
val polyQuery = (indexNoYield(options, polyStr) as Table?)?.let { toPoly(it) }
|
||||||
val rectQuery = (indexNoYield(options, "rect") as Table?)?.let { toAABB(it) }
|
val rectQuery = (indexNoYield(options, rectStr) as Table?)?.let { toAABB(it) }
|
||||||
|
|
||||||
val radius = indexNoYield(options, "radius")
|
val radius = indexNoYield(options, radiusStr)
|
||||||
|
|
||||||
val radiusQuery = if (radius is Number) {
|
val radiusQuery = if (radius is Number) {
|
||||||
val center = toVector2d(indexNoYield(options, "center") ?: throw LuaRuntimeException("Specified 'radius', but not 'center'"))
|
val center = toVector2d(indexNoYield(options, centerStr) ?: throw LuaRuntimeException("Specified 'radius', but not 'center'"))
|
||||||
center to radius.toDouble()
|
center to radius.toDouble()
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
val boundMode = EntityBoundMode.entries.valueOf((indexNoYield(options, "boundMode") as ByteString?)?.decode() ?: "CollisionArea")
|
val boundMode = EntityBoundMode.entries.valueOf((indexNoYield(options, boundModeStr) as ByteString?)?.decode() ?: "CollisionArea")
|
||||||
|
|
||||||
val innerPredicate = Predicate<AbstractEntity> {
|
val innerPredicate = Predicate<AbstractEntity> {
|
||||||
if (!predicate.test(it)) return@Predicate false
|
if (!predicate.test(it)) return@Predicate false
|
||||||
@ -185,7 +197,7 @@ private fun ExecutionContext.entityQueryImpl(self: World<*, *>, options: Table,
|
|||||||
mutableListOf()
|
mutableListOf()
|
||||||
}
|
}
|
||||||
|
|
||||||
when (val order = (indexNoYield(options, "order") as ByteString?)?.decode()?.lowercase()) {
|
when (val order = (indexNoYield(options, orderStr) as ByteString?)?.decode()?.lowercase()) {
|
||||||
null -> {} // do nothing
|
null -> {} // do nothing
|
||||||
"random" -> entitites.shuffle(self.random)
|
"random" -> entitites.shuffle(self.random)
|
||||||
"nearest" -> {
|
"nearest" -> {
|
||||||
@ -204,11 +216,11 @@ private fun ExecutionContext.intermediateQueryFunction(self: World<*, *>, pos1:
|
|||||||
val actualOptions = options ?: tableOf()
|
val actualOptions = options ?: tableOf()
|
||||||
|
|
||||||
if (pos2OrRadius is Number) {
|
if (pos2OrRadius is Number) {
|
||||||
actualOptions["center"] = pos1
|
actualOptions[centerStr] = pos1
|
||||||
actualOptions["radius"] = pos2OrRadius
|
actualOptions[radiusStr] = pos2OrRadius
|
||||||
} else {
|
} else {
|
||||||
pos2OrRadius as Table
|
pos2OrRadius as Table
|
||||||
actualOptions["rect"] = tableOf(pos1[1L], pos1[2L], pos2OrRadius[1L], pos2OrRadius[2L])
|
actualOptions[rectStr] = tableOf(pos1[1L], pos1[2L], pos2OrRadius[1L], pos2OrRadius[2L])
|
||||||
}
|
}
|
||||||
|
|
||||||
returnBuffer.setTo(entityQueryImpl(self, actualOptions, predicate))
|
returnBuffer.setTo(entityQueryImpl(self, actualOptions, predicate))
|
||||||
@ -216,7 +228,7 @@ private fun ExecutionContext.intermediateQueryFunction(self: World<*, *>, pos1:
|
|||||||
|
|
||||||
private fun ExecutionContext.intermediateLineQueryFunction(self: World<*, *>, pos1: Table, pos2: Table, options: Table?, predicate: Predicate<AbstractEntity>) {
|
private fun ExecutionContext.intermediateLineQueryFunction(self: World<*, *>, pos1: Table, pos2: Table, options: Table?, predicate: Predicate<AbstractEntity>) {
|
||||||
val actualOptions = options ?: tableOf()
|
val actualOptions = options ?: tableOf()
|
||||||
actualOptions["line"] = tableOf(pos1, pos2)
|
actualOptions[lineStr] = tableOf(pos1, pos2)
|
||||||
returnBuffer.setTo(entityQueryImpl(self, actualOptions, predicate))
|
returnBuffer.setTo(entityQueryImpl(self, actualOptions, predicate))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,19 +73,19 @@ fun provideWorldObjectBindings(self: WorldObject, lua: LuaEnvironment) {
|
|||||||
table["level"] = luaFunction { returnBuffer.setTo(self.lookupProperty(JsonPath("level")) { JsonPrimitive(self.world.template.threatLevel) }.asDouble) }
|
table["level"] = luaFunction { returnBuffer.setTo(self.lookupProperty(JsonPath("level")) { JsonPrimitive(self.world.template.threatLevel) }.asDouble) }
|
||||||
table["toAbsolutePosition"] = luaFunction { pos: Table -> returnBuffer.setTo(from(toVector2d(pos) + self.position)) }
|
table["toAbsolutePosition"] = luaFunction { pos: Table -> returnBuffer.setTo(from(toVector2d(pos) + self.position)) }
|
||||||
|
|
||||||
table["say"] = luaFunction { line: ByteString, tags: Table?, config: Table ->
|
table["say"] = luaFunction { line: ByteString, tags: Table?, sayConfig: Table? ->
|
||||||
if (tags == null) {
|
if (tags == null) {
|
||||||
if (line.isEmpty) {
|
if (line.isEmpty) {
|
||||||
returnBuffer.setTo(false)
|
returnBuffer.setTo(false)
|
||||||
} else {
|
} else {
|
||||||
self.addChatMessage(line.decode(), config.toJson())
|
self.addChatMessage(line.decode(), sayConfig?.toJson() ?: JsonNull.INSTANCE)
|
||||||
returnBuffer.setTo(true)
|
returnBuffer.setTo(true)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (line.isEmpty) {
|
if (line.isEmpty) {
|
||||||
returnBuffer.setTo(false)
|
returnBuffer.setTo(false)
|
||||||
} else {
|
} else {
|
||||||
self.addChatMessage(SBPattern.of(line.decode()).resolveOrSkip({ tags[it]?.toString() }), config.toJson())
|
self.addChatMessage(SBPattern.of(line.decode()).resolveOrSkip({ tags[it]?.toString() }), sayConfig?.toJson() ?: JsonNull.INSTANCE)
|
||||||
returnBuffer.setTo(true)
|
returnBuffer.setTo(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user