From 72fac6d84e504abdefa010a9095454df5ff9b06c Mon Sep 17 00:00:00 2001 From: "jason@long.name" Date: Sat, 28 Sep 2013 16:33:56 +0000 Subject: [PATCH] refactor: move traffic-density mutation code to new function git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@861 d9718cc8-9f43-0410-858b-315f434eb58c --- src/micropolisj/engine/Micropolis.java | 30 ++++++++++++++++++++++++++ src/micropolisj/engine/TrafficGen.java | 24 ++------------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/micropolisj/engine/Micropolis.java b/src/micropolisj/engine/Micropolis.java index 0d04e57..e175cf3 100644 --- a/src/micropolisj/engine/Micropolis.java +++ b/src/micropolisj/engine/Micropolis.java @@ -1028,6 +1028,36 @@ public class Micropolis } } + /** + * Increase the traffic-density measurement at a particular + * spot. + * @param traffic the amount to add to the density + */ + void addTraffic(int mapX, int mapY, int traffic) + { + int z = trfDensity[mapY/2][mapX/2]; + z += traffic; + + //FIXME- why is this only capped to 240 + // by random chance. why is there no cap + // the rest of the time? + + if (z > 240 && PRNG.nextInt(6) == 0) + { + z = 240; + trafficMaxLocationX = mapX; + trafficMaxLocationY = mapY; + + HelicopterSprite copter = (HelicopterSprite) getSprite(SpriteKind.COP); + if (copter != null) { + copter.destX = mapX; + copter.destY = mapY; + } + } + + trfDensity[mapY/2][mapX/2] = z; + } + public int getTrafficDensity(int xpos, int ypos) { if (testBounds(xpos, ypos)) { diff --git a/src/micropolisj/engine/TrafficGen.java b/src/micropolisj/engine/TrafficGen.java index 4f35536..21097bf 100644 --- a/src/micropolisj/engine/TrafficGen.java +++ b/src/micropolisj/engine/TrafficGen.java @@ -60,31 +60,11 @@ class TrafficGen mapY = pos.y; assert city.testBounds(mapX, mapY); + // check for road/rail int tile = city.getTile(mapX, mapY) & LOMASK; if (tile >= ROADBASE && tile < POWERBASE) { - // check for rail - int z = city.trfDensity[mapY/2][mapX/2]; - z += 50; - - //FIXME- why is this only capped to 240 - // by random chance. why is there no cap - // the rest of the time? - - if (z > 240 && city.PRNG.nextInt(6) == 0) - { - z = 240; - city.trafficMaxLocationX = mapX; - city.trafficMaxLocationY = mapY; - - HelicopterSprite copter = (HelicopterSprite) city.getSprite(SpriteKind.COP); - if (copter != null) { - copter.destX = mapX; - copter.destY = mapY; - } - } - - city.trfDensity[mapY/2][mapX/2] = z; + city.addTraffic(mapX, mapY, 50); } } }