Loading item from module using Groovy

This commit is contained in:
Song Minjae
2017-04-26 23:53:22 +09:00
parent 05c9c8f5df
commit 49d3c9f55b
27 changed files with 291 additions and 67 deletions

View File

@@ -5,39 +5,14 @@
<configuration SdkName="LuaJ" />
</facet>
<facet type="kotlin-language" name="Kotlin">
<configuration version="1">
<option name="compilerInfo">
<KotlinCompilerInfo>
<option name="compilerSettings">
<CompilerSettings />
</option>
<option name="k2jsCompilerArguments">
<K2JSCompilerArguments>
<option name="moduleKind" value="plain" />
</K2JSCompilerArguments>
</option>
<option name="k2jvmCompilerArguments">
<K2JVMCompilerArguments>
<option name="jvmTarget" value="1.6" />
</K2JVMCompilerArguments>
</option>
<option name="_commonCompilerArguments">
<DummyImpl>
<option name="languageVersion" value="1.1" />
<option name="apiVersion" value="1.1" />
<option name="coroutinesEnable" value="true" />
</DummyImpl>
</option>
</KotlinCompilerInfo>
</option>
<option name="useProjectSettings" value="false" />
<option name="versionInfo">
<KotlinVersionInfo>
<option name="apiLevel" value="1.1" />
<option name="languageLevel" value="1.1" />
<option name="targetPlatformName" value="JVM 1.8" />
</KotlinVersionInfo>
</option>
<configuration version="2" platform="JVM 1.6" useProjectSettings="false">
<compilerSettings />
<compilerArguments>
<option name="jvmTarget" value="1.6" />
<option name="languageVersion" value="1.1" />
<option name="apiVersion" value="1.1" />
<option name="coroutinesEnable" value="true" />
</compilerArguments>
</configuration>
</facet>
</component>
@@ -55,5 +30,6 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lib" level="project" />
<orderEntry type="library" name="groovy-2.4.10" level="application" />
</component>
</module>

View File

@@ -1,9 +1,10 @@
# Load order
# Load Order
# Modules are loaded from top to bottom.
# And yes, you can disable basegame, but we don't recommend.
# Acceptable formats:
# module_name,description_in_English_no_comma,
# module_name,description_in_English_no_comma,(external Jar 1);(external Jar 2); ...
# module_name,description_in_English_no_comma,(entry_point.kts),(external Jars sep'd by semicolon)...
# If entry_point is not given, the program will try to run <modulename>.kts.
# if even that does not exist, an the program will quit with error.
# and yes, you can disable basegame, but we don't recommend.
basegame,The base game,
dwarventech,Dwarven technicians are emerged,
basegame,The base game,basegame.groovy,
dwarventech,Dwarven technicians are emerged from their hiding,,
1 # Load order # Load Order
2 # Modules are loaded from top to bottom. # Modules are loaded from top to bottom.
# And yes, you can disable basegame, but we don't recommend.
3 # Acceptable formats: # Acceptable formats:
4 # module_name,description_in_English_no_comma, # module_name,description_in_English_no_comma,(entry_point.kts),(external Jars sep'd by semicolon)...
5 # module_name,description_in_English_no_comma,(external Jar 1);(external Jar 2); ... # If entry_point is not given, the program will try to run <modulename>.kts.
6 # if even that does not exist, an the program will quit with error.
7 # and yes, you can disable basegame, but we don't recommend.
8 basegame,The base game, basegame,The base game,basegame.groovy,
9 dwarventech,Dwarven technicians are emerged, dwarventech,Dwarven technicians are emerged from their hiding,,
10

View File

@@ -0,0 +1 @@
print "Hello, world!"

View File

@@ -0,0 +1,9 @@
import net.torvald.terrarum.ModMgr
/**
* Created by SKYHi14 on 2017-04-26.
*/
static void invoke(String module) {
ModMgr.GameItemLoader.invoke(module)
}

