Skip to content

Commit

Permalink
Add styles to tileclass (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Jan 22, 2019
1 parent a9567a3 commit b566dc0
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 83 deletions.
69 changes: 38 additions & 31 deletions src/cdogs/door.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@
#include "net_util.h"


static void DoorGetClassName(
char *buf, const char *style, const char *key, const bool isHorizontal);
static int GetDoorCountInGroup(
const MapBuilder *mb, const struct vec2i v, const bool isHorizontal);
static TWatch *CreateCloseDoorWatch(
MapBuilder *mb, const struct vec2i v,
const bool isHorizontal, const int doorGroupCount,
const TileClass *classAlt);
const char *classAltName);
static Trigger *CreateOpenDoorTrigger(
MapBuilder *mb, const struct vec2i v,
const bool isHorizontal, const int doorGroupCount, const int keyFlags);
Expand All @@ -84,8 +86,10 @@ void MapAddDoorGroup(MapBuilder *mb, const struct vec2i v, const int keyFlags)
case FLAGS_KEYCARD_YELLOW: doorKey = "yellow"; break;
default: doorKey = "normal"; break;
}
const TileClass *doorClass = DoorGetClass(
mb->mission->DoorStyle, doorKey, isHorizontal);
char doorClassName[CDOGS_FILENAME_MAX];
DoorGetClassName(
doorClassName, mb->mission->DoorStyle, doorKey, isHorizontal);
const TileClass *doorClass = StrTileClass(doorClassName);
const TileClass *doorClassOpen = DoorGetClass(
mb->mission->DoorStyle, "open", isHorizontal);

Expand Down Expand Up @@ -120,7 +124,7 @@ void MapAddDoorGroup(MapBuilder *mb, const struct vec2i v, const int keyFlags)
}

TWatch *w = CreateCloseDoorWatch(
mb, v, isHorizontal, doorGroupCount, doorClass);
mb, v, isHorizontal, doorGroupCount, doorClassName);
Trigger *t = CreateOpenDoorTrigger(
mb, v, isHorizontal, doorGroupCount, keyFlags);
// Connect trigger and watch up
Expand All @@ -143,8 +147,6 @@ void MapAddDoorGroup(MapBuilder *mb, const struct vec2i v, const int keyFlags)
}
}

