Skip to content

Commit

Permalink
normalized 'oddsOfAppearance'
Browse files Browse the repository at this point in the history
  • Loading branch information
trichner committed Mar 11, 2014
1 parent c6d449e commit a3a8128
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ out
.metadata
.idea
Metropolis.iml
bin
metropolis.properties
38 changes: 17 additions & 21 deletions src/ch/k42/metropolis/grid/urbanGrid/districts/District.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,40 @@ private Parcel placeRandom(Cartesian2D base,Cartesian2D size){
Direction direction = findRoad(base, size);
List<Clipboard> clips = grid.getClipboardProvider().getFit(size, context, direction);
if(clips.size()!=0){ // can we place anything?
Collections.shuffle(clips);
List<Integer> sums = new LinkedList<>();
for(Clipboard c : clips){
if(grid.getRandom().getChance(c.getConfig().getOddsOfAppearance())){
return new ClipboardParcel(grid, base.X, base.Y, size.X, size.Y, c, context, direction);
}
sums.add(c.getConfig().getOddsOfAppearance());
}
return new ClipboardParcel(grid, base.X, base.Y, size.X, size.Y, clips.get(Minions.getRandomWeighted(sums,grid.getRandom())), context, direction);
}
return null;
}

private Parcel placeRandomForSure(Cartesian2D base,Cartesian2D size){
private Parcel placeRandom(Cartesian2D base,Cartesian2D size,int buildChance){
if(!(grid.getRandom().getChance(buildChance))) return null;
Direction direction = findRoad(base, size);
List<Clipboard> clips = grid.getClipboardProvider().getFit(size, context, direction);
if(clips.size()!=0){ // can we place anything?
Collections.shuffle(clips);
for(int i=0;i<20;i++){
for(Clipboard c : clips){
if(grid.getRandom().getChance(c.getConfig().getOddsOfAppearance())){
return new ClipboardParcel(grid, base.X, base.Y, size.X, size.Y, c, context, direction);
}
}
List<Integer> sums = new LinkedList<>();
for(Clipboard c : clips){
sums.add(c.getConfig().getOddsOfAppearance());
}
Bukkit.getLogger().info("Couldn't place schem for sure! Tried 20 times, but all odds failed me.");
// you failed! just take the first one
return new ClipboardParcel(grid, base.X, base.Y, size.X, size.Y, clips.get(0), context, direction);
return new ClipboardParcel(grid, base.X, base.Y, size.X, size.Y, clips.get(Minions.getRandomWeighted(sums,grid.getRandom())), context, direction);
}
return null;
}

private static final int BUILD_CHANCE = 80;

private int recPartitionX(Set<Parcel> parcels,Cartesian2D base, Cartesian2D size){
Parcel parcel;
if(size.X<2){ // can't make smaller
if((parcel=placeRandom(base,size))!=null){
if((parcel=placeRandom(base,size,BUILD_CHANCE))!=null){
parcels.add(parcel);
return Minions.square(size.X * size.Y);
}
if(size.Y<2){
if((parcel=placeRandomForSure(base,size))==null){
if((parcel=placeRandom(base, size))==null){
parcels.add(new EmptyParcel(grid,base.X,base.Y,size.X,size.Y));
return 0;
}else {
Expand All @@ -91,7 +87,7 @@ private int recPartitionX(Set<Parcel> parcels,Cartesian2D base, Cartesian2D size
}
return recPartitionZ(parcels,base, size);
}else {
if((parcel=placeRandom(base,size))!=null){
if((parcel=placeRandom(base,size,BUILD_CHANCE))!=null){
parcels.add(parcel);
return Minions.square(size.X * size.Y);
}else {
Expand All @@ -107,12 +103,12 @@ private int recPartitionX(Set<Parcel> parcels,Cartesian2D base, Cartesian2D size
private int recPartitionZ(Set<Parcel> parcels,Cartesian2D base, Cartesian2D size){
Parcel parcel;
if(size.Y<2){ // can't make smaller
if((parcel=placeRandom(base,size))!=null){
if((parcel=placeRandom(base,size,BUILD_CHANCE))!=null){
parcels.add(parcel);
return Minions.square(size.X * size.Y);
}
if(size.X<2){
if((parcel=placeRandomForSure(base,size))==null){
if((parcel=placeRandom(base, size))==null){
parcels.add(new EmptyParcel(grid,base.X,base.Y,size.X,size.Y));
return 0;
}else {
Expand All @@ -122,7 +118,7 @@ private int recPartitionZ(Set<Parcel> parcels,Cartesian2D base, Cartesian2D size
}
return recPartitionX(parcels,base, size);
}else {
if((parcel=placeRandom(base,size))!=null){
if((parcel=placeRandom(base,size,BUILD_CHANCE))!=null){
parcels.add(parcel);
return Minions.square(size.X * size.Y);
}else {
Expand Down
22 changes: 22 additions & 0 deletions src/ch/k42/metropolis/minions/Minions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import ch.k42.metropolis.generator.MetropolisGenerator;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.generator.ChunkGenerator;
import sun.util.logging.resources.logging_de;

import java.io.*;
import java.security.MessageDigest;
Expand Down Expand Up @@ -119,4 +121,24 @@ public static final int getNormalCut(GridRandom random,double mean, double sigma
public static final int square(int x){
return x*x;
}

/**
* Selects a random entry from a List
*
* @param rand a number between [0,SUM]
* @return
*/
public static int getRandomWeighted(List<Integer> odds,GridRandom rand) {
int[] thresholds = new int[odds.size()];
thresholds[0] = odds.get(0);
for (int i = 1; i < odds.size(); i++) {
thresholds[i] = thresholds[i-1] + odds.get(i);
}
int random = rand.getRandomInt(thresholds[thresholds.length-1]);
for (int i = 0; i < thresholds.length; i++) {
if (random < thresholds[i])
return i;
}
return 0; // something went wrong
}
}

0 comments on commit a3a8128

Please sign in to comment.