View File

@@ -1,2 +1,2 @@
"id";"filename"
"8448";"testpick.lua"
"8448";"testpick.groovy"
1 id filename
2 8448 testpick.lua testpick.groovy

View File

@@ -0,0 +1,97 @@
/**
* Created by SKYHi14 on 2017-04-26.
*/
import net.torvald.point.Point2d
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameactors.ActorWithPhysics
import net.torvald.terrarum.itemproperties.Calculate
import net.torvald.terrarum.itemproperties.InventoryItem
import net.torvald.terrarum.itemproperties.Material
import net.torvald.terrarum.tileproperties.Tile
import org.jetbrains.annotations.NotNull
import org.newdawn.slick.GameContainer
static InventoryItem invoke(int id) {
return new TestPick(id)
}
class TestPick extends InventoryItem {
int originalID
int dynamicID
String originalName = "GROOVY_PICK"
double baseMass = 10.0
Double baseToolSize = 10.0
boolean stackable = true
int maxDurability = 147
float durability = maxDurability
int equipPosition = 9 //EquipPosition.HAND_GRIP
String inventoryCategory = "tool" //Category.TOOL
// !! TEST MATERIAL !!
Material material = new Material(0,0,0,0,0,0,0,0,1,0.0)
TestPick(int id) {
originalID = id
dynamicID = id
name = "Groovy Pickaxe"
}
@Override
boolean isUnique() {
return false
}
@Override
boolean isDynamic() {
return true
}
@Override
boolean primaryUse(@NotNull GameContainer gc, int delta) {
int mouseTileX = Terrarum.getMouseTileX()
int mouseTileY = Terrarum.getMouseTileY()
def mousePoint = new Point2d(mouseTileX, mouseTileY)
def actorvalue = Terrarum.ingame.player.actorValue
using = true
// linear search filter (check for intersection with tilewise mouse point and tilewise hitbox)
// return false if hitting actors
Terrarum.ingame.actorContainer.forEach({
if (it instanceof ActorWithPhysics && it.tilewiseHitbox.intersects(mousePoint))
return false
})
// return false if here's no tile
if (Tile.AIR == Terrarum.ingame.world.getTileFromTerrain(mouseTileX, mouseTileY))
return false
// filter passed, do the job
double swingDmgToFrameDmg = delta.toDouble() / actorvalue.getAsDouble(AVKey.ACTION_INTERVAL)
Terrarum.ingame.world.inflictTerrainDamage(
mouseTileX, mouseTileY,
Calculate.pickaxePower(Terrarum.ingame.player, material) * swingDmgToFrameDmg
)
return true
}
@Override
boolean endPrimaryUse(@NotNull GameContainer gc, int delta) {
using = false
// reset action timer to zero
Terrarum.ingame.player.actorValue.set(AVKey.__ACTION_TIMER, 0.0)
return true
}
}

View File

@@ -6,10 +6,14 @@
"argn"; "9"; "0.91";"argentum/silver"
"stal"; "14"; "1.73";"steel"
"eaur"; "21"; "1.36";"elven aurichalcum"
"tial"; "33"; "2.16";"titanium alloy"
"tial"; "33"; "2.16";"titanium alloy (Ti6Al4V)"
"admt"; "71"; "3.42";"adamant"
# idst: ID_STRING
# forcemod: related to attack points
# Attack points = `4 * forcemod.sqrt()` for each strike
# endurance: multiplier, using copper as reference; determines durability of tools/weapons/armours/etc.
# Comments: do nothing; do not parse
1 idst forcemod endurance comments
6 argn 9 0.91 argentum/silver
7 stal 14 1.73 steel
8 eaur 21 1.36 elven aurichalcum
9 tial 33 2.16 titanium alloy titanium alloy (Ti6Al4V)
10 admt 71 3.42 adamant
11 # forcemod: related to attack points # idst: ID_STRING
12 # forcemod: related to attack points
13 # Attack points = `4 * forcemod.sqrt()` for each strike
14 # Attack points = `4 * forcemod.sqrt()` for each strike # endurance: multiplier, using copper as reference determines durability of tools/weapons/armours/etc.
15 # endurance: multiplier, using copper as reference # Comments: do nothing determines durability of tools/weapons/armours/etc. do not parse
16
17
18
19

