Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Spell.{spellFive, spellFour, spellOne, spellThree, spellTwo}
- object Spell {
- case object spellOne extends Spell("one", 96, 6.5)
- case object spellTwo extends Spell("two", 54, 4.5)
- case object spellThree extends Spell("three", 48, 4.5)
- case object spellFour extends Spell("four", 48, 4.5)
- case object spellFive extends Spell("five", 36, 4.5)
- }
- class Spell(val name: String, val duration: Double, val castTime: Double) {
- var remainingDuration = 0.0
- def cast(): Double = {
- remainingDuration = duration + castTime
- println(s"Cast $name")
- castTime
- }
- def elapse(time: Double): Unit = {
- remainingDuration = Math.max(0.0, remainingDuration - time)
- }
- }
- object EQ {
- var time = 0.0
- val allSpells = List(spellOne, spellTwo, spellThree, spellFour, spellFive)
- val normalSpells = List(spellTwo, spellThree, spellFour, spellFive)
- def main(args: Array[String]): Unit = {
- while(time < 500) {
- val spell = if(allSpells.forall(s => s.remainingDuration == 0.0)) {
- normalSpells.minBy(s => s.duration - s.remainingDuration)
- } else {
- val sorted = normalSpells.sortBy(s => s.remainingDuration - s.castTime).toVector
- val currentSpell = sorted.head
- val waitTime = getWaitTime(0, sorted, currentSpell.remainingDuration - currentSpell.castTime, 0)
- if(waitTime > 0) {
- println(s"wait $waitTime")
- time += waitTime
- allSpells.foreach(s => s.elapse(waitTime))
- }
- currentSpell
- }
- val castTime = if(spellOne.remainingDuration < (spell.castTime + spellOne.castTime)) {
- val wait = spellOne.remainingDuration - spellOne.castTime
- if(wait > 0) {
- println(s"wait $wait")
- time += wait
- allSpells.foreach(s => s.elapse(wait))
- }
- spellOne.cast()
- } else {
- spell.cast()
- }
- time += castTime
- allSpells.foreach(s => s.elapse(castTime))
- println(allSpells.map(s => {
- s"${s.name}, remaining: ${s.remainingDuration}"
- }))
- }
- }
- def getWaitTime(i: Int, sorted: Vector[Spell], time: Double, extraTime: Double): Double = {
- if(time <= 0) {
- 0
- } else if(i >= sorted.length) {
- time
- } else {
- val spell = sorted(i)
- var wait = time
- while(spell.remainingDuration - wait - spell.castTime - extraTime < 0) {
- wait -= 0.5
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement