Skip to content

Commit

Permalink
feat(mindustry): Added custom day and night cycle (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Jan 18, 2025
1 parent a882f10 commit 1b51a90
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ data class MindustryConfig(
val displayCoreId: Boolean = true,
val displayResourceTracker: Boolean = true,
val explosiveDamageAlertDelay: Duration = 15.seconds,
val dayNightCycleDuration: Duration = 20.minutes,
)

data class Security(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.xpdustry.imperium.mindustry.formation.FormationListener
import com.xpdustry.imperium.mindustry.game.AlertListener
import com.xpdustry.imperium.mindustry.game.AntiGriefListener
import com.xpdustry.imperium.mindustry.game.ChangelogCommand
import com.xpdustry.imperium.mindustry.game.DayNighCycleListener
import com.xpdustry.imperium.mindustry.game.GameListener
import com.xpdustry.imperium.mindustry.game.ImperiumLogicListener
import com.xpdustry.imperium.mindustry.game.LogicListener
Expand Down Expand Up @@ -198,6 +199,7 @@ class ImperiumPlugin : AbstractMindustryPlugin() {
FlexListener::class,
MetricsListener::class,
ChangelogCommand::class,
DayNighCycleListener::class,
)
.forEach(application::register)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Imperium, the software collection powering the Chaotic Neutral network.
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.imperium.mindustry.game

import arc.math.Interp
import com.xpdustry.distributor.api.annotation.EventHandler
import com.xpdustry.distributor.api.annotation.TaskHandler
import com.xpdustry.distributor.api.scheduler.MindustryTimeUnit
import com.xpdustry.imperium.common.application.ImperiumApplication
import com.xpdustry.imperium.common.config.ImperiumConfig
import com.xpdustry.imperium.common.inject.InstanceManager
import com.xpdustry.imperium.common.inject.get
import com.xpdustry.imperium.mindustry.misc.dayNightCycle
import mindustry.Vars
import mindustry.gen.Call

class DayNighCycleListener(instances: InstanceManager) : ImperiumApplication.Listener {

private val cycle = instances.get<ImperiumConfig>().mindustry.world.dayNightCycleDuration.inWholeSeconds

@EventHandler
fun onMenuToPlayEvent(event: MenuToPlayEvent) {
if (!Vars.state.rules.lighting) {
Vars.state.map.dayNightCycle = true
Vars.state.rules.lighting = true
}
}

@TaskHandler(interval = 1, unit = MindustryTimeUnit.SECONDS)
fun onSolarCycleUpdate() {
if (!Vars.state.isGame || !Vars.state.rules.lighting || !Vars.state.map.dayNightCycle) return
val time = ((System.currentTimeMillis() / 1000L) % cycle) / (cycle * 0.5F)
Vars.state.rules.ambientLight.a = Interp.sine.apply(0F, 0.8F, time)
Call.setRules(Vars.state.rules)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import kotlin.time.Duration
import kotlin.time.Duration.Companion.ZERO
import kotlin.time.Duration.Companion.seconds
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonObject
Expand All @@ -35,6 +36,7 @@ import mindustry.Vars
import mindustry.io.SaveFileReader.CustomChunk
import mindustry.maps.Map

// TODO Use a key container for this shit
private const val MAP_IDENTIFIER = "imperium-map-identifier"
var Map.id: Int?
get() = tags.get(MAP_IDENTIFIER)?.toIntOrNull()
Expand All @@ -56,6 +58,13 @@ var Map.playtime: Duration
tags.put(MAP_PLAYTIME, value.inWholeSeconds.toString())
}

private const val MAP_DAY_NIGHT_CYCLE = "imperium-map-day-night-cicle"
var Map.dayNightCycle: Boolean
get() = tags.get(MAP_DAY_NIGHT_CYCLE)?.toBooleanStrictOrNull() ?: false
set(value) {
tags.put(MAP_DAY_NIGHT_CYCLE, value.toString())
}

object ImperiumMetadataChunkReader : CustomChunk {

private val logger by LoggerDelegate()
Expand All @@ -65,6 +74,7 @@ object ImperiumMetadataChunkReader : CustomChunk {
Vars.state.map.id?.let { put(MAP_IDENTIFIER, it) }
Vars.state.map.start?.toEpochMilli()?.let { put(MAP_START, it) }
put(MAP_PLAYTIME, Vars.state.map.playtime.inWholeSeconds.toString())
put(MAP_DAY_NIGHT_CYCLE, Vars.state.map.dayNightCycle)
}
stream.writeUTF(json.toString())
logger.debug("Written imperium metadata: {}", json.toString())
Expand All @@ -75,6 +85,7 @@ object ImperiumMetadataChunkReader : CustomChunk {
Vars.state.map.id = json[MAP_IDENTIFIER]?.jsonPrimitive?.int
Vars.state.map.start = json[MAP_START]?.jsonPrimitive?.long?.let(Instant::ofEpochMilli)
Vars.state.map.playtime = json[MAP_PLAYTIME]?.jsonPrimitive?.long?.seconds ?: ZERO
Vars.state.map.dayNightCycle = json[MAP_DAY_NIGHT_CYCLE]?.jsonPrimitive?.booleanOrNull ?: false
logger.debug("Read imperium metadata: {}", json.toString())
}
}

0 comments on commit 1b51a90

Please sign in to comment.