still wip modularisation, game somehow boots

This commit is contained in:
minjaesong
2018-06-21 17:33:22 +09:00
parent f0a6f8b9c2
commit a6ea2b4e18
266 changed files with 2409 additions and 1122 deletions

View File

@@ -0,0 +1,147 @@
package net.torvald.terrarum.modulebasegame.magiccontroller
import java.util.*
/**
* YE OLDE MAGIC IDEA No.0
*
* Provides MDL interpretation, pre-compilation and stores state of the interpreter
*
* Created by minjaesong on 2016-07-30.
*/
class MDLInterpreterState {
val stack = MagicArrayStack(20)
fun interpret(line: String) {
}
fun execute(property: MagicWords, power: MagicWords? = null, arg: Int? = null) {
}
enum class MagicWords {
// properties
ELDR, IS, STORMR, HREYFING, LAEKNING, GLEYPI, TJON,
//fire, ice, storm, kinesis, heal, absorb, harm
// reserved words
LAEKNINGHRADI, HREYFINGHRADI, LAEKNINGAUKI, HREYFINGAUKI, STOEKKAUKI, HEILSASTIG,
// heal rate,movement speed, healratemult,movespeedmult, jump boost, health point
// adjectives (power)
// operators
ITA, TOGA, PLUS, MINUS, SINNUM, DEILING, LEIFASTOFN, AFRIT, STAFLISKIPTI, HENNA, NA
// push, pop, +, -, *, /, %, dup, swap, drop, fetch
}
class MagicArrayStack {
/**
* Number of elements in the stack
*/
var depth: Int = 0
private set
var size: Int
get() = data.size
set(newSize) {
if (newSize > depth) inflate(newSize - data.size)
else deflate(data.size - newSize)
}
private lateinit var data: Array<Int?>
constructor(stackSize: Int) {
data = Array(stackSize, { null })
}
constructor(arr: Array<Int?>) {
data = arr.copyOf()
depth = size
}
fun push(v: Int) {
if (depth >= data.size) throw StackOverflowError()
data[depth++] = v
}
fun pop(): Int {
if (depth == 0) throw EmptyStackException()
return data[--depth]!!
}
fun peek(): Int? {
if (depth == 0) return null
return data[depth - 1]
}
fun dup() {
if (depth == 0) throw EmptyStackException()
if (depth == data.size) throw StackOverflowError()
push(peek()!!)
}
fun swap() {
if (depth < 2) throw UnsupportedOperationException("Stack is empty or has only one element.")
val up = pop()
val dn = pop()
push(up)
push(dn)
}
fun drop() {
if (depth == 0) throw EmptyStackException()
--depth
}
fun defineFromArray(arr: Array<Int?>) { data = arr.copyOf() }
/**
* Increase the stack size by a factor.
*/
fun inflate(sizeToAdd: Int) {
if (sizeToAdd < 0) throw UnsupportedOperationException("$sizeToAdd: Cannot deflate the stack with this function. Use deflate(int) instead.")
size += sizeToAdd
val oldStack = this.asArray()
data = Array(size, { if (it < oldStack.size) oldStack[it] else null })
}
/**
* Decrease the stack size by a factor. Overflowing data will be removed.
*/
fun deflate(sizeToTake: Int) {
if (size - sizeToTake < 1) throw UnsupportedOperationException("$sizeToTake: Cannot deflate the stack to the size of zero or negative.")
size -= sizeToTake
val oldStack = this.asArray()
data = Array(size, { oldStack[it] })
if (depth > data.size) depth = data.size
}
/**
* Convert stack as array. Index zero is the bottommost element.
* @return array of data, with array size equivalent to the stack depth.
*/
fun asArray() = data.copyOfRange(0, depth - 1)
fun equalTo(other: MagicArrayStack) = (this.asArray() == other.asArray())
fun plus() { data[depth - 2] = data[depth - 2]!! + (pop().toInt()) }
fun minus() { data[depth - 2] = data[depth - 2]!! - (pop().toInt()) }
fun times() { data[depth - 2] = data[depth - 2]!! * (pop().toInt()) }
fun div() { data[depth - 2] = data[depth - 2]!! / (pop().toInt()) }
fun mod() { data[depth - 2] = data[depth - 2]!! % (pop().toInt()) }
}
}

View File

@@ -0,0 +1,109 @@
package net.torvald.terrarum.modulebasegame.magiccontroller
import net.torvald.terrarum.modulebasegame.gameactors.AVKey
import net.torvald.terrarum.gameactors.Actor
/**
* "Data Type" describing magical force
*
* Created by minjaesong on 2018-06-03.
*/
class TheMagicLanguage(vm: TheMagicMachine) {
open class MagicException : Exception()
class MagicPortReadError : MagicException()
class MagicPortWriteError : MagicException()
/**
* A vessel contains magical power to be process.
*
* Negative numbers are tsraoatrsed as if it's negative power (still has >0 amount)
*/
class MagicAccumulator {
private var power = 0.0
fun pourIn(value: Double) {
power += value // positive powers and negative powers will cancel eath other
}
fun pourOut(value: Double) {
power -= value
}
fun pourOutInto(other: MagicAccumulator, value: Double) {
if (power >= 0) {
// pour out positive power without inversion; result is positive power
if (value >= 0) {
val value = minOf(power, value)
other.pourIn(value)
power -= value
}
// pour out positive power with inversion; result is negative power
else {
val value = minOf(-power, value)
other.pourIn(value)
power += value
}
}
else {
// pour out negative power without inversion; result is negative power
if (value < 0) {
val value = minOf(power, value)
other.pourIn(-value)
}
// pour out negative power with inversion; result is positive power
else {
val value = minOf(-power, value)
other.pourIn(-value)
}
}
}
fun dumpAllInto(other: MagicAccumulator) {
pourOutInto(other, this.power)
}
fun empty() {
// release residual power as heat or something
power = 0.0
}
fun readForPortWrite(): Double {
val r = power
power = 0.0
return r
}
}
interface MagicOutputPort {
fun read(a: MagicAccumulator): Double?
fun write(a: MagicAccumulator)
}
class HealthPort(val output1: Actor) : MagicOutputPort {
override fun read(a: MagicAccumulator): Double {
val value = output1.actorValue.getAsDouble(AVKey.HEALTH) ?: throw MagicPortReadError()
a.pourIn(value)
return value
}
override fun write(a: MagicAccumulator) {
val value = output1.actorValue.getAsDouble(AVKey.HEALTH) ?: throw MagicPortReadError()
output1.actorValue[AVKey.HEALTH] = value + a.readForPortWrite()
}
}
fun opCombine(a: MagicAccumulator, b: MagicAccumulator, c: MagicAccumulator) {
b.dumpAllInto(a)
c.dumpAllInto(a)
}
fun opRelease(akku: MagicAccumulator, port: MagicOutputPort) {
port.write(akku)
}
fun opSiphon(akku: MagicAccumulator, port: MagicOutputPort) {
port.read(akku)
}
}

View File

@@ -0,0 +1,18 @@
package net.torvald.terrarum.modulebasegame.magiccontroller
/**
* Created by minjaesong on 2018-06-03.
*/
class TheMagicMachine(skill: MagicianSkillDefinition) {
val akkuPack = Array(skill.numberOfAccumulator, { TheMagicLanguage.MagicAccumulator() })
}
data class MagicianSkillDefinition(
var numberOfAccumulator: Int,
var useableMagicPower: Double,
var runeComprehensionLevel: Int
)