Proper building detection

This commit is contained in:
2023-11-10 05:54:40 +01:00
parent 3fdd24d951
commit 60e66bbd67
6 changed files with 123 additions and 51 deletions

View File

@@ -292,6 +292,14 @@
"id":36,
"type":"fletcher"
},
{
"id":38,
"type":"mine"
},
{
"id":39,
"type":"mine"
},
{
"id":40,
"type":"baracks"

View File

@@ -79,8 +79,10 @@ BzTileset bzTilesetCreate(const BzTilesetDesc *desc) {
}
BzTile bzTilesetGetTile(BzTileset *tileset, BzTile tile) {
if (tile == 0)
return -1;
tile = tile - tileset->startID;
BZ_ASSERT(tile >= tile && tile < tileset->tileCount);
BZ_ASSERT(tile >= 0 && tile < tileset->tileCount);
return tile;
}
Rectangle bzTilesetGetTileRegion(BzTileset *tileset, BzTile tileID) {

View File

@@ -53,27 +53,6 @@ bool prepareBuildingLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
return false;
}
static void detectSize(BzTile *const data, BzTile * const ownData,
const BzTile tile, const BzTile ownerTile,
const i32 width, const i32 height,
BzTile x, BzTile y, TileSize * const max) {
if (x >= width || y >= height) return;
BzTile *curTile = data + y * width + x;
BzTile *curOwn = ownData + y * width + x;
if (*curTile != tile || *curOwn != ownerTile) return;
max->w = max->w > x ? max->w : x;
max->h = max->h > y ? max->h : y;
*curTile = 0;
*curOwn = 0;
detectSize(data, ownData, tile, ownerTile, width, height, x + 1, y, max);
detectSize(data, ownData, tile, ownerTile, width, height, x, y + 1, max);
}
bool handleBuildLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
ECS_COMPONENT(ECS, TilePosition);
ECS_COMPONENT(ECS, TileSize);
@@ -86,16 +65,10 @@ bool handleBuildLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
for (i32 x = 0; x < layer->width; x++) {
BzTile *tile = data + y * layer->width + x;
BzTile *ownTile = ownData + y * layer->width + x;
if (*tile == 0) continue;
if (*tile == BUILDINGS_NONE) continue;
// We have a building
TileSize size = {.w=(BzTile)x, .h=(BzTile)y};
if (*tile != BUILDINGS_ROAD) {
detectSize(data, ownData, *tile, *ownTile,
layer->width, layer->height, (BzTile) x, (BzTile) y, &size);
}
size.w -= x - 1;
size.h -= y - 1;
TileSize size = {};
getBuildingSize(*tile, &size.w, &size.h);
bzLogInfo("Got size: %2d %2d", size.w, size.h);
ecs_entity_t e = ecs_new_id(ECS);
@@ -105,7 +78,7 @@ bool handleBuildLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
bzTileMapUpdateCollider(&GAME.map, x, y);
}
}
return true;
return false;
}
bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
@@ -123,9 +96,17 @@ bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
sizeX += 2;
sizeY += 2;
BzTileLayer *buildLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
for (i32 y = tileY; y < tileY + sizeY; y++) {
for (i32 x = tileX; x < tileX + sizeX; x++) {
if (x != tileX && x != tileX + sizeX - 1 &&
y != tileY && y != tileY + sizeY - 1) {
// Without padding
BzTile tile = bzTileLayerGetTile(buildLayer, x, y);
if (tile == BUILDINGS_ROAD)
return false;
}
BzTileCollider collider = bzTileMapGetCollider(map, x, y);
f32 posX = x * map->tileWidth;
f32 posY = y * map->tileHeight;

View File

@@ -27,7 +27,7 @@ typedef enum BUILDINGS {
} BUILDINGS;
static bool getTileBuilding(BzTile tile) {
static BUILDINGS getTileBuilding(BzTile tile) {
switch (tile) {
case 0:
case 1:
@@ -47,6 +47,8 @@ static bool getTileBuilding(BzTile tile) {
return BUILDINGS_WAREHOUSE;
case 6:
case 7:
case 38:
case 39:
return BUILDINGS_MINE;
case 8:
case 9:
@@ -93,22 +95,99 @@ static bool getTileBuilding(BzTile tile) {
}
static BUILDINGS getBuildingFromStr(const char *str) {
if (strncmp("keep", str, 4)) return BUILDINGS_KEEP;
if (strncmp("granary", str, 7)) return BUILDINGS_GRANARY;
if (strncmp("armory", str, 6)) return BUILDINGS_ARMORY;
if (strncmp("warehouse", str, 9)) return BUILDINGS_WAREHOUSE;
if (strncmp("mine", str, 4)) return BUILDINGS_MINE;
if (strncmp("baracks", str, 7)) return BUILDINGS_BARACKS;
if (strncmp("orchard", str, 7)) return BUILDINGS_ORCHARD;
if (strncmp("animal_farm", str, 11)) return BUILDINGS_ANIMAL_FARM;
if (strncmp("fletcher", str, 8)) return BUILDINGS_FLETCHER;
if (strncmp("smithy", str, 6)) return BUILDINGS_SMITHY;
if (strncmp("workshop", str, 8)) return BUILDINGS_WORKSHOP;
if (strncmp("farm", str, 4)) return BUILDINGS_FARM;
if (strncmp("road", str, 4)) return BUILDINGS_ROAD;
if (strncmp("wall", str, 4)) return BUILDINGS_WALL;
if (strncmp("gatehouse", str, 9)) return BUILDINGS_GATEHOUSE;
if (strncmp("tower", str, 5)) return BUILDINGS_TOWER;
if (strncmp("small_tower", str, 11)) return BUILDINGS_SMALL_TOWER;
if (strncmp("keep", str, 4) == 0) return BUILDINGS_KEEP;
if (strncmp("granary", str, 7) == 0) return BUILDINGS_GRANARY;
if (strncmp("armory", str, 6) == 0) return BUILDINGS_ARMORY;
if (strncmp("warehouse", str, 9) == 0) return BUILDINGS_WAREHOUSE;
if (strncmp("mine", str, 4) == 0) return BUILDINGS_MINE;
if (strncmp("baracks", str, 7) == 0) return BUILDINGS_BARACKS;
if (strncmp("orchard", str, 7) == 0) return BUILDINGS_ORCHARD;
if (strncmp("animal_farm", str, 11) == 0) return BUILDINGS_ANIMAL_FARM;
if (strncmp("fletcher", str, 8) == 0) return BUILDINGS_FLETCHER;
if (strncmp("smithy", str, 6) == 0) return BUILDINGS_SMITHY;
if (strncmp("workshop", str, 8) == 0) return BUILDINGS_WORKSHOP;
if (strncmp("farm", str, 4) == 0) return BUILDINGS_FARM;
if (strncmp("road", str, 4) == 0) return BUILDINGS_ROAD;
if (strncmp("wall", str, 4) == 0) return BUILDINGS_WALL;
if (strncmp("gatehouse", str, 9) == 0) return BUILDINGS_GATEHOUSE;
if (strncmp("tower", str, 5) == 0) return BUILDINGS_TOWER;
if (strncmp("small_tower", str, 11) == 0) return BUILDINGS_SMALL_TOWER;
else return BUILDINGS_NONE;
}
static void getBuildingSize(BUILDINGS type, BzTile *outWidth, BzTile *outHeight) {
switch (type) {
case BUILDINGS_KEEP:
if (outWidth) *outWidth = 3;
if (outHeight) *outHeight = 3;
break;
case BUILDINGS_GRANARY:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_ARMORY:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_WAREHOUSE:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_MINE:
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 2;
break;
case BUILDINGS_BARACKS:
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 2;
break;
case BUILDINGS_ORCHARD:
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 2;
break;
case BUILDINGS_ANIMAL_FARM:
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 2;
break;
case BUILDINGS_FLETCHER:
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_SMITHY:
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_WORKSHOP:
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_FARM:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_ROAD:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_WALL:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_GATEHOUSE:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
case BUILDINGS_TOWER:
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 2;
break;
case BUILDINGS_SMALL_TOWER:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
default:
if (outWidth) *outWidth = 0;
if (outHeight) *outHeight = 0;
break;
}
}

View File

@@ -65,7 +65,7 @@ print()
# ============================
print(f"{indent()}static bool getTileBuilding(BzTile tile) {{")
print(f"{indent()}static {enum_name} getTileBuilding(BzTile tile) {{")
indent_level += indent_offset
print(f"{indent()}switch (tile) {{")
for enum, ids in types.items():

View File

@@ -59,6 +59,8 @@
</tile>
<tile id="35" type="fletcher"/>
<tile id="36" type="fletcher"/>
<tile id="38" type="mine"/>
<tile id="39" type="mine"/>
<tile id="40" type="baracks"/>
<tile id="41" type="baracks"/>
<tile id="42" type="orchard"/>