Skip to content

Commit

Permalink
AbstractEquipment "cursed" & "identified" flag rolls now depend on pl…
Browse files Browse the repository at this point in the history
…ayer's luck

* Chance to add items from a random ItemCategory  to dungeon floor based on player's luck
* New item quantity cap system when rolling from ItemCategoryLists
  • Loading branch information
Hitonoriol committed Aug 10, 2021
1 parent 84fa107 commit e5384cc
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 90 deletions.
3 changes: 0 additions & 3 deletions core/src/hitonoriol/madsand/GameSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ private static boolean saveWorld() {
File worldFile = new File(getCurSaveDir() + MadSand.WORLDFILE);
Player player = MadSand.player();

if (player.newlyCreated)
player.newlyCreated = false;

player.stats.equipment.setStatBonus(false);
/*Resources.mapper.writeValue(new File(fl), player);*/
getMapper().writeValue(worldFile, MadSand.world());
Expand Down
7 changes: 3 additions & 4 deletions core/src/hitonoriol/madsand/MadSand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class MadSand extends Game {
public static String WORLDNAME = "";

private static boolean worldUntouched = true;
private long startTime = System.currentTimeMillis();

private World world;
private Storage<AbstractScreen<?>> currentScreen = new Storage<>();
Expand All @@ -51,8 +52,6 @@ public class MadSand extends Game {
public static DeathScreen deathScreen;
public static MainMenu mainMenu;

private long startTime = Utils.now();

public void create() {
Utils.out("Starting initialization!");
game = this;
Expand All @@ -73,7 +72,7 @@ public void create() {
game.world.generate();
Keyboard.initDefaultKeyBinds();
switchScreen(mainMenu);
Utils.out("End of initialization!");
Utils.out("End of initialization (%.3f sec spent)", Utils.toSeconds(System.currentTimeMillis() - startTime));
Utils.printMemoryInfo();
}

Expand Down Expand Up @@ -140,7 +139,7 @@ public void pause() {}
@Override
public void dispose() {
Prefs.savePrefs();
Utils.out("Bye! Session lasted for [%s]", Utils.timeString(Utils.now() - startTime));
Utils.out("Bye! Session lasted for [%s]", Utils.timeString((long) (Utils.now() - Utils.toSeconds(startTime))));
}

public static OrthographicCamera getCamera() {
Expand Down
61 changes: 56 additions & 5 deletions core/src/hitonoriol/madsand/entities/BaseStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@
import hitonoriol.madsand.util.Utils;

public class BaseStats extends HashMap<Stat, Integer> {
public static final int MAX_LVL = 35;
private static final int RAND_MAX = 8, RAND_MIN = 1;
private final static int ROLLABLE_STATS = Stat.rollableStats.size();
public static final int MAX_LVL = 35, MAX_SUM = MAX_LVL * ROLLABLE_STATS;
private static final int RAND_MAX = 8, RAND_MIN = 1;
private static final int DEF_MAX_SUM = 15;

public int maxStatSum = DEF_MAX_SUM;

public BaseStats() {
super();
}
public BaseStats() {}

public BaseStats(BaseStats stats) {
set(stats);
}

public double getOverallProgress() {
return (double) getSum() / (double) MAX_SUM;
}

public BaseStats prepareLivingCreature() {
Stat.rollableStats.forEach(stat -> set(stat, 1));
return this;
}

public int get(Stat stat) {
return super.getOrDefault(stat, 0);
}
Expand Down Expand Up @@ -93,6 +100,7 @@ public int getFreePoints() {
return maxStatSum - getSum();
}

/* Stat effectiveness rating in %: [0; 100] */
private static double getEffectiveness(Stat stat, double lvl) {
switch (stat) {
case Luck:
Expand All @@ -114,6 +122,49 @@ public double getEffectiveness(Stat stat) {
return getEffectiveness(stat, Math.min(MAX_LVL, get(stat)));
}

public double maxEffectiveness(Stat stat) {
return getEffectiveness(stat, MAX_LVL);
}

private double toMax(Stat stat) {
return getEffectiveness(stat) / maxEffectiveness(stat);
}

private double toMaxInverse(Stat stat) {
double max = maxEffectiveness(stat);
return (max - getEffectiveness(stat)) / max;
}

/* Equivalent to (roll() && roll() && ...) */
public boolean rollAnd(Stat stat, int times) {
if (times < 2)
return roll(stat);

double combinedProb = Math.pow(getEffectiveness(stat) / 100, times) * 100;
Utils.dbg("Rolling %s[%d] &&%d times = %.5f%%", stat, get(stat), times, combinedProb);
return Utils.percentRoll(combinedProb);
}

/* Equivalent to (roll() || roll() || ...) */
public boolean rollOr(Stat stat, int times) {
if (times < 2)
return roll(stat);

for (int i = 0; i < times; ++i)
if (roll(stat))
return true;
return false;
}

/* !roll() equivalent, but with max probability capped at <probCap>% */
public boolean rollInverse(Stat stat, double probCap) {
return Utils.percentRoll(toMaxInverse(stat) * probCap);
}

public boolean roll(Stat stat, double probCap) {
return Utils.percentRoll(toMax(stat) * probCap);
}

public boolean roll(Stat stat) {
return Utils.percentRoll(getEffectiveness(stat));
}
Expand Down
5 changes: 0 additions & 5 deletions core/src/hitonoriol/madsand/entities/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ public void setName(String name) {
stats.name = name;
}

public void reinit() {
initInventory();
inventory.setMaxWeight(stats.calcMaxInventoryWeight());
}

public boolean memberOf(Faction faction) {
return stats.faction == faction;
}
Expand Down
22 changes: 16 additions & 6 deletions core/src/hitonoriol/madsand/entities/Player.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hitonoriol.madsand.entities;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -30,6 +31,7 @@
import hitonoriol.madsand.containers.Pair;
import hitonoriol.madsand.dialog.DialogChainGenerator;
import hitonoriol.madsand.dialog.GameDialog;
import hitonoriol.madsand.dialog.GameTextSubstitutor;
import hitonoriol.madsand.entities.ability.Ability;
import hitonoriol.madsand.entities.ability.ActiveAbility;
import hitonoriol.madsand.entities.equipment.EquipSlot;
Expand Down Expand Up @@ -108,8 +110,8 @@ public class Player extends Entity {

private Runnable scheduledAction, afterMovement;

@JsonProperty("newlyCreated")
public boolean newlyCreated = true;
@JsonProperty
private boolean newlyCreated = true;

static {
loadPlayerAnimation();
Expand Down Expand Up @@ -143,6 +145,14 @@ public void postLoadInit() {
refreshAvailableRecipes();
setFov();
}

public void finishCreation() {
newlyCreated = false;
}

public boolean uninitialized() {
return newlyCreated;
}

private static final int ANIM_WIDTH = 35, ANIM_HEIGHT = 74;
private static final float animDuration = 0.2f;
Expand Down Expand Up @@ -268,6 +278,7 @@ public void refreshEquipment() {
@JsonIgnore
public void setName(String name) {
super.setName(name);
GameTextSubstitutor.add(GameTextSubstitutor.PLAYER_NAME, name);
}

public void unTarget() {
Expand Down Expand Up @@ -546,11 +557,10 @@ void satiate(int amt) {
}

void increaseStamina(int to) {
if (stats.stamina + to < stats.maxstamina) {
if (stats.stamina + to < stats.maxstamina)
stats.stamina += to;
} else {
else
stats.stamina = stats.maxstamina;
}
}

public void changeStamina(float by) {
Expand Down Expand Up @@ -652,7 +662,7 @@ public String buildRecipeProgress() {

public void unlockCraftRecipe(int recipe) {
unlockRecipe(craftRecipes, recipe);
showItemUnlockNotification(List.of(recipe));
showItemUnlockNotification(Arrays.asList(recipe));
}

public void unlockBuildRecipe(int recipe) {
Expand Down
7 changes: 6 additions & 1 deletion core/src/hitonoriol/madsand/entities/Stats.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Stats {
protected Entity owner;

public Stats(Entity owner) {
baseStats = new BaseStats();
baseStats = new BaseStats().prepareLivingCreature();
setOwner(owner);
}

Expand All @@ -73,6 +73,10 @@ public void set(Stat stat, int value) {
baseStats.set(stat, value);
}

public boolean roll(Stat stat, int times) {
return baseStats.rollAnd(stat, times);
}

public boolean roll(Stat stat) {
return baseStats.roll(stat);
}
Expand All @@ -96,6 +100,7 @@ public void calcStats() {

calcSpeed();
hp = Math.min(hp, mhp);
owner.inventory.setMaxWeight(calcMaxInventoryWeight());
}

public void restore() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.fasterxml.jackson.annotation.JsonIgnore;

import hitonoriol.madsand.MadSand;
import hitonoriol.madsand.entities.BaseStats;
import hitonoriol.madsand.entities.Player;
import hitonoriol.madsand.entities.Stat;
import hitonoriol.madsand.gfx.ConditionalEffects;
import hitonoriol.madsand.gfx.Effects;
import hitonoriol.madsand.util.Functional;
Expand All @@ -33,10 +35,11 @@ public AbstractEquipment(AbstractEquipment protoItem) {
hp = protoItem.hp;
maxHp = hp;

BaseStats stats = MadSand.player().stats().baseStats;
if (protoItem.isProto()) {
uid = MadSand.world().itemCounter().incrementAndGet();
cursed = Utils.percentRoll(7.5);
identified = Utils.percentRoll(15);
cursed = Utils.percentRoll(5.5) || stats.rollInverse(Stat.Luck, 17.5);
identified = Utils.percentRoll(12.5) || stats.roll(Stat.Luck);
} else {
uid = protoItem.uid;
cursed = protoItem.cursed;
Expand Down
Loading

0 comments on commit e5384cc

Please sign in to comment.