Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
BNTFryingPan committed Aug 14, 2022
1 parent 8d2d1f8 commit ab4962c
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 18 deletions.
8 changes: 6 additions & 2 deletions build.hxp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <target> - Runs the most recent build available of Cuttlefish for the provided target
test <target> - Builds and then runs Cuttlefish for the provided target. Shortcut for `build -<target>` then `run <target>`');
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/Chat.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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) + '}';
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions src/Logger.hx
Original file line number Diff line number Diff line change
@@ -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';
}
}
13 changes: 9 additions & 4 deletions src/game/DataParser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ typedef BlockState = Map<String, String>;
class DataParser {
public static var dataLocation:String = '~/Desktop/dev/Calamari/data';
public static var blockStateData:Null<Map<String, BlockStateList>> = null;
public static var blockStateCount:Null<Int> = null;

public static function getRegistry(name:Identifier):SubRegistry {
var content = File.getContent('/home/frying-pan/Desktop/dev/Calamari/data/generated/reports/registries.json');
Expand All @@ -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<BlockStateList> = Json.parse(content);

trace(parsed.get('minecraft:acacia_button').states[0].id);


var sanitized:Map<String, BlockStateList> = [];
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 {
Expand Down
8 changes: 8 additions & 0 deletions src/game/GameServer.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package game;

import Chat;
import game.DataParser;
import sys.net.Host;
import sys.net.Socket;
Expand All @@ -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();
Expand Down
96 changes: 84 additions & 12 deletions src/game/world/Chunk.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockState> {
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;
Expand Down Expand Up @@ -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<Int, BlockState>;
var stateToId:Map<BlockState, Int>;

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) {

}
}

0 comments on commit ab4962c

Please sign in to comment.