View File

@@ -0,0 +1,3 @@
println("Hello, world!!")
return 42

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,4 +1,4 @@
package net.torvald
package net.torvald.dataclass
import java.util.*

View File

@@ -2,7 +2,6 @@ package net.torvald.terrarum
import net.torvald.terrarum.gameactors.ActorHumanoid
import net.torvald.terrarum.itemproperties.InventoryItem
import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.ui.UIVitalMetre
/**

View File

@@ -1,21 +1,41 @@
package net.torvald.terrarum
import net.torvald.CSVFetcher
import net.torvald.terrarum.itemproperties.InventoryItem
import net.torvald.terrarum.itemproperties.ItemCodex
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
import java.io.File
import java.io.FileNotFoundException
import java.io.FileReader
import java.nio.file.FileSystems
import javax.script.ScriptEngineManager
import javax.script.Invocable
/**
* Modules Resource Manager
*
*
* NOTE!!: Usage of Groovy is only temporary; if Kotlin's "JSR 223" is no longer experimental and
* is readily available, ditch that Groovy.
*
*
* Created by SKYHi14 on 2017-04-17.
*/
object ModMgr {
data class ModuleMetadata(val order: Int, val isDir: Boolean, val desc: String, val libraries: Array<String>) {
data class ModuleMetadata(
val order: Int,
val isDir: Boolean,
val desc: String,
val entryPoint: String,
val libraries: Array<String>
) {
override fun toString() =
"\tModule #$order -- $desc\n" +
"\tEntry point: $entryPoint\n" +
"\tExternal libraries: ${libraries.joinToString(", ")}"
}
const val modDir = "./assets/modules"
@@ -38,12 +58,46 @@ object ModMgr {
println("[ModMgr] Loading module $moduleName")
val description = it[1]
val libs = it[2].split(';').toTypedArray()
val entryPoint = it[2]
val libs = it[3].split(';').toTypedArray()
val isDir = FileSystems.getDefault().getPath("$modDir/$moduleName").toFile().isDirectory
moduleInfo[moduleName] = ModuleMetadata(index, isDir, description, libs)
moduleInfo[moduleName] = ModuleMetadata(index, isDir, description, entryPoint, libs)
println(moduleInfo[moduleName])
// run entry script in entry point
if (entryPoint.isNotBlank()) {
val extension = entryPoint.split('.').last()
val engine = ScriptEngineManager().getEngineByExtension(extension)!!
val invocable = engine as Invocable
engine.eval(FileReader(getFile(moduleName, entryPoint)))
invocable.invokeFunction("invoke", moduleName)
}
println("[ModMgr] $moduleName loaded successfully")
}
/*val manager = ScriptEngineManager()
val factories = manager.engineFactories
for (f in factories) {
println("engine name:" + f.engineName)
println("engine version:" + f.engineVersion)
println("language name:" + f.languageName)
println("language version:" + f.languageVersion)
println("names:" + f.names)
println("mime:" + f.mimeTypes)
println("extension:" + f.extensions)
println("-----------------------------------------------")
}*/
/*val engine = ScriptEngineManager().getEngineByExtension("groovy")!!
engine.eval(FileReader(getFile("basegame", "/items/testpick.groovy")))
val newPick = (engine as Invocable).invokeFunction("invoke", 8449) as InventoryItem
ItemCodex[8449] = newPick*/
}
private fun checkExistence(module: String) {
@@ -74,4 +128,27 @@ object ModMgr {
return dir.listFiles()
}
}
object GameItemLoader {
val itemPath = "items/"
@JvmStatic operator fun invoke(module: String) {
val engine = ScriptEngineManager().getEngineByExtension("groovy")!!
val invocable = engine as Invocable
val csv = CSVFetcher.readFromModule(module, itemPath + "itemid.csv")
csv.forEach {
val filename = it["filename"].toString()
val script = getFile(module, itemPath + filename).readText()
val itemID = it["id"].toInt()
engine.eval(script)
ItemCodex[itemID] = invocable.invokeFunction("invoke", itemID) as InventoryItem
}
}
}
}