static void DoorGetClassName(
char *buf, const char *style, const char *key, const bool isHorizontal);
// Count the number of doors that are in the same group as this door
// Only check to the right/below
static int GetDoorCountInGroup(
Expand All @@ -166,7 +168,7 @@ static int GetDoorCountInGroup(
static TWatch *CreateCloseDoorWatch(
MapBuilder *mb, const struct vec2i v,
const bool isHorizontal, const int doorGroupCount,
const TileClass *classAlt)
const char *classAltName)
{
TWatch *w = WatchNew();
const struct vec2i dv = svec2i(isHorizontal ? 1 : 0, isHorizontal ? 0 : 1);
Expand Down Expand Up @@ -211,7 +213,7 @@ static TWatch *CreateCloseDoorWatch(
DoorGetClassName(
a->a.Event.u.TileSet.ClassName, mb->mission->DoorStyle, "open",
isHorizontal);
strcpy(a->a.Event.u.TileSet.ClassAltName, classAlt->Name);
strcpy(a->a.Event.u.TileSet.ClassAltName, classAltName);
}

// Add shadows below doors
Expand All @@ -227,15 +229,13 @@ static TWatch *CreateCloseDoorWatch(
const struct vec2i vI2 = svec2i(vI.x + dAside.x, vI.y + dAside.y);
a->a.Event.u.TileSet.Pos = Vec2i2Net(vI2);
const bool isFloor = !MapBuilderGetIsRoom(mb, vI2);
strcpy(
TileClassGetName(
a->a.Event.u.TileSet.ClassName,
TileClassesGetMaskedTile(
&gTileFloor,
isFloor ? mb->mission->FloorStyle : mb->mission->RoomStyle,
"shadow",
isFloor ? mb->mission->FloorMask : mb->mission->RoomMask,
mb->mission->AltMask
)->Name
"tile",
isFloor ? mb->mission->FloorStyle : mb->mission->RoomStyle,
"shadow",
isFloor ? mb->mission->FloorMask : mb->mission->RoomMask,
mb->mission->AltMask
);
}
}
Expand Down Expand Up @@ -292,15 +292,13 @@ static Trigger *CreateOpenDoorTrigger(
a->a.Event = GameEventNew(GAME_EVENT_TILE_SET);
const bool isFloor = !MapBuilderGetIsRoom(mb, vIAside);
a->a.Event.u.TileSet.Pos = Vec2i2Net(vIAside);
strcpy(
TileClassGetName(
a->a.Event.u.TileSet.ClassName,
TileClassesGetMaskedTile(
&gTileFloor,
isFloor? mb->mission->FloorStyle : mb->mission->RoomStyle,
"normal",
isFloor ? mb->mission->FloorMask : mb->mission->RoomMask,
mb->mission->AltMask
)->Name
"tile",
isFloor ? mb->mission->FloorStyle : mb->mission->RoomStyle,
"normal",
isFloor ? mb->mission->FloorMask : mb->mission->RoomMask,
mb->mission->AltMask
);
}
}
Expand Down Expand Up @@ -358,23 +356,32 @@ const TileClass *DoorGetClass(
DoorGetClassName(buf, style, key, isHorizontal);
return StrTileClass(buf);
}
static void DoorGetClassName(
char *buf, const char *style, const char *key, const bool isHorizontal)
static void DoorGetTypeName(char *buf, const char *key, const bool isHorizontal)
{
// If the key is "wall", it doesn't include orientation
sprintf(
buf, "door/%s/%s%s", style, key,
buf, "%s%s", key,
strcmp(key, "wall") == 0 ? "" : (isHorizontal ? "_h" : "_v"));
}
static void DoorGetClassName(
char *buf, const char *style, const char *key, const bool isHorizontal)
{
char type[256];
DoorGetTypeName(type, key, isHorizontal);
// If the key is "wall", it doesn't include orientation
TileClassGetName(buf, "door", style, type, colorWhite, colorWhite);
}
void DoorAddClass(
TileClasses *c, const PicManager *pm,
TileClasses *c, PicManager *pm,
const char *style, const char *key, const bool isHorizontal)
{
char buf[CDOGS_FILENAME_MAX];
DoorGetClassName(buf, style, key, isHorizontal);
TileClass *t = TileClassAdd(c->customClasses, pm, NULL, buf);
DoorGetTypeName(buf, key, isHorizontal);
PicManagerGenerateMaskedStylePic(
pm, "door", style, buf, colorWhite, colorWhite);
TileClass *t = TileClassesAdd(
c, pm, &gTileDoor, style, buf, colorWhite, colorWhite);
CASSERT(t != NULL, "cannot add door class");
t->Type = TILE_CLASS_DOOR;
const bool isOpenOrWallCavity =
strcmp(key, "open") == 0 || strcmp(key, "wall") == 0;
t->isOpaque = !isOpenOrWallCavity;
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/door.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void MapAddDoorGroup(MapBuilder *mb, const struct vec2i v, const int keyFlags);
const TileClass *DoorGetClass(
const char *style, const char *key, const bool isHorizontal);
void DoorAddClass(
TileClasses *c, const PicManager *pm,
TileClasses *c, PicManager *pm,
const char *style, const char *key, const bool isHorizontal);

// Legacy door style int to str
Expand Down
10 changes: 7 additions & 3 deletions src/cdogs/map_build.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static void MapBuilderSetupTileClasses(const MapBuilder *mb)
// Floor/square
for (int i = 0; i < FLOOR_TYPES; i++)
{
TileClassesAddMaskedTile(
TileClassesAdd(
&gTileClasses, &gPicManager,
&gTileFloor,
mb->mission->FloorStyle, IntTileType(i),
Expand All @@ -167,7 +167,7 @@ static void MapBuilderSetupTileClasses(const MapBuilder *mb)
// Room
for (int i = 0; i < ROOMFLOOR_TYPES; i++)
{
TileClassesAddMaskedTile(
TileClassesAdd(
&gTileClasses, &gPicManager, &gTileFloor, mb->mission->RoomStyle,
IntTileType(i),
mb->mission->RoomMask, mb->mission->AltMask);
Expand All @@ -191,7 +191,7 @@ static void MapBuilderSetupTileClasses(const MapBuilder *mb)
// Wall
for (int i = 0; i < WALL_TYPE_COUNT; i++)
{
TileClassesAddMaskedTile(
TileClassesAdd(
&gTileClasses, &gPicManager, &gTileWall,
mb->mission->WallStyle, IntWallType(i),
mb->mission->WallMask, mb->mission->AltMask);
Expand Down Expand Up @@ -1037,6 +1037,7 @@ static void MapGrowWall(
int level = v.y - (pos.y - 2);
for (v.x = pos.x - 1 - level; v.x <= pos.x + 1 + level; v.x++)
{
if (!MapIsTileIn(mb->Map, v)) continue;
if (MapBuilderGetTile(mb, v)->Type != TILE_CLASS_FLOOR ||
!MapBuilderGetIsRoom(mb, v) != isRoom)
{
Expand All @@ -1058,6 +1059,7 @@ static void MapGrowWall(
int level = v.x - (pos.x + 2);
for (v.y = pos.y - 1 - level; v.y <= pos.y + 1 + level; v.y++)
{
if (!MapIsTileIn(mb->Map, v)) continue;
if (MapBuilderGetTile(mb, v)->Type != TILE_CLASS_FLOOR ||
!MapBuilderGetIsRoom(mb, v) != isRoom)
{
Expand All @@ -1077,6 +1079,7 @@ static void MapGrowWall(
int level = v.y - (pos.y + 2);
for (v.x = pos.x - 1 - level; v.x <= pos.x + 1 + level; v.x++)
{
if (!MapIsTileIn(mb->Map, v)) continue;
if (MapBuilderGetTile(mb, v)->Type != TILE_CLASS_FLOOR ||
!MapBuilderGetIsRoom(mb, v) != isRoom)
{
Expand All @@ -1102,6 +1105,7 @@ static void MapGrowWall(
int level = v.x - (pos.x - 2);
for (v.y = pos.y - 1 - level; v.y <= pos.y + 1 + level; v.y++)
{
if (!MapIsTileIn(mb->Map, v)) continue;
if (MapBuilderGetTile(mb, v)->Type != TILE_CLASS_FLOOR ||
!MapBuilderGetIsRoom(mb, v) != isRoom)
{
Expand Down
81 changes: 40 additions & 41 deletions src/cdogs/tile_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,29 @@
#include "tile_class.h"

#include "log.h"
#include "sys_config.h"


TileClasses gTileClasses;
TileClass gTileFloor = {
"tile", NULL, true, false, false, TILE_CLASS_FLOOR,
"tile", NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
true, false, false, TILE_CLASS_FLOOR,
};
TileClass gTileWall = {
"wall", NULL, false, true, true, TILE_CLASS_WALL,
"wall", NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
false, true, true, TILE_CLASS_WALL,
};
TileClass gTileNothing = {
NULL, NULL, false, false, false, TILE_CLASS_NOTHING,
NULL, NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
false, false, false, TILE_CLASS_NOTHING,
};
TileClass gTileExit = {
"tile", NULL, true, false, false, TILE_CLASS_FLOOR,
"exits", NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
true, false, false, TILE_CLASS_FLOOR,
};
TileClass gTileDoor = {
"door", NULL, false, true, true, TILE_CLASS_DOOR,
"door", NULL, NULL, NULL, { 255, 255, 255, 255 }, { 255, 255, 255, 255 },
false, true, true, TILE_CLASS_DOOR,
};

void TileClassesInit(TileClasses *c)
Expand All @@ -67,6 +73,8 @@ static void TileClassDestroy(any_t data)
{
TileClass *c = data;
CFREE(c->Name);
CFREE(c->Style);
CFREE(c->StyleType);
CFREE(c);
}

Expand All @@ -92,27 +100,38 @@ const TileClass *StrTileClass(const char *name)
return &gTileNothing;
}

static void GetMaskedName(
char *buf, const char *name, const char *style, const char *type,
const color_t mask, const color_t maskAlt);
const TileClass *TileClassesGetMaskedTile(
const TileClass *baseClass, const char *style, const char *type,
const color_t mask, const color_t maskAlt)
{
char buf[256];
GetMaskedName(buf, baseClass->Name, style, type, mask, maskAlt);
TileClassGetName(buf, baseClass->Name, style, type, mask, maskAlt);
return StrTileClass(buf);
}
void TileClassesAddMaskedTile(
TileClass *TileClassesAdd(
TileClasses *c, const PicManager *pm, const TileClass *baseClass,
const char *style, const char *type,
const color_t mask, const color_t maskAlt)
{
char buf[256];
GetMaskedName(buf, baseClass->Name, style, type, mask, maskAlt);
TileClassAdd(c->customClasses, pm, baseClass, buf);
TileClass *t;
CCALLOC(t, sizeof *t);
memcpy(t, baseClass, sizeof *t);
CSTRDUP(t->Name, baseClass->Name);
CSTRDUP(t->Style, style);
CSTRDUP(t->StyleType, type);
char buf[CDOGS_PATH_MAX];
TileClassGetName(buf, baseClass->Name, style, type, mask, maskAlt);
t->Pic = PicManagerGetPic(pm, buf);

const int error = hashmap_put(c->customClasses, buf, t);
if (error != MAP_OK)
{
LOG(LM_MAIN, LL_ERROR, "failed to add tile class %s: %d", buf, error);
return NULL;
}
return t;
}
static void GetMaskedName(
void TileClassGetName(
char *buf, const char *name, const char *style, const char *type,
const color_t mask, const color_t maskAlt)
{
Expand All @@ -124,40 +143,20 @@ static void GetMaskedName(
}

const TileClass *TileClassesGetExit(
TileClasses *c, const PicManager *pm,
const char *style, const bool isShadow)
TileClasses *c, PicManager *pm, const char *style, const bool isShadow)
{
char buf[256];
sprintf(buf, "exits/%s/%s", style, isShadow ? "shadow" : "normal");
const char *type = isShadow ? "shadow" : "normal";
TileClassGetName(buf, "exits", style, type, colorWhite, colorWhite);
const TileClass *t = StrTileClass(buf);
if (t != &gTileNothing)
{
return t;
}

// tile class not found; create it
return TileClassAdd(c->customClasses, pm, &gTileExit, buf);
}

TileClass *TileClassAdd(
map_t classes, const PicManager *pm, const TileClass *base,
const char *name)
{
TileClass *t;
CCALLOC(t, sizeof *t);
if (base != NULL)
{
memcpy(t, base, sizeof *t);
}
CSTRDUP(t->Name, name);
t->Pic = PicManagerGetPic(pm, name);

const int error = hashmap_put(classes, name, t);
if (error != MAP_OK)
{
LOG(LM_MAIN, LL_ERROR, "failed to add tile class %s: %d",
name, error);
return NULL;
}
return t;
PicManagerGenerateMaskedStylePic(
pm, "exits", style, type, colorWhite, colorWhite);
return TileClassesAdd(
c, pm, &gTileExit, style, type, colorWhite, colorWhite);
}
16 changes: 9 additions & 7 deletions src/cdogs/tile_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ typedef struct
{
char *Name;
const Pic *Pic;
char *Style;
char *StyleType;
color_t Mask;
color_t MaskAlt;
bool canWalk; // can walk on tile
bool isOpaque; // cannot see through
bool shootable; // blocks bullets
Expand Down Expand Up @@ -83,14 +87,12 @@ const TileClass *StrTileClass(const char *name);
const TileClass *TileClassesGetMaskedTile(
const TileClass *baseClass, const char *style, const char *type,
const color_t mask, const color_t maskAlt);
void TileClassesAddMaskedTile(
TileClass *TileClassesAdd(
TileClasses *c, const PicManager *pm, const TileClass *baseClass,
const char *style, const char *type,
const color_t mask, const color_t maskAlt);
void TileClassGetName(
char *buf, const char *name, const char *style, const char *type,
const color_t mask, const color_t maskAlt);
const TileClass *TileClassesGetExit(
TileClasses *c, const PicManager *pm,
const char *style, const bool isShadow);

TileClass *TileClassAdd(
map_t classes, const PicManager *pm, const TileClass *base,
const char *name);
TileClasses *c, PicManager *pm, const char *style, const bool isShadow);

0 comments on commit b566dc0

Please sign in to comment.