KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt

275 lines
9.0 KiB
Kotlin

package ru.dbotthepony.kstarbound
import org.apache.logging.log4j.LogManager
import org.lwjgl.Version
import org.lwjgl.glfw.GLFW.glfwSetWindowShouldClose
import ru.dbotthepony.kstarbound.client.StarboundClient
import ru.dbotthepony.kstarbound.defs.animation.AnimationDefinition
import ru.dbotthepony.kstarbound.io.BTreeDB
import ru.dbotthepony.kstarbound.player.Avatar
import ru.dbotthepony.kstarbound.player.QuestDescriptor
import ru.dbotthepony.kstarbound.player.QuestInstance
import ru.dbotthepony.kstarbound.util.JVMTimeSource
import ru.dbotthepony.kstarbound.world.api.IChunkCell
import ru.dbotthepony.kstarbound.world.entities.ItemEntity
import ru.dbotthepony.kstarbound.world.entities.PlayerEntity
import ru.dbotthepony.kstarbound.io.json.VersionedJson
import ru.dbotthepony.kstarbound.io.readVarInt
import ru.dbotthepony.kstarbound.util.AssetPathStack
import ru.dbotthepony.kstarbound.world.`object`.WorldObject
import ru.dbotthepony.kvector.vector.Vector2d
import ru.dbotthepony.kvector.vector.Vector2f
import java.io.BufferedInputStream
import java.io.ByteArrayInputStream
import java.io.DataInputStream
import java.io.File
import java.util.*
import java.util.zip.Inflater
import java.util.zip.InflaterInputStream
private val LOGGER = LogManager.getLogger()
fun main() {
LOGGER.info("Running LWJGL ${Version.getVersion()}")
//Thread.sleep(6_000L)
val db = BTreeDB(File("F:\\SteamLibrary\\steamapps\\common\\Starbound - Unstable\\storage\\universe\\389760395_938904237_-238610574_5.world"))
//val db = BTreeDB(File("world.world"))
val client = StarboundClient()
//Starbound.addFilePath(File("./unpacked_assets/"))
Starbound.addPakPath(File("J:\\Steam\\steamapps\\common\\Starbound\\assets\\packed.pak"))
/*for (folder in File("J:\\Steam\\steamapps\\workshop\\content\\211820").list()!!) {
val f = File("J:\\Steam\\steamapps\\workshop\\content\\211820\\$folder\\contents.pak")
if (f.exists()) {
Starbound.addPakPath(f)
}
}*/
//Starbound.addPakPath(File("packed.pak"))
Starbound.initializeGame { finished, replaceStatus, status ->
client.putDebugLog(status, replaceStatus)
}
client.onTermination {
Starbound.terminateLoading = true
}
val ent = PlayerEntity(client.world!!)
Starbound.onInitialize {
//for (chunkX in 17 .. 18) {
//for (chunkX in 14 .. 24) {
for (chunkX in 0 .. 100) {
// for (chunkY in 21 .. 21) {
for (chunkY in 18 .. 24) {
val data = db.read(byteArrayOf(1, 0, chunkX.toByte(), 0, chunkY.toByte()))
val data2 = db.read(byteArrayOf(2, 0, chunkX.toByte(), 0, chunkY.toByte()))
if (data != null) {
var reader = DataInputStream(BufferedInputStream(InflaterInputStream(ByteArrayInputStream(data), Inflater())))
reader.skipBytes(3)
for (y in 0 .. 31) {
for (x in 0 .. 31) {
val cell = client.world!!.getCellDirect(chunkX * 32 + x, chunkY * 32 + y)
if (cell == null) {
IChunkCell.skip(reader)
} else {
cell.read(Starbound.tilesByID::get, Starbound.tileModifiersByID::get, Starbound.liquidByID::get, reader)
}
}
}
}
if (data2 != null) {
val reader = DataInputStream(BufferedInputStream(InflaterInputStream(ByteArrayInputStream(data2), Inflater())))
val i = reader.readVarInt()
for (i2 in 0 until i) {
val obj = VersionedJson(reader)
if (obj.identifier == "ObjectEntity") {
try {
WorldObject(client.world!!, obj.content.asJsonObject).spawn()
//println(obj.content)
//println(created)
} catch (err: Throwable) {
}
}
}
//val read = BinaryJsonReader.readElement(reader)
//println(read)
}
}
}
//client.world!!.parallax = Starbound.parallaxAccess["garden"]
val item = Starbound.items.values.random()
val rand = java.util.Random()
client.world!!.physics.gravity = Vector2d.ZERO
for (i in 0 .. 0) {
val item = ItemEntity(client.world!!, item.value)
item.position = Vector2d(7.0 + i, 685.0)
item.spawn()
//item.movement.applyVelocity(Vector2d(rand.nextDouble() * 1000.0 - 500.0, rand.nextDouble() * 1000.0 - 500.0))
item.movement.applyVelocity(Vector2d(-1.0, 0.0))
}
// println(Starbound.statusEffects["firecharge"])
AssetPathStack.push("/animations/dust4")
val def = Starbound.gson.fromJson(Starbound.locate("/animations/dust4/dust4.animation").reader(), AnimationDefinition::class.java)
AssetPathStack.pop()
val avatar = Avatar(Starbound, UUID.randomUUID())
val quest = QuestInstance(avatar, descriptor = QuestDescriptor("floran_mission1"))
quest.init()
quest.start()
var last = JVMTimeSource.INSTANCE.millis
client.onPostDrawWorld {
if (JVMTimeSource.INSTANCE.millis - last > 20L) {
quest.update(1)
last = JVMTimeSource.INSTANCE.millis
}
}
println(Starbound.treasurePools["motherpoptopTreasure"]!!.value.evaluate(Random(), 2.0))
}
//ent.position += Vector2d(y = 14.0, x = -10.0)
ent.position = Vector2d(600.0 + 16.0, 721.0 + 48.0)
client.camera.pos = Vector2d(238.0, 685.0)
//client.camera.pos = Vector2f(0f, 0f)
client.onDrawGUI {
client.font.render("${ent.position}", y = 100f, scale = 0.25f)
client.font.render("${ent.movement.velocity}", y = 120f, scale = 0.25f)
client.font.render("Camera: ${client.camera.pos} ${client.settings.zoom}", y = 140f, scale = 0.25f)
client.font.render("World chunk: ${client.world!!.chunkFromCell(client.camera.pos)}", y = 160f, scale = 0.25f)
}
client.onPreDrawWorld {
//client.camera.pos.x = ent.pos.x.toFloat()
//client.camera.pos.y = ent.pos.y.toFloat()
}
/*val lightRenderer = LightRenderer(client.gl)
lightRenderer.resizeFramebuffer(client.viewportWidth, client.viewportHeight)
client.onViewportChanged(lightRenderer::resizeFramebuffer)
lightRenderer.addShadowGeometry(object : LightRenderer.ShadowGeometryRenderer {
override fun renderHardGeometry(lightPosition: Vector2f, stack: Matrix4fStack, program: GLHardLightGeometryProgram) {
val builder = lightRenderer.builder
builder.begin()
builder.quad(-6f, 0f, -2f, 2f)
builder.quad(0f, 0f, 2f, 2f)
builder.upload()
builder.draw(GL_LINES)
}
override fun renderSoftGeometry(lightPosition: Vector2f, stack: Matrix4fStack, program: GLSoftLightGeometryProgram) {
val builder = lightRenderer.builderSoft
builder.begin()
builder.shadowQuad(-6f, 0f, -2f, 2f)
builder.shadowQuad(0f, 0f, 2f, 2f)
builder.upload()
builder.draw(GL_TRIANGLES)
}
})
client.onPostDrawWorld {
lightRenderer.begin()
for ((lightPosition, color) in listOf(
(client.screenToWorld(client.mouseCoordinatesF)) to Color.RED,
(client.screenToWorld(client.mouseCoordinatesF) + Vector2f(0.1f)) to Color.GREEN,
(client.screenToWorld(client.mouseCoordinatesF) + Vector2f(-0.1f)) to Color.BLUE,
)) {
lightRenderer.renderSoftLight(lightPosition, color, radius = 40f)
}
for ((lightPosition, color) in listOf(
(client.screenToWorld(client.mouseCoordinatesF) + Vector2f(10.1f)) to Color.RED,
(client.screenToWorld(client.mouseCoordinatesF) + Vector2f(10.1f)) to Color.GREEN,
(client.screenToWorld(client.mouseCoordinatesF) + Vector2f(9.9f)) to Color.BLUE,
)) {
//lightRenderer.renderSoftLight(lightPosition, color, radius = 10f)
}
lightRenderer.renderOutputAdditive()
}*/
client.box2dRenderer.drawShapes = false
client.box2dRenderer.drawPairs = false
client.box2dRenderer.drawAABB = false
client.box2dRenderer.drawJoints = false
//ent.spawn()
client.input.addScrollCallback { _, x, y ->
if (y > 0.0) {
client.settings.zoom *= y.toFloat() * 2f
} else if (y < 0.0) {
client.settings.zoom /= -y.toFloat() * 2f
}
}
while (client.renderFrame()) {
Starbound.pollCallbacks()
//ent.think(client.frameRenderTime)
//client.camera.pos.x = ent.position.x.toFloat()
//client.camera.pos.y = ent.position.y.toFloat()
client.camera.pos += Vector2d(
(if (client.input.KEY_LEFT_DOWN || client.input.KEY_A_DOWN) -Starbound.TICK_TIME_ADVANCE * 32f / client.settings.zoom else 0.0) + (if (client.input.KEY_RIGHT_DOWN || client.input.KEY_D_DOWN) Starbound.TICK_TIME_ADVANCE * 32f / client.settings.zoom else 0.0),
(if (client.input.KEY_UP_DOWN || client.input.KEY_W_DOWN) Starbound.TICK_TIME_ADVANCE * 32f / client.settings.zoom else 0.0) + (if (client.input.KEY_DOWN_DOWN || client.input.KEY_S_DOWN) -Starbound.TICK_TIME_ADVANCE * 32f / client.settings.zoom else 0.0)
)
//println(client.camera.velocity.toDoubleVector() * client.frameRenderTime * 0.1)
//if (ent.onGround)
//ent.velocity += client.camera.velocity.toDoubleVector() * client.frameRenderTime * 0.1
/*if (client.input.KEY_LEFT_DOWN) {
ent.movement.moveDirection = Move.MOVE_LEFT
} else if (client.input.KEY_RIGHT_DOWN) {
ent.movement.moveDirection = Move.MOVE_RIGHT
} else {
ent.movement.moveDirection = Move.STAND_STILL
}
if (client.input.KEY_SPACE_PRESSED && ent.movement.onGround) {
ent.movement.requestJump()
} else if (client.input.KEY_SPACE_RELEASED) {
ent.movement.recallJump()
}*/
if (client.input.KEY_ESCAPE_PRESSED) {
glfwSetWindowShouldClose(client.window, true)
}
//ent.wantsToDuck = client.input.KEY_DOWN_DOWN
}
}