From ab4962c119a561b8e5e87cebcdc4ed955ed42e40 Mon Sep 17 00:00:00 2001 From: FryingPan Date: Sat, 13 Aug 2022 21:33:58 -0400 Subject: [PATCH] stuff --- build.hxp | 8 +++- src/Chat.hx | 22 ++++++++++ src/Logger.hx | 48 +++++++++++++++++++++ src/game/DataParser.hx | 13 ++++-- src/game/GameServer.hx | 8 ++++ src/game/world/Chunk.hx | 96 +++++++++++++++++++++++++++++++++++------ 6 files changed, 177 insertions(+), 18 deletions(-) create mode 100644 src/Logger.hx diff --git a/build.hxp b/build.hxp index 174cb63..12bf995 100644 --- a/build.hxp +++ b/build.hxp @@ -17,8 +17,12 @@ class Build extends Script { case 'run': command_run(); default: - command_build(); - Log.info('Task completed.'); + Log.info('Cuttlefish Build Tool - Help +Commands: + build [list of target flags] - Builds Cuttlefish for all targets listed + buildall - Builds Cuttlefish for all targets. Shortcut for `build` followed by all target flags + run - Runs the most recent build available of Cuttlefish for the provided target + test - Builds and then runs Cuttlefish for the provided target. Shortcut for `build -` then `run `'); } } diff --git a/src/Chat.hx b/src/Chat.hx index eb7548e..3eedf01 100644 --- a/src/Chat.hx +++ b/src/Chat.hx @@ -141,6 +141,19 @@ class ChatComponent { return json; } + public function terminalize(?before:String):String { + var out = ''; + if (before != null) out += before; + if (this.isBold != null) out += this.isBold ? Logger.ansi_bold : Logger.ansi_bold_end; + if (this.isItalic != null) out += this.isItalic ? Logger.ansi_italic : Logger.ansi_italic_end; + if (this.isUnderlined != null) out += this.isUnderlined ? Logger.ansi_underline : Logger.ansi_underline_end; + if (this.isStrikethrough != null) out += this.isStrikethrough ? Logger.ansi_strike : Logger.ansi_strike_end; + if (this.isObfuscated != null) out += this.isObfuscated ? Logger.ansi_obsfucated : Logger.ansi_obsfucated_end; + if (this.usedColor != null) out += ChatComponent.terminalizeColor(this.usedColor); + if (this.extras != null) for (extra in extras) out += extra.terminalize(); + return out; + } + inline function comp(str:String):String { if (str.startsWith('{"') && str.endsWith(',')) str = str.substr(0, str.length - 1) + '}'; @@ -170,6 +183,10 @@ class ChatComponent { case Hex(code): '#$code'; } } + + public static function terminalizeColor(col:Color):String { + return Logger.colorToAnsi(col); + } } class StringComponent extends ChatComponent { @@ -180,6 +197,11 @@ class StringComponent extends ChatComponent { s += '"text":"$text",'; return comp(s); } + + override public function terminalize(?before:String):String { + var s = super.terminalize(text); + return s; + } } class TranslationComponent extends ChatComponent { diff --git a/src/Logger.hx b/src/Logger.hx new file mode 100644 index 0000000..b4481be --- /dev/null +++ b/src/Logger.hx @@ -0,0 +1,48 @@ +package; + +import Chat.Color; + +using StringTools; + +class Logger { + public static final ansi_obsfucated = '\u001B[5m'; + public static final ansi_bold = '\u001B[1m'; + public static final ansi_strike = '\u001B[9m'; + public static final ansi_underline = '\u001B[4m'; + public static final ansi_italic = '\u001B[3m'; + public static final ansi_obsfucated_end = '\u001B[25m'; + public static final ansi_bold_end = '\u001B[21m'; + public static final ansi_strike_end = '\u001B[29m'; + public static final ansi_underline_end = '\u001B[24m'; + public static final ansi_italic_end = '\u001B[23m'; + + public static function colorToAnsi(col:Color):String { + switch (col) { + case Hex(code): return hexAnsi(code); + case Reset: return '\u001B[m'; + case Legacy(code): return code; // i cant be bothered + case Black: return hexAnsi('000000'); + case DarkBlue: return hexAnsi('0000aa'); + case DarkGreen: return hexAnsi('00aa00'); + case DarkCyan: return hexAnsi('00aaaa'); + case DarkRed: return hexAnsi('aa0000'); + case DarkPurple: return hexAnsi('aa00aa'); + case Gold: return hexAnsi('ffaa00'); + case LightGray: return hexAnsi('aaaaaa'); + case DarkGray: return hexAnsi('555555'); + case LightBlue: return hexAnsi('5555ff'); + case LightGreen: return hexAnsi('5555ff'); + case LightCyan: return hexAnsi('55ffff'); + case LightRed: return hexAnsi('ff5555'); + case LightPurple: return hexAnsi('ff55ff'); + case Yellow: return hexAnsi('ffff55'); + case White: return hexAnsi('ffffff'); + }; + } + + static function hexAnsi(hex:String):String { + if (hex.length == 7 && hex.startsWith('#')) hex = hex.substr(1, 6); + if (hex.length != 6) throw 'Invalid hex code'; + return '\u001B[38;2;${hex.substr(0,2)};${hex.substr(2,2)};${hex.substr(4,2)}m'; + } +} \ No newline at end of file diff --git a/src/game/DataParser.hx b/src/game/DataParser.hx index 68e6fb5..112f0db 100644 --- a/src/game/DataParser.hx +++ b/src/game/DataParser.hx @@ -27,6 +27,7 @@ typedef BlockState = Map; class DataParser { public static var dataLocation:String = '~/Desktop/dev/Calamari/data'; public static var blockStateData:Null> = null; + public static var blockStateCount:Null = null; public static function getRegistry(name:Identifier):SubRegistry { var content = File.getContent('/home/frying-pan/Desktop/dev/Calamari/data/generated/reports/registries.json'); @@ -44,14 +45,18 @@ class DataParser { return ret; } - static function loadBlockStateData() { + public static function loadBlockStateData() { if (blockStateData != null) return; - var content = File.getContent('$dataLocation/generated/reports/blocks.json'); + var content = File.getContent('/home/frying-pan/Desktop/dev/Calamari/data/generated/reports/blocks.json'); var parsed:DynamicAccess = Json.parse(content); trace(parsed.get('minecraft:acacia_button').states[0].id); - - + var sanitized:Map = []; + for (name => data in parsed) { + sanitized.set(name, {properties: data.properties, states: data.states}); + DataParser.blockStateCount += data.states.length; + } + DataParser.blockStateData = sanitized; } public static function getBlockStateFromId(id:Int):BlockState { diff --git a/src/game/GameServer.hx b/src/game/GameServer.hx index 6845138..05200f5 100644 --- a/src/game/GameServer.hx +++ b/src/game/GameServer.hx @@ -1,5 +1,6 @@ package game; +import Chat; import game.DataParser; import sys.net.Host; import sys.net.Socket; @@ -26,6 +27,13 @@ class GameServer { this.blockRegistry = DataParser.getRegistry(new Identifier('minecraft', 'block')); this.entityRegistry = DataParser.getRegistry(new Identifier('minecraft', 'entity_type')); + DataParser.loadBlockStateData(); + + var out = Sys.stdout(); + + trace(Logger.colorToAnsi(Color.Gold)); + out.writeString(ChatComponent.buildText('hello').color(Color.Gold).terminalize() + '\n'); + out.writeString(ChatComponent.buildText('kicked').color(Color.Yellow).underline(true).extra(ChatComponent.buildText(' by ').underline(false).strike(true).color(Color.Hex('ff00ff'))).extra(ChatComponent.buildText('haxe!').color(Gold).italic(true)).extra(ChatComponent.buildText(' wow').obfuscate(true).underline(false).color(Hex('40d0e0')).extra(ChatComponent.buildText('trolled').color(LightGreen).obfuscate(false).font(new Identifier('minecraft', 'alt')))).terminalize() + '\n'); // trace(blockRegistry.entries); this.listeningSocket = new Socket(); diff --git a/src/game/world/Chunk.hx b/src/game/world/Chunk.hx index 19bc325..6c36fe3 100644 --- a/src/game/world/Chunk.hx +++ b/src/game/world/Chunk.hx @@ -4,15 +4,37 @@ import haxe.io.Input; import game.DataParser.BlockState; import haxe.io.Output; +using VarIntLong; + class Chunk { public static function getGlobalPaletteIdFromState(state:BlockState):Int { var ret:Int = -1; for (name => data in DataParser.blockStateData) { for (bstate in data.states) { - if ( + var matches:Bool = true; + for (key => value in bstate.properties) { + if (value != state[key]) { + matches = false; + break; + } + } + if (matches) { + return bstate.id; + } + } + } + return ret; + } + + public static function getStateFromGlobalPaletteId(id:Int):Null { + for (name => data in DataParser.blockStateData) { + for (state in data.states) { + if (state.id == id) { + return state.properties; + } } } - return ret + return null; } public final x:Int; @@ -47,26 +69,76 @@ class ChunkSection { } interface PalettedContainer { - public function idForState(state:Dynamic):Int; - public function stateForId(id:Int):Dynamic; - public var bitsPerBlock:Int; - public function read(input:Input); - public function write(output:Output); + public function idForState(state:BlockState):Int; + public function stateForId(id:Int):BlockState; + public var bitsPerBlock(get, never):Int; + public function read(input:Input):Void; + public function write(output:Output):Void; } class DirectPalette implements PalettedContainer { - + public var bitsPerBlock(get, never):Int; + + public function get_bitsPerBlock():Int { + return Math.ceil(Math.log(DataParser.blockStateCount)); + } + + public function new() {} + + public function idForState(state:BlockState):Int { + return Chunk.getGlobalPaletteIdFromState(state); + } + + public function stateForId(id:Int):BlockState { + return Chunk.getStateFromGlobalPaletteId(id); + } + + public function read(input:Input) { + + } + + public function write(output:Output) { + + } } class IndirectPalette implements PalettedContainer { - public var bitsPerBlock:Int; - var idToState:Map< + public var bitsPerBlock(get, never):Int; + private var _bitsPerBlock:Int; + var idToState:Map; + var stateToId:Map; + + public function get_bitsPerBlock():Int { + return _bitsPerBlock; + } public function new(bitsPerBlock:Int) { - this.bitsPerBlock = bitsPerBlock; + this._bitsPerBlock = bitsPerBlock; + } + + public function idForState(state:BlockState):Int { + return stateToId.get(state); + } + + public function stateForId(id:Int):BlockState { + return idToState.get(id); } - public function idForState(state:Dynamic):Int { + public function read(input:Input) { + idToState = []; + stateToId = []; + + var length = input.readVarInt(); + + for (i in 0...length) { + var stateId = input.readVarInt(); + var state = Chunk.getStateFromGlobalPaletteId(stateId); + idToState.set(stateId, state); + stateToId.set(state, stateId); + } + } + public function write(output:Output) { + } } \ No newline at end of file