Skip to content

Commit

Permalink
Time machine -- can be built from iron & time flux powder, allows pla…
Browse files Browse the repository at this point in the history
…yer to skip World time & realtime ticks using time flux powder
  • Loading branch information
Hitonoriol committed Jan 14, 2021
1 parent 3ba16de commit d8bbea3
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 21 deletions.
7 changes: 3 additions & 4 deletions MadSand-core/src/hitonoriol/madsand/Keyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,9 @@ private static void pollActionKeys() {

if (Gdx.input.isKeyJustPressed(Keys.N) && MadSand.world.curLayer() == Location.LAYER_OVERWORLD)
MadSand.world.travel();

if (Gdx.input.isKeyPressed(Keys.CONTROL_LEFT) && Gdx.input.isKeyJustPressed(Keys.W))
World.player.skipTime();
}

private static void pollMovementKeys() {

if (Gdx.input.isKeyPressed(Keys.A))
World.player.walk(Direction.LEFT);

Expand Down Expand Up @@ -183,6 +179,9 @@ else if (Gdx.input.isKeyJustPressed(Keys.R)) {
MadSand.world.generate();
MadSand.worldEntered();
}

else if (Gdx.input.isKeyJustPressed(Keys.W))
World.player.skipTime();
}

if (Gdx.input.isKeyJustPressed(Keys.Y))
Expand Down
2 changes: 1 addition & 1 deletion MadSand-core/src/hitonoriol/madsand/Mouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static String getCurrentCellInfo() {
}

if (!object.equals(Map.nullObject))
info += ("Object: " + object.name + " (" + object.getHpPercent() + "%)") + Resources.LINEBREAK;
info += ("Object: " + object.name + " (" + Utils.round(object.getHpPercent()) + "%)") + Resources.LINEBREAK;

if (station != null)
info += getProdStationInfo(station);
Expand Down
17 changes: 14 additions & 3 deletions MadSand-core/src/hitonoriol/madsand/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import hitonoriol.madsand.dialog.GameTextSubstitutor;
import me.xdrop.jrand.JRand;

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang3.exception.ExceptionUtils;

