tcr/java/src/game/gui/world/GuiCreate.java
2025-03-12 18:13:11 +01:00

405 lines
14 KiB
Java
Executable file

package game.gui.world;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import game.collect.Sets;
import game.color.TextColor;
import game.dimension.DimType;
import game.dimension.Dimension;
import game.dimension.Space;
import game.gui.GuiScreen;
import game.gui.element.Button;
import game.gui.element.TextField;
import game.init.UniverseRegistry;
import game.lib.input.Keyboard;
import game.network.NetHandlerPlayServer;
import game.renderer.FontRenderer;
import game.rng.Random;
import game.util.Predicate;
import game.world.Region;
public class GuiCreate extends GuiScreen
{
private static final String MBASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!?-_<3";
private static final String MBASE64A_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-!";
private static final String MBASE64B_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz<_";
private GuiScreen parentScreen;
private TextField worldNameField;
private TextField worldSeedField;
private TextField worldUserField;
// private Button modeButton;
private Button dimButton;
// private String descLine1;
// private String descLine2;
private String[] descLines;
private boolean alreadyGenerated;
private boolean fileExists;
private boolean fileInvalid;
private String seed;
private String decoded;
private int dimension;
private static long getSeed(String str) {
if(str == null || str.isEmpty())
return (new Random()).longv();
try {
return Long.parseLong(str);
}
catch(NumberFormatException e) {
}
long seed = 0L;
if(str.length() <= 9) {
for(int z = 0; z < str.length(); z++) {
seed <<= 7;
char c = str.charAt(z);
if(c >= 0x80) {
seed = -1L;
break;
}
seed |= (long)c;
}
}
else if(str.length() == 10) {
String ch = str.indexOf('<') != -1 || str.indexOf('_') != -1 ? MBASE64B_CHARS : MBASE64A_CHARS;
for(int z = 0; z < str.length(); z++) {
seed <<= 6;
int idx = ch.indexOf(str.charAt(z));
if(idx == -1) {
seed = -1L;
break;
}
seed |= (long)idx;
}
if(seed != -1L)
seed |= ch == MBASE64B_CHARS ? 0xb000000000000000L : 0xa000000000000000L;
}
else if(str.length() <= 12) {
for(int z = 0; z < str.length(); z++) {
seed <<= 5;
int idx = MBASE32_CHARS.indexOf(Character.toUpperCase(str.charAt(z)));
if(idx == -1) {
seed = -1L;
break;
}
seed |= (long)idx;
}
if(seed != -1L)
seed |= str.length() == 12 ? 0x9000000000000000L : 0x8000000000000000L;
}
else {
seed = -1L;
}
if(seed == -1L) {
seed = 0L;
for(int z = 0; z < str.length(); z++) {
seed <<= 7;
seed |= (long)((char)(str.charAt(z) & 0x7f));
}
seed = (seed ^ (((long)str.hashCode()) | (~((long)str.hashCode()) << 32))) | 0xc000000000000000L;
}
return seed;
}
private static String decodeSeed(long seed, String def) {
if(seed == 0L)
return (def == null ? ("" + seed) : def);
if((seed & 0x8000000000000000L) == 0L) {
String str = "";
long value = seed;
while(value != 0L) {
char c = (char)(value & 0x7fL);
if(c < 32 || c > 126)
return (def == null ? ("" + seed) : def);
str = c + str;
value >>= 7;
}
try {
Long.parseLong(str);
}
catch(NumberFormatException e) {
return str;
}
return (def == null ? ("" + seed) : def);
}
String chset;
int len;
int shift;
long mask;
long type = (seed >> 60) & 0xf;
switch((int)type) {
case 0x8:
chset = MBASE32_CHARS;
len = 11;
shift = 5;
mask = 0x1fL;
break;
case 0x9:
chset = MBASE32_CHARS;
len = 12;
shift = 5;
mask = 0x1fL;
break;
case 0xa:
chset = MBASE64A_CHARS;
len = 10;
shift = 6;
mask = 0x3fL;
break;
case 0xb:
chset = MBASE64B_CHARS;
len = 10;
shift = 6;
mask = 0x3fL;
break;
default:
return (def == null ? ("" + seed) : def);
}
String str = "";
for(int z = 0; z < len; z++) {
str = chset.charAt((int)(seed & mask)) + str;
seed >>= shift;
}
try {
Long.parseLong(str);
}
catch(NumberFormatException e) {
return str;
}
return (def == null ? ("" + seed) : def);
}
public GuiCreate(GuiScreen parent)
{
UniverseRegistry.clear();
this.parentScreen = parent;
// this.worldSeed = "";
// this.worldName = "Neue Welt";
}
public void updateScreen()
{
this.worldNameField.updateCursorCounter();
this.worldSeedField.updateCursorCounter();
this.worldUserField.updateCursorCounter();
String text = this.worldSeedField.getText();
if(text.isEmpty()) {
this.seed = "Zufällig";
this.decoded = "-";
}
else {
this.seed = "" + getSeed(text);
try {
this.decoded = decodeSeed(Long.parseLong(text), "-");
}
catch(NumberFormatException e) {
this.decoded = decodeSeed(getSeed(text), "-");
}
}
this.fileExists = false;
this.fileInvalid = false;
text = this.worldNameField.getText().trim();
// for(String n : GuiCreate.DISALLOWED_FILES) {
// if(text.equalsIgnoreCase(n)) {
// ((Button)this.buttonList.get(0)).enabled = false;
// this.fileInvalid = true;
// return;
// }
// }
if(this.fileInvalid = GuiCreate.DISALLOWED_FILES.contains(text.toUpperCase())) {
((Button)this.buttonList.get(0)).enabled = false;
return;
}
if(this.fileExists = (!text.isEmpty() && new File(Region.SAVE_DIR, text).exists()))
((Button)this.buttonList.get(0)).enabled = false;
}
public void initGui()
{
Keyboard.enableRepeatEvents(true);
this.buttonList.clear();
this.buttonList.add(new Button(0, this.width / 2 - 155, this.height - 28, 150, 20, "Neue Welt erstellen"));
this.buttonList.add(new Button(1, this.width / 2 + 5, this.height - 28, 150, 20, "Abbrechen"));
// this.buttonList.add(this.modeButton = new Button(2, this.width / 2 - 155, 175, 310, 20, ""));
this.buttonList.add(this.dimButton = new Button(2, this.width / 2 - 155, 220, 310, 20, ""));
this.worldNameField = new TextField(this.width / 2 - 100, 60, 200, 20);
this.worldNameField.setFocused(true);
this.worldNameField.setValidator(new Predicate<String>() {
public boolean apply(String name) {
for(int z = 0; z < name.length(); z++) {
if(DISALLOWED_CHARS.contains(name.charAt(z)))
return false;
}
return true;
}
});
// this.worldNameField.setText("Welt");
this.worldSeedField = new TextField(this.width / 2 - 100, 136, 200, 20);
// this.worldSeedField.setText(this.worldSeed);
this.worldUserField = new TextField(this.width / 2 - 100, 98, 200, 20);
this.worldUserField.setMaxStringLength(16);
this.worldUserField.setValidator(new Predicate<String>() {
public boolean apply(String name) {
return NetHandlerPlayServer.isValidUser(name);
}
});
// this.calcSaveDirName();
// this.setModeButton();
this.setDimButton();
((Button)this.buttonList.get(0)).enabled = false;
}
// private void setModeButton() {
// this.modeButton.display = "Kreativmodus: " + (this.noCreative ? "Aus" : "An");
// this.descLine1 = this.noCreative ? "Suche nach Ressourcen, baue Werkzeuge, Waffen und mehr"
// : "Unbegrenzte Rohstoffe, Flugmodus, Unverwundbarkeit, keine";
// this.descLine2 = this.noCreative ? "Gegenstände, sammle Erfahrung und erkunde die Welten"
// : "Energie oder Mana und sofortiges Zerstören von Blöcken";
// }
private void setDimButton() {
Dimension dim = this.dimension == Integer.MAX_VALUE ? Space.INSTANCE : UniverseRegistry.getBaseDimensions().get(this.dimension);
this.dimButton.display = (dim == Space.INSTANCE ? "" :
((dim.getDimensionId() >= UniverseRegistry.MORE_DIM_ID ?
"Vorlage" : (dim.getType() == DimType.PLANET ? "Heimplanet" : "Dimension")) + ": ")) +
(dim.getDimensionId() >= UniverseRegistry.MORE_DIM_ID ? dim.getCustomName() :
dim.getFormattedName(false));
this.descLines = dim.getFormattedName(true).split(" / ");
}
// private void calcSaveDirName()
// {
// this.saveDirName = this.worldNameField.getText().trim();
//
//// for (char c : DISALLOWED_CHARS)
//// {
//// this.saveDirName = this.saveDirName.replace(c, '_');
//// }
//
// if (!this.saveDirName.isEmpty())
//// {
//// this.saveDirName = "World";
//// }
//
// this.saveDirName = getUncollidingSaveDirName(Region.SAVE_DIR, this.saveDirName);
// }
public void onGuiClosed()
{
Keyboard.enableRepeatEvents(false);
}
protected void actionPerformed(Button button) throws IOException
{
if (button.enabled)
{
if (button.id == 1)
{
this.gm.displayGuiScreen(this.parentScreen);
}
else if (button.id == 0)
{
this.gm.displayGuiScreen(null);
if(this.alreadyGenerated)
return;
this.alreadyGenerated = true;
Dimension dim = this.dimension == Integer.MAX_VALUE ? Space.INSTANCE :
UniverseRegistry.getBaseDimensions().get(this.dimension);
dim.setSeed(getSeed(this.worldSeedField.getText()));
this.gm.launchIntegratedServer(this.worldNameField.getText().trim(), dim, this.worldUserField.getText());
}
// else if (button.id == 2)
// {
// this.noCreative ^= true;
// this.setModeButton();
// }
else if (button.id == 2)
{
// int index = Integer.MAX_VALUE;
// for(int z = 0; z < UniverseRegistry.getBasePlanets().size(); z++) {
// if(UniverseRegistry.getBasePlanets().get(z).getDimensionId() == this.dimension) {
// index = z;
// break;
// }
// }
if(this.dimension == Integer.MAX_VALUE) {
this.dimension = GuiScreen.isShiftKeyDown() ? UniverseRegistry.getBaseDimensions().size() - 1 : 0;
}
else {
if(GuiScreen.isShiftKeyDown()) {
if(--this.dimension < 0)
this.dimension = Integer.MAX_VALUE;
}
else {
if(++this.dimension >= UniverseRegistry.getBaseDimensions().size())
this.dimension = Integer.MAX_VALUE;
}
}
// this.dimension = index == Integer.MAX_VALUE ? Space.INSTANCE.getDimensionId() :
// UniverseRegistry.getBasePlanets().get(index).getDimensionId();
this.setDimButton();
}
}
}
protected void keyTyped(char typedChar, int keyCode) throws IOException
{
// if (this.worldNameField.isFocused())
// {
this.worldNameField.textboxKeyTyped(typedChar, keyCode);
// this.worldName = this.worldNameField.getText();
// }
// else if (this.worldSeedField.isFocused())
// {
this.worldSeedField.textboxKeyTyped(typedChar, keyCode);
// this.worldSeed = this.worldSeedField.getText();
// }
// else if (this.worldUserField.isFocused())
// {
this.worldUserField.textboxKeyTyped(typedChar, keyCode);
// }
if (keyCode == 28 || keyCode == 156)
{
this.actionPerformed((Button)this.buttonList.get(0));
}
((Button)this.buttonList.get(0)).enabled = this.worldNameField.getText().length() > 0 && this.worldUserField.getText().length() > 0;
// this.calcSaveDirName();
}
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
{
super.mouseClicked(mouseX, mouseY, mouseButton);
this.worldSeedField.mouseClicked(mouseX, mouseY, mouseButton);
this.worldNameField.mouseClicked(mouseX, mouseY, mouseButton);
this.worldUserField.mouseClicked(mouseX, mouseY, mouseButton);
}
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
this.drawBackground();
FontRenderer.drawCenteredString("Neue Welt erstellen", this.width / 2, 20, -1);
FontRenderer.drawStringWithShadow("Startwert [" + this.seed + "]", this.width / 2 - 100, 125, -6250336);
FontRenderer.drawStringWithShadow("Dekodiert: " + this.decoded, this.width / 2 - 100, 160, -6250336);
FontRenderer.drawStringWithShadow((this.worldNameField.getText().trim().isEmpty() || this.fileExists || this.fileInvalid ? TextColor.DARK_RED : "")
+ "Ordner der Welt" + (this.fileExists ? " - Existiert bereits" : ""), this.width / 2 - 100, 49, -6250336);
FontRenderer.drawStringWithShadow((this.worldUserField.getText().isEmpty() ? TextColor.DARK_RED : "") + "Spielername", this.width / 2 - 100, 87, -6250336);
this.worldSeedField.drawTextBox();
this.worldNameField.drawTextBox();
this.worldUserField.drawTextBox();
// FontRenderer.drawStringWithShadow(this.descLine1, this.width / 2 - 153, 197, -6250336);
// FontRenderer.drawStringWithShadow(this.descLine2, this.width / 2 - 153, 197 + FontRenderer.FONT_HEIGHT + 1, -6250336);
for(int z = 1; z < this.descLines.length; z++) {
FontRenderer.drawStringWithShadow(this.descLines[z], this.width / 2 - 153, 242 + (z - 1) * (FontRenderer.FONT_HEIGHT + 1), -6250336);
}
super.drawScreen(mouseX, mouseY, partialTicks);
}
}