View File

@@ -69,7 +69,7 @@ class StateInGame : BasicGameState() {
var playableActorDelegate: PlayableActorDelegate? = null // DO NOT LATEINIT!
private set
internal val player: ActorHumanoid? // currently POSSESSED actor :)
val player: ActorHumanoid? // currently POSSESSED actor :)
get() = playableActorDelegate?.actor
var screenZoom = 1.0f

View File

@@ -4,6 +4,7 @@ import net.torvald.terrarum.gameactors.*
import net.torvald.terrarum.itemproperties.IVKey
import net.torvald.terrarum.itemproperties.InventoryItem
import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.itemproperties.Material
import net.torvald.terrarum.tileproperties.Tile
import net.torvald.terrarum.ui.*
import org.newdawn.slick.*
@@ -58,6 +59,7 @@ class StateUITest : BasicGameState() {
override var durability: Float = 64f
override var stackable = false
override val isDynamic = true
override val material = Material(0,0,0,0,0,0,0,0,0,0.0)
})
actor.inventory.getByDynamicID(5656)!!.item.name = "Test tool"
@@ -74,6 +76,7 @@ class StateUITest : BasicGameState() {
override var inventoryCategory: String = InventoryItem.Category.MISC
override var stackable = false
override val isDynamic = false
override val material = Material(0,0,0,0,0,0,0,0,0,0.0)
})
actor.inventory.add(ItemCodex[16], 543)

View File

