Skip to content

Commit

Permalink
Fix static map tile drawing (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Jan 15, 2019
1 parent 609a0a3 commit 0b36c70
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
28 changes: 13 additions & 15 deletions src/cdogs/map_build.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,27 +697,23 @@ const TileClass *MapBuildGetTileFromType(const uint16_t tile, bool *isRoom)
// TODO: change static map building
void MapBuildTile(
Map *m, const Mission *mission, const struct vec2i pos,
const TileClass *tile)
const TileClass *tile, const bool isRoom)
{
MapBuilder mb;
MapBuilderInit(&mb, m, mission, NULL);
// Load tiles from +2 perimeter
RECT_FOREACH(Rect2iNew(svec2i_subtract(pos, svec2i(2, 2)), svec2i(5, 5)))
MapStaticLoadTile(&mb, _v);
RECT_FOREACH_END()
// Update the tile as well, plus neighbours as they may be affected
// by shadows etc. especially walls
MapBuilderSetTile(&mb, pos, tile, false);
MapBuilderSetTile(&mb, pos, tile, isRoom);
MapSetupTile(&mb, pos);
struct vec2i v;
for (v.y = pos.y - 1; v.y <= pos.y + 1; v.y++)
{
for (v.x = pos.x - 1; v.x <= pos.x + 1; v.x++)
{
if (!MapIsTileIn(m, v))
{
continue;
}
MapSetupTile(&mb, v);
}
}
RECT_FOREACH(Rect2iNew(svec2i_subtract(pos, svec2i(1, 1)), svec2i(3, 3)))
MapSetupTile(&mb, _v);
RECT_FOREACH_END()
CArrayCopy(&mb.Map->access, &mb.access);
DebugPrintMap(&mb);
MapBuilderTerminate(&mb);
}

Expand Down Expand Up @@ -782,6 +778,7 @@ static const char *MapGetWallPic(const MapBuilder *m, const struct vec2i pos);
// Set tile properties for a map tile
static void MapSetupTile(MapBuilder *mb, const struct vec2i pos)
{
if (!MapIsTileIn(mb->Map, pos)) return;
const Tile *tAbove = MapGetTile(mb->Map, svec2i(pos.x, pos.y - 1));
const bool canSeeTileAbove = !(tAbove != NULL && TileIsOpaque(tAbove));
Tile *t = MapGetTile(mb->Map, pos);
Expand Down Expand Up @@ -889,7 +886,8 @@ static bool W(const MapBuilder *mb, const int x, const int y)
{
const struct vec2i v = svec2i(x, y);
if (!MapIsTileIn(mb->Map, v)) return false;
return MapBuilderGetTile(mb, v)->IsWall;
const TileClass *tc = MapBuilderGetTile(mb, v);
return tc->IsWall;
}

static bool MapIsValidStartForWall(
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/map_build.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ uint16_t MapGetTileType(const Map *map, const struct vec2i pos);
const TileClass *MapBuildGetTileFromType(const uint16_t tile, bool *isRoom);
void MapBuildTile(
Map *m, const Mission *mission, const struct vec2i pos,
const TileClass *tile);
const TileClass *tile, const bool isRoom);

uint16_t GenerateAccessMask(int *accessLevel);
void MapGenerateRandomExitArea(Map *map);
32 changes: 19 additions & 13 deletions src/cdogs/map_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,7 @@ void MapStaticLoad(MapBuilder *mb)
{
for (v.x = 0; v.x < mb->Map->Size.x; v.x++)
{
int idx = v.y * mb->Map->Size.x + v.x;
uint16_t tile =
*(uint16_t *)CArrayGet(&mb->mission->u.Static.Tiles, idx);
uint16_t tileAccess = tile & MAP_ACCESSBITS;
tile &= MAP_MASKACCESS;
if (!AreKeysAllowed(gCampaign.Entry.Mode))
{
tileAccess = 0;
}
bool isRoom = false;
const TileClass *t = MapBuildGetTileFromType(tile, &isRoom);
MapBuilderSetTile(mb, v, t, isRoom);
MapBuildSetAccess(mb, v, tileAccess);
MapStaticLoadTile(mb, v);
}
}

Expand All @@ -71,6 +59,24 @@ void MapStaticLoad(MapBuilder *mb)
}
}

void MapStaticLoadTile(MapBuilder *mb, const struct vec2i v)
{
if (!MapIsTileIn(mb->Map, v)) return;
const int idx = v.y * mb->Map->Size.x + v.x;
uint16_t tile =
*(uint16_t *)CArrayGet(&mb->mission->u.Static.Tiles, idx);
uint16_t tileAccess = tile & MAP_ACCESSBITS;
tile &= MAP_MASKACCESS;
if (!AreKeysAllowed(gCampaign.Entry.Mode))
{
tileAccess = 0;
}
bool isRoom = false;
const TileClass *t = MapBuildGetTileFromType(tile, &isRoom);
MapBuilderSetTile(mb, v, t, isRoom);
MapBuildSetAccess(mb, v, tileAccess);
}

static void AddCharacters(const CArray *characters);
static void AddObjectives(MapBuilder *mb, const CArray *objectives);
static void AddKeys(MapBuilder *mb, const CArray *keys);
Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/map_static.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2014, 2018 Cong Xu
Copyright (c) 2014, 2018-2019 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -30,4 +30,5 @@
#include "map_build.h"

void MapStaticLoad(MapBuilder *mb);
void MapStaticLoadTile(MapBuilder *mb, const struct vec2i v);
void MapStaticLoadDynamic(MapBuilder *mb);
2 changes: 1 addition & 1 deletion src/cdogsed/editor_brush.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static void SetTile(Mission *m, struct vec2i pos, const uint16_t tile)
{
bool isRoom = false;
const TileClass *t = MapBuildGetTileFromType(tile, &isRoom);
MapBuildTile(&gMap, m, pos, t);
MapBuildTile(&gMap, m, pos, t, isRoom);
}
}

Expand Down

0 comments on commit 0b36c70

Please sign in to comment.