Skip to content

Commit

Permalink
The level of each trade is evaluated more accurately.
Browse files Browse the repository at this point in the history
  • Loading branch information
Estecka committed Sep 22, 2023
1 parent 5735885 commit a7ce0b1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.14.22

# Mod Properties
mod_version=1.0.4
mod_version=1.0.5
maven_group=tk.estecka.shiftingwares
archives_base_name=shifting-wares

Expand Down
67 changes: 32 additions & 35 deletions src/main/java/tk/estecka/shiftingwares/TradeShuffler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.util.math.random.Random;
import net.minecraft.village.TradeOffer;
Expand Down Expand Up @@ -42,25 +44,26 @@ public void Reroll(){
}

MapTradesCache.FillCacheFromTrades(villager);
int tradeIndex = 0;
for (int tradeLvl=VillagerData.MIN_LEVEL; tradeLvl<=jobLevel; ++tradeLvl)
{
final int levelSize = (tradeLvl<VillagerData.MAX_LEVEL) ? 2 : 1;

final TradeOffer[] rerollMap = new TradeOffer[levelSize];
for (int n=0; n<levelSize; ++n)
if (shouldReroll(tradeIndex+n))
rerollMap[n] = ShiftingWares.PLACEHOLDER_TRADE;
IntList slotLevels = new IntArrayList();
for (int lvl=VillagerData.MIN_LEVEL; lvl<=this.jobLevel; ++lvl)
if (jobPool.containsKey(lvl)){
var pool = jobPool.get(lvl);
if (pool == null)
ShiftingWares.LOGGER.error("Missing pool for job {} lvl.{}", job, jobLevel);
else for (int i=0; i<2 && i<pool.length; ++i)
slotLevels.add(lvl);
}

DuplicataAwareReroll(tradeLvl, rerollMap);
for (int i=offers.size()-1; slotLevels.size()<=i; --i)
if (shouldReroll(i))
offers.remove(i);
while(offers.size() < slotLevels.size())
offers.add(ShiftingWares.PLACEHOLDER_TRADE);

for (int tradeLvl=VillagerData.MIN_LEVEL; tradeLvl<=jobLevel; ++tradeLvl)
DuplicataAwareReroll(tradeLvl, slotLevels);

for (int n=0; n<levelSize; ++n, ++tradeIndex) {
while (offers.size() <= tradeIndex)
offers.add(ShiftingWares.PLACEHOLDER_TRADE);
if (rerollMap[n] != null)
offers.set(tradeIndex, rerollMap[n]);
}
}
MapTradesCache.FillCacheFromTrades(villager);
}

Expand All @@ -71,38 +74,32 @@ public boolean shouldReroll(int tradeIndex){
;
}

/**
* @param rerollMap Will attempt to reroll every non-null entry.
*/
private TradeOffer[] DuplicataAwareReroll(int tradeLvl, TradeOffer[] rerollMap){
private void DuplicataAwareReroll(int tradeLvl, IntList slotLevels){
Factory[] levelPool = jobPool.get(tradeLvl);
if (levelPool == null) {
ShiftingWares.LOGGER.error("No trade pool for job {} lvl.{}", job, jobLevel);
return rerollMap;
}
else if (levelPool.length < rerollMap.length) {
ShiftingWares.LOGGER.error("Trade pool smaller than expected for job {} lvl.{}", job, jobLevel);
}
boolean missingSome = false;

var randomPool = new ArrayList<Factory>(levelPool.length);
for (var f : levelPool)
randomPool.add(f);

for (int n=0; n<rerollMap.length; ++n)
if (rerollMap[n] != null)
{
for (int i=0; i<offers.size(); ++i)
if (tradeLvl==slotLevels.getInt(i) && shouldReroll(i)) {
TradeOffer offer = null;
while (offer == null && !randomPool.isEmpty()) {
int roll = random.nextInt(randomPool.size());
offer = randomPool.get(roll).create(villager, random);
randomPool.remove(roll);
}
if (offer == null)
ShiftingWares.LOGGER.warn("Failed to generate a valid offer for {} lvl.{} ({})", job, tradeLvl, villager);
else
rerollMap[n] = offer;
if (offer == null){
offer = ShiftingWares.PLACEHOLDER_TRADE;
missingSome = true;
}

offers.set(i, offer);
}
return rerollMap;

if (missingSome)
ShiftingWares.LOGGER.warn("Failed to generate some trade offers for {} lvl.{} ({})", job, tradeLvl, villager);
}

}

0 comments on commit a7ce0b1

Please sign in to comment.