@@ -7,6 +7,8 @@ import net.torvald.imagefont.GameFontImpl
import net.torvald.JsonFetcher
import net.torvald.JsonWriter
import net.torvald.imagefont.TinyAlphNum
import net.torvald.terrarum.gamecontroller.mouseTileX
import net.torvald.terrarum.gamecontroller.mouseTileY
import net.torvald.terrarum.gameworld.toUint
import org.lwjgl.input.Controllers
import org.lwjgl.opengl.*
@@ -500,6 +502,12 @@ object Terrarum : StateBasedGame(GAME_NAME) {
return file // TODO TEST CODE
}
// for external scripts (e.g. Groovy)
@JvmStatic fun getMouseTileX(): Int = appgc.mouseTileX
@JvmStatic fun getMouseTileY(): Int = appgc.mouseTileY
}
fun main(args: Array<String>) {

View File

@@ -6,6 +6,7 @@ import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.faction.Faction
import net.torvald.terrarum.gamecontroller.EnumKeyFunc
import net.torvald.terrarum.itemproperties.InventoryItem
import net.torvald.terrarum.itemproperties.Material
import net.torvald.terrarum.realestate.LandUtil
import org.newdawn.slick.GameContainer
import org.newdawn.slick.Input
@@ -147,6 +148,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
override val originalName: String = actorValue.getAsString(AVKey.NAME) ?: "(no name)"
override var stackable = false
override val isDynamic = false
override val material = Material(0,0,0,0,0,0,0,0,0,0.0)
}
override fun update(gc: GameContainer, delta: Int) {

View File

@@ -8,6 +8,7 @@ import net.torvald.terrarum.gameactors.ai.LuaAIWrapper
import net.torvald.terrarum.gamecontroller.mouseX
import net.torvald.terrarum.gamecontroller.mouseY
import net.torvald.terrarum.itemproperties.InventoryItem
import net.torvald.terrarum.itemproperties.Material
import org.luaj.vm2.*
import org.luaj.vm2.compiler.LuaC
import org.luaj.vm2.lib.*
@@ -59,6 +60,7 @@ open class HumanoidNPC(
override val originalName: String = actorValue.getAsString(AVKey.NAME) ?: "NPC"
override var stackable = true
override val isDynamic = false
override val material = Material(0,0,0,0,0,0,0,0,0,0.0)
override fun secondaryUse(gc: GameContainer, delta: Int): Boolean {
try {

View File

@@ -230,7 +230,8 @@ class GameWorld(val width: Int, val height: Int) {
/**
* @return true if block is broken
*/
fun inflctTerrainDamage(x: Int, y: Int, damage: Float): Boolean {
fun inflictTerrainDamage(x: Int, y: Int, damage: Double): Boolean {
val damage = damage.toFloat()
val addr = LandUtil.getTileAddr(x, y)
//println("[GameWorld] ($x, $y) Damage: $damage")
@@ -261,7 +262,8 @@ class GameWorld(val width: Int, val height: Int) {
/**
* @return true if block is broken
*/
fun inflctWallDamage(x: Int, y: Int, damage: Float): Boolean {
fun inflictWallDamage(x: Int, y: Int, damage: Double): Boolean {
val damage = damage.toFloat()
val addr = LandUtil.getTileAddr(x, y)
if (wallDamages[addr] == null) { // add new

View File

@@ -15,7 +15,7 @@ object Calculate {
*
* TODO Newtons as unit?
*/
fun pickaxePower(actor: ActorHumanoid, material: Material): Float {
@JvmStatic fun pickaxePower(actor: ActorHumanoid, material: Material): Float {
return (4.0 * material.forceMod.toDouble().sqrt() * (actor.avStrength / 1000.0)).toFloat()
}

View File

@@ -75,7 +75,7 @@ abstract class InventoryItem : Comparable<InventoryItem>, Cloneable {
*/
open val equipPosition: Int = EquipPosition.NULL
open val material: Material? = null
abstract val material: Material
/**
* Apparent mass of the item. (basemass * scale^3)

View File

@@ -3,13 +3,11 @@ package net.torvald.terrarum.itemproperties
import net.torvald.point.Point2d
import net.torvald.terrarum.KVHashMap
import net.torvald.terrarum.gameactors.CanBeAnItem
import net.torvald.terrarum.itemproperties.InventoryItem
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameactors.ActorWithPhysics
import net.torvald.terrarum.gamecontroller.mouseTileX
import net.torvald.terrarum.gamecontroller.mouseTileY
import net.torvald.terrarum.itemproperties.IVKey
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.mapdrawer.TilesDrawer
import net.torvald.terrarum.mapdrawer.TilesDrawer.wallOverlayColour
@@ -56,6 +54,7 @@ object ItemCodex {
override var stackable = true
override var inventoryCategory = Category.BLOCK
override var isDynamic = false
override val material = Material(0,0,0,0,0,0,0,0,0,0.0)
init {
itemProperties[IVKey.ITEMTYPE] = if (i in ITEM_TILES)
@@ -104,7 +103,7 @@ object ItemCodex {
}
// test copper pickaxe
itemCodex[ITEM_STATIC.first] = object : InventoryItem() {
/*itemCodex[ITEM_STATIC.first] = object : InventoryItem() {
override val originalID = ITEM_STATIC.first
override var dynamicID = originalID
override val isUnique = false
@@ -117,11 +116,7 @@ object ItemCodex {
override var equipPosition = EquipPosition.HAND_GRIP
override var inventoryCategory = Category.TOOL
override val isDynamic = true
private val testmaterial = Material(
0,0,0,0,0,0,0,0,1,0.0 // quick test material Stone
)
override val material = Material(0,0,0,0,0,0,0,0,1,0.0)
init {
itemProperties[IVKey.ITEMTYPE] = IVKey.ItemType.PICK
@@ -150,10 +145,10 @@ object ItemCodex {
// filter passed, do the job
val swingDmgToFrameDmg = delta.toDouble() / actorvalue.getAsDouble(AVKey.ACTION_INTERVAL)!!
Terrarum.ingame!!.world.inflctTerrainDamage(
Terrarum.ingame!!.world.inflictTerrainDamage(
gc.mouseTileX,
gc.mouseTileY,
Calculate.pickaxePower(Terrarum.ingame!!.player!!, testmaterial) * swingDmgToFrameDmg.toFloat()
Calculate.pickaxePower(Terrarum.ingame!!.player!!, material) * swingDmgToFrameDmg
)
return true
}
@@ -164,9 +159,8 @@ object ItemCodex {
Terrarum.ingame!!.player!!.actorValue[AVKey.__ACTION_TIMER] = 0.0
return true
}
}
}*/
// TODO read prop in Lua and fill itemCodex
// read from save (if applicable) and fill dynamicItemDescription
}
@@ -188,6 +182,13 @@ object ItemCodex {
}
}
/**
* Mainly used by GameItemLoader
*/
operator fun set(code: Int, item: InventoryItem) {
itemCodex[code] = item
}
fun getItemImage(item: InventoryItem): Image {
// terrain
if (item.originalID in ITEM_TILES) {

View File

@@ -47,13 +47,13 @@ class ItemEffectsLuaAPI(g: Globals) {
class StrikeEarth : ThreeArgFunction() {
override fun call(x: LuaValue, y: LuaValue, power: LuaValue): LuaValue {
Terrarum.ingame!!.world.inflctTerrainDamage(x.checkint(), y.checkint(), power.checkdouble().toFloat())
Terrarum.ingame!!.world.inflictTerrainDamage(x.checkint(), y.checkint(), power.checkdouble())
return LuaValue.NONE
}
}
class StrikeWall : ThreeArgFunction() {
override fun call(x: LuaValue, y: LuaValue, power: LuaValue): LuaValue {
Terrarum.ingame!!.world.inflctWallDamage(x.checkint(), y.checkint(), power.checkdouble().toFloat())
Terrarum.ingame!!.world.inflictWallDamage(x.checkint(), y.checkint(), power.checkdouble())
return LuaValue.NONE
}
}

View File

@@ -1,6 +1,6 @@
package net.torvald.terrarum.mapgenerator
import net.torvald.IntArrayStack
import net.torvald.dataclass.IntArrayStack
import net.torvald.colourutil.Col4096
import net.torvald.random.HQRNG
import org.newdawn.slick.Color

View File

@@ -17,7 +17,7 @@ constructor(override var width: Int, isBlackVariant: Boolean) : UICanvas {
private var segmentRight: Image? = null
private var segmentBody: Image? = null
private lateinit var messagesList: Array<String>
var messagesList: Array<String>
override var height: Int = 0
private val messageWindowRadius: Int

View File

@@ -0,0 +1,39 @@
# Package BASEGAME
## Tools
- Pickaxe -- all 9 tool materials
- Hatchet -- all 9 tool materials
## Weapons
- Bows
- Longbow
- Hunting bow
-- TODO discriminate two above
- Halberd
- all 8 metals
- Sword
- all 8 metals
- neolithic polished knife
## Armour
- Headgear
- all 8 metals
- Body
- all 8 metals
- Boots
- all 8 metals
## Misc.
- Dye bottle -- Game will use this master to generate 64 dyes
- Generic tapestry
- Basic loom (16/64 col, print on the fly, no undo, can only use dyes you currently have)
# Package DWARVENTECH
## Misc.
- Loom with computer (16/64 col, internal editor that "prints" when design is completed, undo/redo, save/load
can use all colours, shows how much dyes you need to "print out")