Skip to content

Commit

Permalink
Generating Shops for Scenario mode, Plain/Hills towns no longer conne…
Browse files Browse the repository at this point in the history
…cted through a portal since they are going to be replaced by Gates later on
  • Loading branch information
tukkek committed Oct 5, 2017
1 parent ae59690 commit 86b252c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 81 deletions.
70 changes: 66 additions & 4 deletions javelin/controller/WorldGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import javelin.controller.db.Preferences;
import javelin.controller.exception.RestartWorldGeneration;
import javelin.controller.generator.feature.FeatureGenerator;
import javelin.controller.scenario.Scenario;
import javelin.controller.terrain.Terrain;
import javelin.controller.walker.Walker;
import javelin.model.Realm;
import javelin.model.unit.Squad;
import javelin.model.world.Actor;
import javelin.model.world.World;
import javelin.model.world.location.Location;
import javelin.model.world.location.Outpost;
import javelin.model.world.location.town.Town;
import javelin.model.world.location.town.governor.MonsterGovernor;
import javelin.view.screen.InfoScreen;
import tyrant.mikera.engine.RPG;

Expand Down Expand Up @@ -63,9 +66,7 @@ public static Actor determinecolor(Point p) {
public void run() {
try {
world = new World();
generate(world);
Town start = FeatureGenerator.SINGLETON
.placestartingfeatures(world);
Town start = generate(world);
for (Actor a : world.actors.get(Town.class)) {
if (a != start) {
((Town) a).populategarisson();
Expand Down Expand Up @@ -184,7 +185,7 @@ public static void retry() {
}
}

public static void generate(World w) {
public static Town generate(World w) {
int size = World.scenario.size;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
Expand All @@ -210,6 +211,67 @@ public static void generate(World w) {
floodedge(ne, se, -1, 0, w);
floodedge(nw, ne, 0, +1, w);
generatetowns(realms, regions);
Town starting = determinestartingtown(w);
normalizemap(starting);
FeatureGenerator.SINGLETON.placestartingfeatures(w, starting);
normalizemap(starting);
return starting;
}

static Town determinestartingtown(World seed) {
Terrain starton = RPG.r(1, 2) == 1 ? Terrain.PLAIN : Terrain.HILL;
ArrayList<Town> towns = Town.gettowns();
Town starting = World.scenario.easystartingtown ? gettown(starton, seed, towns)
: RPG.pick(towns);
if (Terrain.search(new Point(starting.x, starting.y), Terrain.WATER, 2,
seed) != 0) {
throw new RestartWorldGeneration();
}
return starting;
}

static Town gettown(Terrain terrain, World seed, ArrayList<Town> towns) {
Collections.shuffle(towns);
for (Town town : towns) {
if (seed.map[town.x][town.y] == terrain) {
return town;
}
}
if (Javelin.DEBUG) {
throw new RuntimeException("No town in terrain " + terrain);
} else {
throw new RestartWorldGeneration();
}
}

/**
* Turn whole map into 2 {@link Realm}s only so that there won't be
* in-fighting between hostile {@link Town}s.
*
* @param starting
*
* @see Scenario#normalizemap
*/
static void normalizemap(Town starting) {
if (!World.scenario.normalizemap) {
return;
}
ArrayList<Town> towns = Town.gettowns();
towns.remove(starting);
Realm r = towns.get(0).originalrealm;
for (Actor a : World.getactors()) {
if (a instanceof Location) {
Location l = (Location) a;
if (l.realm != null) {
l.realm = r;
if (a instanceof Town) {
Town t = (Town) a;
t.originalrealm = r;
t.replacegovernor(new MonsterGovernor(t));
}
}
}
}
}

static void generatetowns(LinkedList<Realm> realms,
Expand Down
71 changes: 4 additions & 67 deletions javelin/controller/generator/feature/FeatureGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
import java.util.Comparator;
import java.util.HashMap;

import javelin.Javelin;
import javelin.controller.Point;
import javelin.controller.exception.RestartWorldGeneration;
import javelin.controller.terrain.Terrain;
import javelin.controller.upgrade.UpgradeHandler;
import javelin.model.Realm;
import javelin.model.unit.Monster;
import javelin.model.unit.Squad;
import javelin.model.unit.abilities.discipline.Discipline;
Expand All @@ -34,7 +30,6 @@
import javelin.model.world.location.haunt.SunkenShip;
import javelin.model.world.location.haunt.WitchesHideout;
import javelin.model.world.location.town.Town;
import javelin.model.world.location.town.governor.MonsterGovernor;
import javelin.model.world.location.town.labor.basic.Dwelling;
import javelin.model.world.location.town.labor.basic.Lodge;
import javelin.model.world.location.town.labor.criminal.ThievesGuild;
Expand Down Expand Up @@ -212,81 +207,22 @@ static void spawnnear(Town t, Actor a, World w, int min, int max,
}
}

static Town gettown(Terrain terrain, World seed, ArrayList<Town> towns) {
Collections.shuffle(towns);
for (Town town : towns) {
if (seed.map[town.x][town.y] == terrain) {
return town;
}
}
if (Javelin.DEBUG) {
throw new RuntimeException("No town in terrain " + terrain);
} else {
throw new RestartWorldGeneration();
}
}

/**
* @param seed
* Place all world features in this seed.
* @return Starting town for the initial {@link Squad} to be placed nearby.
*/
public Town placestartingfeatures(World seed) {
public void placestartingfeatures(World seed, Town starting) {
Temple.generatetemples();
Terrain starton = RPG.r(1, 2) == 1 ? Terrain.PLAIN : Terrain.HILL;
ArrayList<Town> towns = Town.gettowns();
Town[] easy = getlinkedtowns(seed, starton, towns);
Town starting = (Town) World.get(easy[0].x, easy[0].y, towns);
if (Terrain.search(new Point(starting.x, starting.y), Terrain.WATER, 2,
seed) != 0) {
throw new RestartWorldGeneration();
}
new Portal(starting, World.get(easy[1].x, easy[1].y, towns), false,
false, true, true, null, false).place();
generatestartingarea(seed, easy[0]);
generatelocations(seed, easy[0]);
generatestartingarea(seed, starting);
generatelocations(seed, starting);
for (Class<? extends Actor> feature : generators.keySet()) {
generators.get(feature).seed(feature);
}
int target = World.scenario.startingfeatures - Location.count();
while (countplaces() < target) {
spawn(1, true);
}
if (World.scenario.normalizemap) {
normalizemap(towns, starting);
}
return starting;
}

void normalizemap(ArrayList<Town> towns, Town starting) {
towns = new ArrayList<Town>(towns);
towns.remove(starting);
Realm r = towns.get(0).originalrealm;
for (Actor a : World.getactors()) {
if (a instanceof Location) {
Location l = (Location) a;
if (l.realm != null) {
l.realm = r;
if (a instanceof Town) {
Town t = (Town) a;
t.originalrealm = r;
t.replacegovernor(new MonsterGovernor(t));
}
}
}
}
}

Town[] getlinkedtowns(World w, Terrain t, ArrayList<Town> towns) {
if (World.scenario.linktowns) {
Town a = gettown(t, w, towns);
Town b = gettown(t == Terrain.PLAIN ? Terrain.HILL : Terrain.PLAIN,
w, towns);
return new Town[] { a, b };
} else {
Collections.shuffle(towns);
return new Town[] { towns.get(0), towns.get(1) };
}
}

void generatelocations(World seed, Town easya) {
Expand All @@ -295,6 +231,7 @@ void generatelocations(World seed, Town easya) {
UpgradeHandler.singleton.gather();
generatemageguilds(locations);
generateacademies(locations);
locations.addAll(World.scenario.generatelocations(seed));
Collections.shuffle(locations);
int place = Math.min(locations.size(),
World.scenario.startingfeatures / 3 - countplaces());
Expand Down
13 changes: 11 additions & 2 deletions javelin/controller/scenario/Campaign.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package javelin.controller.scenario;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javelin.controller.challenge.CrCalculator;
import javelin.controller.challenge.RewardCalculator;
Expand All @@ -9,6 +11,8 @@
import javelin.model.Realm;
import javelin.model.unit.Squad;
import javelin.model.unit.attack.Combatant;
import javelin.model.world.World;
import javelin.model.world.location.Location;

public class Campaign extends Scenario {
/** Minimum starting party encounter level. */
Expand All @@ -24,7 +28,7 @@ public Campaign() {
fogofwar = true;
haxor = true;
helpfile = "How to play";
linktowns = true;
easystartingtown = true;
minigames = true;
normalizemap = false;
record = true;
Expand Down Expand Up @@ -70,4 +74,9 @@ public boolean checkfullsquad(ArrayList<Combatant> squad) {
public boolean win() {
return false;
}
}

@Override
public List<Location> generatelocations(World seed) {
return Collections.EMPTY_LIST;
}
}
32 changes: 24 additions & 8 deletions javelin/controller/scenario/Scenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import javelin.Javelin;
import javelin.controller.action.world.Guide;
Expand All @@ -14,8 +16,7 @@
import javelin.controller.generator.feature.GenerationData;
import javelin.controller.kit.Kit;
import javelin.controller.terrain.Desert;
import javelin.controller.terrain.Hill;
import javelin.controller.terrain.Plains;
import javelin.controller.terrain.Terrain;
import javelin.controller.terrain.Water;
import javelin.controller.terrain.hazard.Hazard;
import javelin.controller.upgrade.Upgrade;
Expand All @@ -28,7 +29,7 @@
import javelin.model.world.Incursion;
import javelin.model.world.World;
import javelin.model.world.location.Location;
import javelin.model.world.location.Portal;
import javelin.model.world.location.dungeon.Chest;
import javelin.model.world.location.fortification.Fortification;
import javelin.model.world.location.fortification.Trove;
import javelin.model.world.location.town.Town;
Expand Down Expand Up @@ -101,10 +102,10 @@ public class Scenario implements Serializable {
/** Wheter {@link Key}s should exist at all. */
public boolean allowkeys = false;
/**
* Wheter to link {@link Plains} and {@link Hill} {@link Town}s.through a
* {@link Portal}.
* <code>true</code> if first {@link Town} should be located on
* {@link Terrain#PLAIN} or {@link Terrain#HILL}.
*/
public boolean linktowns = false;
public boolean easystartingtown = false;
/** Minimum distance between {@link Desert} and {@link Water}. */
public int desertradius = 1;
/** Number of {@link Town}s in the {@link World}. */
Expand Down Expand Up @@ -203,8 +204,23 @@ public boolean win() {
return false;
}
}
Javelin.show(
"Congratulations, you have won this scenario!\nThanks for playing!");
String win = "Congratulations, you have won this scenario!\nThanks for playing!";
Javelin.show(win);
return true;
}

public List<Location> generatelocations(World seed) {
HashSet<Realm> realms = new HashSet<Realm>(2);
for (Actor a : seed.actors.get(Town.class)) {
Town t = (Town) a;
realms.add(t.originalrealm);
}
ArrayList<Location> shops = new ArrayList<Location>();
for (Realm r : Realm.values()) {
if (!realms.contains(r)) {
shops.add(new Shop(false, r));
}
}
return shops;
}
}

0 comments on commit 86b252c

Please sign in to comment.