Skip to content

Commit

Permalink
Most of it gets (de)serialized now OwO
Browse files Browse the repository at this point in the history
BlueRPG-28 (de)serialization (#28)
  • Loading branch information
Bluexin committed Sep 9, 2018
1 parent c798115 commit c4e43bb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ repositories {

dependencies {
compile 'com.saomc:saoui:1.12.2-2.0.0.12-SNAPSHOT:deobf'
compile 'com.teamwizardry.librarianlib:librarianlib-1.12.2:4.15-SNAPSHOT:deobf'
compile 'com.teamwizardry.librarianlib:librarianlib-1.12.2:4.16-SNAPSHOT:deobf'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

Expand Down
27 changes: 22 additions & 5 deletions src/main/kotlin/be/bluexin/rpg/items/DebugItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import be.bluexin.saomclib.onClient
import be.bluexin.saomclib.onServer
import com.teamwizardry.librarianlib.features.base.item.ItemMod
import com.teamwizardry.librarianlib.features.kotlin.localize
import kotlinx.coroutines.experimental.channels.Channel
import com.teamwizardry.librarianlib.features.saving.AbstractSaveHandler
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiScreen
import net.minecraft.client.util.ITooltipFlag
Expand Down Expand Up @@ -110,7 +110,7 @@ object DebugSkillItem : ItemMod("debug_skill") {

override fun onItemRightClick(worldIn: World, playerIn: EntityPlayer, handIn: EnumHand): ActionResult<ItemStack> {
val stack = playerIn.getHeldItem(handIn)
if (playerIn.isSneaking) stack.itemDamage = (stack.itemDamage + 1) % 4
if (playerIn.isSneaking) stack.itemDamage = (stack.itemDamage + 1) % 5
else playerIn.activeHand = handIn

return ActionResult.newResult(EnumActionResult.SUCCESS, stack)
Expand All @@ -122,8 +122,6 @@ object DebugSkillItem : ItemMod("debug_skill") {

override fun onPlayerStoppedUsing(stack: ItemStack, worldIn: World, entityLiving: EntityLivingBase, timeLeft: Int) {
worldIn onServer {
BlueRPG.LOGGER.warn("Use ${System.currentTimeMillis()} - timeLeft: $timeLeft")
val channel = Channel<EntityLivingBase>(capacity = Channel.UNLIMITED)
when (stack.itemDamage) {
0 -> {
val p = Processor()
Expand Down Expand Up @@ -169,7 +167,26 @@ object DebugSkillItem : ItemMod("debug_skill") {
)
p.process(entityLiving)
}
else -> channel.close()
4 -> {
val p = Processor()
p.addElement(
Channelling<PlayerHolder, LivingHolder<*>>(
delayMillis = 1000,
procs = 10,
targeting = Self<PlayerHolder>().cast()
),
null,
Damage(-3.0)
)
try {
BlueRPG.LOGGER.warn("Serializing $p")
val nbt = AbstractSaveHandler.writeAutoNBT(p, false)
BlueRPG.LOGGER.warn("Result: $nbt")
BlueRPG.LOGGER.warn("Loaded: ${AbstractSaveHandler.readAutoNBTByClass(Processor::class.java, nbt, false)}")
} catch (e: Exception) {
BlueRPG.LOGGER.warn("Unable to save Processor :", e)
}
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/be/bluexin/rpg/skills/Conditions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,38 @@
package be.bluexin.rpg.skills

import be.bluexin.rpg.util.XoRoRNG
import com.teamwizardry.librarianlib.features.saving.NamedDynamic
import com.teamwizardry.librarianlib.features.saving.Savable
import kotlinx.coroutines.experimental.channels.produce
import kotlinx.coroutines.experimental.runBlocking
import net.minecraft.entity.EntityLivingBase
import net.minecraft.inventory.EntityEquipmentSlot
import net.minecraft.item.Item
import net.minecraft.potion.Potion

@Savable
@NamedDynamic("t:t")
interface Condition<TARGET: Target> {
operator fun invoke(caster: EntityLivingBase, target: TARGET): Boolean
}

@Savable
@NamedDynamic("t:l")
object IsLiving : Condition<Target> {
override fun invoke(caster: EntityLivingBase, target: Target) = target.it is EntityLivingBase

@Suppress("UNCHECKED_CAST")
val living get() = this as Condition<LivingHolder<*>>
}

@Savable
@NamedDynamic("t:h")
object HasPosition : Condition<Target> {
override fun invoke(caster: EntityLivingBase, target: Target) = target is TargetWithPosition
}

@Savable
@NamedDynamic("t:m")
data class MultiCondition<TARGET: Target>(val c1: Condition<TARGET>, val c2: Condition<TARGET>, val mode: LinkMode) : Condition<TARGET> {
override fun invoke(caster: EntityLivingBase, target: TARGET) = when (mode) {
MultiCondition.LinkMode.AND -> c1(caster, target) && c2(caster, target)
Expand All @@ -54,10 +64,14 @@ data class MultiCondition<TARGET: Target>(val c1: Condition<TARGET>, val c2: Con
}
}

@Savable
@NamedDynamic("t:i")
data class Inverted<TARGET: Target>(val c1: Condition<TARGET>) : Condition<TARGET> {
override fun invoke(caster: EntityLivingBase, target: TARGET) = !c1(caster, target)
}

@Savable
@NamedDynamic("t:r")
data class Random<TARGET: Target>(val chance: Double) : Condition<TARGET> {
private val rng = produce(capacity = 5) {
val rng = XoRoRNG()
Expand All @@ -70,14 +84,20 @@ data class Random<TARGET: Target>(val chance: Double) : Condition<TARGET> {
}
}

@Savable
@NamedDynamic("t:g")
data class RequiresGear<TARGET: TargetWithGear>(val slot: EntityEquipmentSlot, val item: Item) : Condition<TARGET> {
override fun invoke(caster: EntityLivingBase, target: TARGET) = target.getItemStackFromSlot(slot).item == item
}

@Savable
@NamedDynamic("t:s")
data class RequireStatus<TARGET: TargetWithStatus>(val status: Status) : Condition<TARGET> {
override fun invoke(caster: EntityLivingBase, target: TARGET) = target.isStatusFor(status, caster.holder)
}

@Savable
@NamedDynamic("t:p")
data class RequirePotion<TARGET: TargetWithEffects>(val effect: Potion, val level: Int = 0) : Condition<TARGET> {
override fun invoke(caster: EntityLivingBase, target: TARGET) = target.getPotionEffect(effect)?.amplifier ?: -1 >= level
}
12 changes: 12 additions & 0 deletions src/main/kotlin/be/bluexin/rpg/skills/Effects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package be.bluexin.rpg.skills

import be.bluexin.rpg.DamageHandler
import be.bluexin.rpg.util.runMainThread
import com.teamwizardry.librarianlib.features.saving.NamedDynamic
import com.teamwizardry.librarianlib.features.saving.Savable
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.ReceiveChannel
import kotlinx.coroutines.experimental.channels.produce
Expand All @@ -30,10 +32,14 @@ import net.minecraft.util.EntityDamageSource
import java.util.*
import kotlin.math.abs

@Savable
@NamedDynamic("e:e")
interface Effect<TARGET : Target> {
operator fun invoke(caster: EntityLivingBase, targets: ReceiveChannel<TARGET>)
}

@Savable
@NamedDynamic("e:d")
data class Damage<TARGET>(val value: Double) : Effect<TARGET>
where TARGET : TargetWithHealth, TARGET : TargetWithWorld { // TODO: take caster stats into account
override fun invoke(caster: EntityLivingBase, targets: ReceiveChannel<TARGET>) {
Expand All @@ -46,6 +52,8 @@ data class Damage<TARGET>(val value: Double) : Effect<TARGET>
}
}

@Savable
@NamedDynamic("e:b") // TODO: (de)serialization of PotionEffects
data class Buff<TARGET>(val effect: PotionEffect) : Effect<TARGET>
where TARGET : TargetWithEffects, TARGET : TargetWithWorld {
override fun invoke(caster: EntityLivingBase, targets: ReceiveChannel<TARGET>) {
Expand All @@ -57,6 +65,8 @@ data class Buff<TARGET>(val effect: PotionEffect) : Effect<TARGET>
}
}

@Savable
@NamedDynamic("e:s")
data class Skill<TARGET: Target, RESULT: Target>(val targeting: Targeting<TARGET, RESULT>, val effect: Effect<RESULT>) : Effect<TARGET> {
override fun invoke(caster: EntityLivingBase, targets: ReceiveChannel<TARGET>) {
launch {
Expand Down Expand Up @@ -96,6 +106,8 @@ data class Skill<TARGET: Target, RESULT: Target>(val targeting: Targeting<TARGET
}
}

@Savable
@NamedDynamic("e:m")
data class MultiEffect<TARGET: Target>(val effects: Array<Effect<TARGET>>) : Effect<TARGET> {

override fun invoke(caster: EntityLivingBase, targets: ReceiveChannel<TARGET>) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/be/bluexin/rpg/skills/Processor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

package be.bluexin.rpg.skills

import com.teamwizardry.librarianlib.features.saving.Savable
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.produce
import net.minecraft.entity.EntityLivingBase
import java.util.*

@Savable
class Processor {
private val chain = LinkedList<Triple<Targeting<Target, Target>, Condition<Target>?, Effect<Target>>>()
private var chain = LinkedList<Triple<Targeting<Target, Target>, Condition<Target>?, Effect<Target>>>()

fun process(caster: EntityLivingBase) {
chain.forEach { (t, c, e) ->
Expand All @@ -37,4 +39,8 @@ class Processor {
@Suppress("UNCHECKED_CAST")
chain.add(Triple(targeting as Targeting<Target, Target>, condition as Condition<Target>?, effect as Effect<Target>))
}

override fun toString(): String {
return "Processor(chain=$chain)"
}
}
30 changes: 25 additions & 5 deletions src/main/kotlin/be/bluexin/rpg/skills/Targeting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import be.bluexin.rpg.util.runMainThread
import com.google.common.base.Predicate
import com.teamwizardry.librarianlib.features.kotlin.minus
import com.teamwizardry.librarianlib.features.kotlin.plus
import com.teamwizardry.librarianlib.features.saving.NamedDynamic
import com.teamwizardry.librarianlib.features.saving.Savable
import com.teamwizardry.librarianlib.features.utilities.RaycastUtils
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.SendChannel
import kotlinx.coroutines.experimental.channels.produce
Expand All @@ -38,11 +41,17 @@ import net.minecraft.util.math.Vec3d
import java.lang.StrictMath.pow
import java.util.*

// TODO Some of these need to be ported to the new system to handle targeting both blocks & entities

@Savable
@NamedDynamic("t:t")
interface Targeting<FROM : Target, RESULT : Target> {
operator fun invoke(caster: EntityLivingBase, from: FROM, result: SendChannel<RESULT>)
val range: Double
}

@Savable
@NamedDynamic("t:p")
data class Projectile<FROM, RESULT>(
override val range: Double = 15.0, val velocity: Float = 1f, val inaccuracy: Float = 1f,
val condition: Condition<RESULT>? = null
Expand All @@ -59,6 +68,8 @@ data class Projectile<FROM, RESULT>(
}
}

@Savable
@NamedDynamic("t:s")
data class Self<T : Target>(val unused: Boolean = false) : Targeting<T, T> {
override operator fun invoke(caster: EntityLivingBase, from: T, result: SendChannel<T>) {
result.offerOrSendAndClose(from)
Expand All @@ -67,23 +78,26 @@ data class Self<T : Target>(val unused: Boolean = false) : Targeting<T, T> {
override val range: Double
get() = 0.0

fun <FROM: T, RESULT: Target> cast() = this as Targeting<FROM, RESULT>
fun <FROM : T, RESULT : Target> cast() = this as Targeting<FROM, RESULT>
}

@Savable
@NamedDynamic("t:r")
data class Raycast<FROM, RESULT>(
override val range: Double = 3.0, val condition: Condition<RESULT>? = null
) : Targeting<FROM, RESULT> where FROM : TargetWithLookVec, FROM : TargetWithPosition, FROM : TargetWithWorld,
RESULT : TargetWithPosition, RESULT : TargetWithWorld {
override operator fun invoke(caster: EntityLivingBase, from: FROM, result: SendChannel<RESULT>) {
launch {
// val e = RaycastUtils.getEntityLookedAt(from, range)
// if (e is EntityLivingBase) result.send(e)
TODO("Port to new system")
val e = RaycastUtils.getEntityLookedAt((from as LivingHolder<*>).it, range)
if (e is EntityLivingBase) result.send(e.holder as RESULT)
result.close()
}
}
}

@Savable
@NamedDynamic("t:c")
data class Channelling<FROM : Target, RESULT : Target>(
val delayMillis: Long, val procs: Int, val targeting: Targeting<FROM, RESULT>
) : Targeting<FROM, RESULT> by targeting {
Expand Down Expand Up @@ -124,6 +138,8 @@ data class Channelling<FROM : Target, RESULT : Target>(
}
}

@Savable
@NamedDynamic("t:a")
data class AoE<FROM, RESULT>(
override val range: Double = 3.0, val shape: Shape = Shape.CIRCLE
) : Targeting<FROM, RESULT> where FROM : TargetWithLookVec, FROM : TargetWithPosition, FROM : TargetWithWorld,
Expand Down Expand Up @@ -151,6 +167,8 @@ data class AoE<FROM, RESULT>(
}
}

@Savable
@NamedDynamic("t:i")
data class Chain<T>(
override val range: Double = 3.0, val maxTargets: Int = 5, val delayMillis: Long = 500, val repeat: Boolean = false,
val condition: Condition<T>? = null
Expand All @@ -173,7 +191,9 @@ data class Chain<T>(
h != previousTarget && previousTarget.getDistanceSq(h) <= dist &&
(repeat || h !in targets) && (condition == null || condition!!(caster, h))
}
} catch (_: Exception) { listOf<EntityLivingBase>() }
} catch (_: Exception) {
listOf<EntityLivingBase>()
}
val e = es.minBy { previousTarget.getDistanceSq(LivingHolder(it!!)) }
if (e != null) {
val h = LivingHolder(e) as T
Expand Down

0 comments on commit c4e43bb

Please sign in to comment.