Expand All @@ -22,13 +25,16 @@
public class Utils {
public static boolean debugMode = false;
public static SpriteBatch batch;
static NumberFormat numberFormatter = NumberFormat.getInstance(Locale.US);

public static Random random = new Random();

public static void init() {
batch = new SpriteBatch();
numberFormatter.setMinimumFractionDigits(0);
numberFormatter.setRoundingMode(RoundingMode.HALF_UP);
try {
Resources.init();
batch = new SpriteBatch();
} catch (Exception e) {
die("Exception on init: " + ExceptionUtils.getStackTrace(e));
}
Expand Down Expand Up @@ -82,8 +88,13 @@ public static void die(String... msg) {
System.exit(-1);
}

public static double round(double num) {
return (Math.round(num * 100) / 100.00);
public static String round(double num) {
return round(num, 2);
}

public static String round(double num, int n) {
numberFormatter.setMaximumFractionDigits(n);
return numberFormatter.format(num);
}

public static int rand(int min, int max) {
Expand Down
4 changes: 3 additions & 1 deletion MadSand-core/src/hitonoriol/madsand/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,8 @@ public void restFully() {
MadSand.notice("Rested for " + Utils.timeString(MadSand.world.toWorldTimeSeconds(ticksToRest)));
}

public static float TIMESKIP_COEF = 18f, REALTIME_SKIP_COEF = 5.5f;

public void skipTime() {
int timeSkipItemId = Globals.getInt(Globals.TIMESKIP_ITEM);
Item timeSkipItem = inventory.getItem(timeSkipItemId);
Expand All @@ -1104,7 +1106,7 @@ public void skipTime() {
return;
}

int maxTimeSkip = timeSkipItem.quantity;
int maxTimeSkip = (int) (timeSkipItem.quantity * TIMESKIP_COEF);
new WaitDialog(maxTimeSkip).show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

public class LandDialog extends GameDialog {

static int DEC_PLACES = 4;
static int ITEMS_PER_ROW = 6;
static float ITEM_SCALE = 0.1f;
static float PAD = 5;
Expand Down Expand Up @@ -143,7 +144,7 @@ private String getWorkerList() {
continue;

sb.append("* " + type.name() + ": " + workers.getQuantity() + " ");
sb.append("(" + Utils.round(workers.getGatheringRate() / MadSand.world.realtimeTickRate) + " actions/sec) ")
sb.append("(" + Utils.round(workers.getGatheringRate() / MadSand.world.realtimeTickRate, DEC_PLACES) + " actions/sec) ")
.append("[[" + Utils.round(workers.itemCharge * 100f) + "%]")
.append(Resources.LINEBREAK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public SliderDialog setOnUpdateText(String text) {
return this;
}

public SliderDialog setStep(int step) {
slider.setStepSize(step);
return this;
}

public SliderDialog setTitle(String title) {
super.setTitle(title);
return this;
Expand All @@ -142,6 +147,10 @@ public int getSliderValue() {
public void setSliderText(String text) { // Set text under the slider
bottomLabel.setText(text);
}

public Label getBottomLabel() {
return bottomLabel;
}

@Override
public void show() {
Expand Down
34 changes: 29 additions & 5 deletions MadSand-core/src/hitonoriol/madsand/gui/dialogs/WaitDialog.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
package hitonoriol.madsand.gui.dialogs;

import com.badlogic.gdx.utils.Align;

import hitonoriol.madsand.MadSand;
import hitonoriol.madsand.Resources;
import hitonoriol.madsand.Utils;
import hitonoriol.madsand.entities.Player;
import hitonoriol.madsand.properties.Globals;
import hitonoriol.madsand.properties.ItemProp;
import hitonoriol.madsand.world.World;

public class WaitDialog extends SliderDialog {

private int worldSeconds;
private int worldSeconds, realtimeTicks;
private int timeSkipItemSpent;

public WaitDialog(int maxTimeSkip) {
super(maxTimeSkip);
super((int) Player.TIMESKIP_COEF, maxTimeSkip);
super.setStep((int) Player.TIMESKIP_COEF);
super.setTitle("Skip Time");
super.setSliderTitle("How much time to skip:");
super.getBottomLabel().setAlignment(Align.center);

String timeSkipItem = ItemProp.getItemName(Globals.getInt(Globals.TIMESKIP_ITEM));
super.setSliderAction(
ticks -> super.setSliderText(Utils.timeString(worldSeconds = MadSand.world.toWorldTimeSeconds(ticks))));
ticks -> {
timeSkipItemSpent = (int) Math.max((float) ticks / Player.TIMESKIP_COEF, 1);
worldSeconds = MadSand.world.toWorldTimeSeconds(ticks);
realtimeTicks = (int) (timeSkipItemSpent * Player.REALTIME_SKIP_COEF);
super.setSliderText("+"+Utils.timeString(worldSeconds) + " [[World time]"
+ Resources.LINEBREAK

+ "+"+Utils.timeString((long) (realtimeTicks * MadSand.world.realtimeTickRate))
+ " [[Realtime]"
+ Resources.LINEBREAK

+ "-"+timeSkipItemSpent + " " + timeSkipItem);
});

super.setConfirmAction(ticks -> {
World.player.inventory.delItem(Globals.getInt(Globals.TIMESKIP_ITEM), timeSkipItemSpent);
World.player.skipTime(ticks);
World.player.inventory.delItem(Globals.getInt(Globals.TIMESKIP_ITEM), ticks);
MadSand.world.skipRealtimeTicks(realtimeTicks);
MadSand.notice("Skipped " + Utils.timeString(worldSeconds));
});
}
}
}
2 changes: 1 addition & 1 deletion MadSand-core/src/hitonoriol/madsand/map/MapObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public int rollDrop(ItemType heldItemType) {
}

public double getHpPercent() {
return Utils.round(((double) hp / (double) maxHp) * 100d);
return ((double) hp / (double) maxHp) * 100d;
}

private static int getAltItem(int id, ItemType hand, HashMap<ItemType, ArrayList<Integer>> container) {
Expand Down
2 changes: 1 addition & 1 deletion MadSand-core/src/hitonoriol/madsand/world/Settlement.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public String getLeaderName() {
}

public static class WorkerContainer { // Info about all workers of certain type
public static float ITEMS_PER_LVL = 0.1f; // Items per lvl per realtimeTick
public static float ITEMS_PER_LVL = 0.025f; // Items per lvl per realtimeTick

public int lvl = 1;
public float itemCharge = 0;
Expand Down
9 changes: 6 additions & 3 deletions MadSand-core/src/hitonoriol/madsand/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,14 +661,17 @@ private void realtimeTick() {
++globalRealtimeTick;
realtimeRefresh();
}

public void skipRealtimeTicks(long ticks) {
for (; ticks > 0; --ticks)
realtimeRefresh();
}

private void offlineReward(long offlineTime) {
long offlineTicks = (long) (offlineTime / realtimeTickRate);
globalRealtimeTick += offlineTicks;
LuaUtils.executeScript(LuaUtils.offlineRewardScript, offlineTime);

for (; offlineTicks > 0; --offlineTicks)
realtimeRefresh();
skipRealtimeTicks(offlineTicks);
}

private float HOUR = 3600;
Expand Down
3 changes: 2 additions & 1 deletion core/assets/buildrecipes.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"75" : "71/1:2/10:9/5",
"82" : "1/30:10/10",
"158" : "72/20",
"157" : "8/50"
"157" : "8/50",
"175" : "101/50:72/10"
}
Binary file added core/assets/obj/175.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions core/assets/objects.json
Original file line number Diff line number Diff line change
Expand Up @@ -2070,5 +2070,12 @@
"hp" : 3,
"name" : "Clay wall",
"isWall" : true
},
"175" : {
"hp" : 1,
"harvestHp" : 50,
"centered" : true,
"onInteract" : "world.player:skipTime()",
"name" : "Time machine"
}
}

0 comments on commit d8bbea3

Please sign in to comment.