Skip to content

Commit

Permalink
Read buildRange (in tiles) from rules.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhendriks committed Oct 5, 2016
1 parent b5783f7 commit 18eacbc
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class EntityData {
private float chop = -1f;
private float halfChop = -1f;
public float buildTimeInSeconds = 5.0F;
public float buildRange = Game.HALF_TILE + (3 * Game.TILE_SIZE);
public float buildRange = 0F;

public EntityType type;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fundynamic.d2tm.game.entities.entitiesdata;


import com.fundynamic.d2tm.Game;
import com.fundynamic.d2tm.game.entities.EntityData;
import com.fundynamic.d2tm.game.entities.EntityNotFoundException;
import com.fundynamic.d2tm.game.entities.EntityType;
Expand Down Expand Up @@ -100,6 +101,35 @@ public EntityData addStructure(String id, IniDataStructure iniDataStructure) thr
iniDataStructure.hitpoints
);

// The buildRange is (for now) determined by the size of the structure.
// Because the range is calculated from the center of the structure.
// In order to make it 'fair' for larger structures (if any would appear),
// we add 'half' of the structure to the range.
//
// For a constyard it is a square, so 64x64 pixels = 1 'half', meaning:
//
// (64/64)/2 = 0,5 * 32 (tile width) = 16 pixels
//
// A larger structure would be (width/height), ie a heavy factory thing would be:
// (96/64)/2 = ,75 * 32 = 24 pixels.
//
// Although on every squared structure this would even out fine, but then the
// 'buildRange' should have one tile extra because (again) it is calculated
// from the center
//
// this is all weird , then again, fixing this would require to check for every cell
// on the structure which basically makes calculating the 'can I place it in this distance' logic
// Width*height times more consuming.
//
// Unless....
//
// We do the 'computed map' thing (where we have all entity data on a map attached), so we don't
// need to do an expensive lookup in the EntityRepository
//TODO: Do something about the above, for now accept its quirks
float someRatio = (float)entityData.getWidth() / (float)entityData.getHeight();
int extraFromCenter = (int)((someRatio / 2) * Game.TILE_SIZE); // this is seriously flawed :/ (too tired to fix now)
// add additional '1' to get 'past the center and occupy one cell'.
entityData.buildRange = extraFromCenter + ((1 + iniDataStructure.buildRangeInTiles) * Game.TILE_SIZE);
entityData.entityBuilderType = iniDataStructure.getEntityBuilderType();
entityData.buildTimeInSeconds = iniDataStructure.buildTimeInSeconds;
entityData.buildList = iniDataStructure.buildList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class EntitiesDataReader {
public static final String INI_KEYWORD_BUILD_ICON = "BuildIcon";
public static final String INI_KEYWORD_BUILDS = "Builds";
public static final String INI_KEYWORD_BUILD_TIME = "BuildTime";
public static final String INI_KEYWORD_BUILD_RANGE = "BuildRange";
public static final String INI_KEYWORD_BUILD_LIST = "BuildList";
public static final String INI_KEYWORD_FPS = "Fps";
public static final String INI_KEYWORD_RECOLOR = "Recolor";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public class IniDataStructure {
public int height;
public int sight;
public int hitpoints;
public int buildRangeInTiles;
public String explosion;
public String buildIcon;
public String entityBuilderType;
public float buildTimeInSeconds;

public String buildList;

public IniDataStructure() {
Expand All @@ -37,6 +39,7 @@ public IniDataStructure(Profile.Section struct) {
this.entityBuilderType = struct.get(INI_KEYWORD_BUILDS, String.class, "");
this.buildTimeInSeconds = struct.get(INI_KEYWORD_BUILD_TIME, Float.class, 0F);
this.buildList = struct.get(INI_KEYWORD_BUILD_LIST, String.class, "");
this.buildRangeInTiles = struct.get(INI_KEYWORD_BUILD_RANGE, Integer.class, 0);
}

public EntityBuilderType getEntityBuilderType() {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/rules.ini
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Builds=STRUCTURES
BuildIcon=ui/icons/icon_constyard.bmp
BuildList=WINDTRAP,REFINERY,BARRACKS,LIGHTFACTORY,HEAVYFACTORY
BuildTime=30
BuildRange=2

[STRUCTURES/REFINERY]
Image=structures/3x2_refinery.png
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fundynamic.d2tm.game.entities.entitiesdata;

import com.fundynamic.d2tm.Game;
import com.fundynamic.d2tm.game.entities.EntityData;
import com.fundynamic.d2tm.game.entities.EntityType;
import com.fundynamic.d2tm.game.entities.entitybuilders.EntityBuilderType;
Expand Down Expand Up @@ -43,6 +44,11 @@ public void readsBuildingStructureFromIniFile() {
assertThat(constyard.explosionId, is("BOOM"));
assertThat(constyard.buildIcon, is(not(nullValue())));
assertThat(constyard.entityBuilderType, is(EntityBuilderType.STRUCTURES));

// 1 extra tile range is added by the EntitiesData class (while it is '2' in the test-rules.ini!)
// therefor we do times 3!
float value = ((Game.TILE_SIZE) * 3) + Game.HALF_TILE; // we can do half-tile because it is a 64x64 structure
assertThat(constyard.buildRange, is(value)); // calculated by entitiesData class
}

@Test
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/test-rules.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ Height=64
Sight=4
Explosion=BOOM
Builds=STRUCTURES
# BuildRange of 1 means attached to building
# BuildRange of 2 means you can have 1 tile in between
BuildRange=2

[STRUCTURES/WINDTRAP]
Image=structures/2x2_windtrap.bmp
Expand Down

0 comments on commit 18eacbc

Please sign in to comment.