Skip to content

Commit

Permalink
Add TileClass (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Jun 25, 2018
1 parent 5721eb8 commit ec323f1
Show file tree
Hide file tree
Showing 34 changed files with 504 additions and 312 deletions.
2 changes: 2 additions & 0 deletions src/cdogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ int main(int argc, char *argv[])
EventInit(&gEventHandlers, NULL, NULL, true);
NetServerInit(&gNetServer);
PicManagerInit(&gPicManager);
TileClassesInit(&gTileClasses);
GraphicsInit(&gGraphicsDevice, &gConfig);
GraphicsInitialize(&gGraphicsDevice);
if (!gGraphicsDevice.IsInitialized)
Expand Down Expand Up @@ -292,6 +293,7 @@ int main(int argc, char *argv[])
CollisionSystemTerminate(&gCollisionSystem);

CharSpriteClassesTerminate(&gCharSpriteClasses);
TileClassesTerminate(gTileClasses);
PicManagerTerminate(&gPicManager);
FontTerminate(&gFont);
AutosaveSave(&gAutosave, GetConfigFilePath(AUTOSAVE_FILE));
Expand Down
2 changes: 2 additions & 0 deletions src/cdogs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ set(CDOGS_SOURCES
texture.c
thing.c
tile.c
tile_class.c
triggers.c
utils.c
vector.c
Expand Down Expand Up @@ -202,6 +203,7 @@ set(CDOGS_HEADERS
texture.h
thing.h
tile.h
tile_class.h
triggers.h
utils.h
vector.h
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/ai_coop.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ static void FindObjectivesSortedByDistance(
// Check if the pickup is actually accessible
// This is because random spawning may cause some pickups to be spawned
// in inaccessible areas
if (MapGetTile(&gMap, Vec2ToTile(co.Pos))->flags & MAPTILE_NO_WALK)
if (!TileCanWalk(MapGetTile(&gMap, Vec2ToTile(co.Pos))))
{
continue;
}
Expand Down
7 changes: 3 additions & 4 deletions src/cdogs/ai_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,11 @@ static bool IsTileWalkableOrOpenable(Map *map, struct vec2i pos)
{
return false;
}
const int tileFlags = tile->flags;
if (!(tileFlags & MAPTILE_NO_WALK))
if (TileCanWalk(tile))
{
return true;
}
if (tileFlags & MAPTILE_OFFSET_PIC)
if (tile->Class->IsDoor)
{
// A door; check if we can open it
int keycard = MapGetDoorKeycardFlag(map, pos);
Expand Down Expand Up @@ -353,7 +352,7 @@ bool AIHasClearShot(const struct vec2 from, const struct vec2 to)
}
static bool IsPosNoSee(void *data, struct vec2i pos)
{
return MapGetTile(data, Vec2iToTile(pos))->flags & MAPTILE_NO_SEE;
return TileIsOpaque(MapGetTile(data, Vec2iToTile(pos)));
}

TObject *AIGetObjectRunningInto(TActor *a, int cmd)
Expand Down
8 changes: 4 additions & 4 deletions src/cdogs/automap.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static void DrawMap(
for (x = 0; x < gMap.Size.x; x++)
{
Tile *tile = MapGetTile(map, svec2i(x, y));
if (!(tile->flags & MAPTILE_IS_NOTHING) &&
if (tile->Class->Pic != NULL &&
(tile->isVisited || (flags & AUTOMAP_FLAGS_SHOWALL)))
{
int j;
Expand All @@ -225,15 +225,15 @@ static void DrawMap(
mapPos.x + x*scale + j,
mapPos.y + y*scale + i);
color_t color = colorRoom;
if (tile->flags & MAPTILE_IS_WALL)
if (tile->Class->IsWall)
{
color = colorWall;
}
else if (tile->flags & MAPTILE_NO_WALK)
else if (tile->Class->IsDoor)
{
color = DoorColor(x, y);
}
else if (tile->flags & MAPTILE_IS_NORMAL_FLOOR)
else if (tile->Class->IsFloor)
{
color = colorFloor;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/bullet_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ static HitType GetHitType(
static bool CheckWall(const struct vec2i tilePos)
{
const Tile *t = MapGetTile(&gMap, tilePos);
return t == NULL || t->flags & MAPTILE_NO_SHOOT;
return t == NULL || TileIsShootable(t);
}
static bool HitWallFunc(
const struct vec2i tilePos, void *data,
Expand Down
9 changes: 2 additions & 7 deletions src/cdogs/collision/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,10 @@ void CollisionSystemReset(CollisionSystem *cs);
void CollisionSystemTerminate(CollisionSystem *cs);

#define HitWall(x, y)\
(MapGetTile(\
(!TileCanWalk(MapGetTile(\
&gMap,\
svec2i((int)(x)/TILE_WIDTH, (int)(y)/TILE_HEIGHT)\
)->flags & MAPTILE_NO_WALK)
#define ShootWall(x, y) \
(MapGetTile(\
&gMap,\
svec2i((int)(x)/TILE_WIDTH, (int)(y)/TILE_HEIGHT))\
->flags & MAPTILE_NO_SHOOT)
)))

// Which "team" the actor's on, for collision
// Actors on the same team don't have to collide
Expand Down
135 changes: 87 additions & 48 deletions src/cdogs/door.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2015, Cong Xu
Copyright (c) 2013-2015, 2018 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand All @@ -49,21 +49,16 @@
#include "door.h"

#include "gamedata.h"
#include "log.h"
#include "net_util.h"


#define DOOR_TILE_FLAGS \
(MAPTILE_NO_SEE | MAPTILE_NO_WALK | MAPTILE_NO_SHOOT | MAPTILE_OFFSET_PIC)


static int GetDoorCountInGroup(
const Map *map, const struct vec2i v, const bool isHorizontal);
static NamedPic *GetDoorBasePic(
const PicManager *pm, const char *style, const bool isHorizontal);
static TWatch *CreateCloseDoorWatch(
Map *map, const Mission *m, const struct vec2i v,
const bool isHorizontal, const int doorGroupCount,
const char *picAltName);
const TileClass *classAlt);
static Trigger *CreateOpenDoorTrigger(
Map *map, const Mission *m, const struct vec2i v,
const bool isHorizontal, const int doorGroupCount, const int keyFlags);
Expand Down Expand Up @@ -91,39 +86,45 @@ void MapAddDoorGroup(
case FLAGS_KEYCARD_YELLOW: doorKey = "yellow"; break;
default: doorKey = "normal"; break;
}
NamedPic *doorPic =
GetDoorPic(&gPicManager, m->DoorStyle, doorKey, isHorizontal);
const TileClass *doorClass = DoorGetClass(
gTileClasses, &gPicManager, m->DoorStyle, doorKey, isHorizontal);
const TileClass *doorClassOpen = DoorGetClass(
gTileClasses, &gPicManager, m->DoorStyle, "open", isHorizontal);

// set up the door pics
for (int i = 0; i < doorGroupCount; i++)
{
const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
Tile *tile = MapGetTile(map, vI);
tile->picAlt = doorPic;
tile->pic = GetDoorBasePic(&gPicManager, m->DoorStyle, isHorizontal);
tile->flags = DOOR_TILE_FLAGS;
tile->ClassAlt = doorClass;
tile->Class = doorClassOpen;
if (isHorizontal)
{
const struct vec2i vB = svec2i_add(vI, dAside);
Tile *tileB = MapGetTile(map, vB);
CASSERT(TileCanWalk(MapGetTile(
map, svec2i(vI.x - dAside.x, vI.y - dAside.y))),
CASSERT(
TileCanWalk(MapGetTile(
map, svec2i(vI.x - dAside.x, vI.y - dAside.y)
)),
"map gen error: entrance should be clear");
CASSERT(TileCanWalk(tileB),
"map gen error: entrance should be clear");
// Change the tile below to shadow, cast by this door
const bool isFloor = IMapGet(map, vB) == MAP_FLOOR;
tileB->pic = PicManagerGetMaskedStylePic(
tileB->Class = TileClassesGetMaskedTile(
gTileClasses,
&gPicManager,
"tile",
&gTileFloor,
isFloor ? m->FloorStyle : m->RoomStyle,
"shadow",
isFloor ? m->FloorMask : m->RoomMask, m->AltMask);
isFloor ? m->FloorMask : m->RoomMask,
m->AltMask
);
}
}

TWatch *w = CreateCloseDoorWatch(
map, m, v, isHorizontal, doorGroupCount, doorPic->name);
map, m, v, isHorizontal, doorGroupCount, doorClass);
Trigger *t = CreateOpenDoorTrigger(
map, m, v, isHorizontal, doorGroupCount, keyFlags);
// Connect trigger and watch up
Expand Down Expand Up @@ -167,7 +168,7 @@ static int GetDoorCountInGroup(
static TWatch *CreateCloseDoorWatch(
Map *map, const Mission *m, const struct vec2i v,
const bool isHorizontal, const int doorGroupCount,
const char *picAltName)
const TileClass *classAlt)
{
TWatch *w = WatchNew();
const struct vec2i dv = svec2i(isHorizontal ? 1 : 0, isHorizontal ? 0 : 1);
Expand Down Expand Up @@ -209,11 +210,13 @@ static TWatch *CreateCloseDoorWatch(
a->Type = ACTION_EVENT;
a->a.Event = GameEventNew(GAME_EVENT_TILE_SET);
a->a.Event.u.TileSet.Pos = Vec2i2Net(vI);
a->a.Event.u.TileSet.Flags = DOOR_TILE_FLAGS;
strcpy(
a->a.Event.u.TileSet.PicName,
GetDoorBasePic(&gPicManager, m->DoorStyle, isHorizontal)->name);
strcpy(a->a.Event.u.TileSet.PicAltName, picAltName);
a->a.Event.u.TileSet.ClassName,
DoorGetClass(
gTileClasses, &gPicManager, m->DoorStyle, "open", isHorizontal
)->Name
);
strcpy(a->a.Event.u.TileSet.ClassAltName, classAlt->Name);
}

// Add shadows below doors
Expand All @@ -230,14 +233,17 @@ static TWatch *CreateCloseDoorWatch(
a->a.Event.u.TileSet.Pos = Vec2i2Net(vI2);
const bool isFloor = IMapGet(map, vI2) == MAP_FLOOR;
strcpy(
a->a.Event.u.TileSet.PicName,
PicManagerGetMaskedStylePic(
a->a.Event.u.TileSet.ClassName,
TileClassesGetMaskedTile(
gTileClasses,
&gPicManager,
"tile",
&gTileFloor,
isFloor ? m->FloorStyle : m->RoomStyle,
"shadow",
isFloor ? m->FloorMask : m->RoomMask,
m->AltMask)->name);
m->AltMask
)->Name
);
}
}

Expand Down Expand Up @@ -268,17 +274,21 @@ static Trigger *CreateOpenDoorTrigger(
a->Type = ACTION_EVENT;
a->a.Event = GameEventNew(GAME_EVENT_TILE_SET);
a->a.Event.u.TileSet.Pos = Vec2i2Net(vI);
a->a.Event.u.TileSet.Flags =
(isHorizontal || i > 0) ? 0 : MAPTILE_OFFSET_PIC;
strcpy(
a->a.Event.u.TileSet.PicName,
GetDoorBasePic(&gPicManager, m->DoorStyle, isHorizontal)->name);
a->a.Event.u.TileSet.ClassName,
DoorGetClass(
gTileClasses, &gPicManager, m->DoorStyle, "open", isHorizontal
)->Name
);
if (!isHorizontal && i == 0)
{
// special door cavity picture
strcpy(
a->a.Event.u.TileSet.PicAltName,
GetDoorPic(&gPicManager, m->DoorStyle, "wall", false)->name);
a->a.Event.u.TileSet.ClassAltName,
DoorGetClass(
gTileClasses, &gPicManager, m->DoorStyle, "wall", false
)->Name
);
}
}

Expand All @@ -296,14 +306,17 @@ static Trigger *CreateOpenDoorTrigger(
const bool isFloor = IMapGet(map, vIAside) == MAP_FLOOR;
a->a.Event.u.TileSet.Pos = Vec2i2Net(vIAside);
strcpy(
a->a.Event.u.TileSet.PicName,
PicManagerGetMaskedStylePic(
a->a.Event.u.TileSet.ClassName,
TileClassesGetMaskedTile(
gTileClasses,
&gPicManager,
"tile",
&gTileFloor,
isFloor? m->FloorStyle : m->RoomStyle,
"normal",
isFloor ? m->FloorMask : m->RoomMask,
m->AltMask)->name);
m->AltMask
)->Name
);
}
}

Expand Down Expand Up @@ -331,12 +344,6 @@ static void TileAddTrigger(Tile *t, Trigger *tr)
CArrayPushBack(&t->triggers, &tr);
}

static NamedPic *GetDoorBasePic(
const PicManager *pm, const char *style, const bool isHorizontal)
{
return GetDoorPic(pm, style, "open", isHorizontal);
}


// Old door pics definitions
typedef struct
Expand All @@ -356,17 +363,49 @@ typedef struct
int Wall;
} DoorPics;

NamedPic *GetDoorPic(
const PicManager *pm, const char *style, const char *key,
static void GetClassName(
char *buf, const char *style, const char *key, const bool isHorizontal);
// Get the tile class of a door; if it doesn't exist create it
// style: office/dungeon/blast/alien, or custom
// key: normal/yellow/green/blue/red/wall/open
const TileClass *DoorGetClass(
map_t classes, const PicManager *pm,
const char *style, const char *key,
const bool isHorizontal)
{
char buf[CDOGS_FILENAME_MAX];
// Construct filename
GetClassName(buf, style, key, isHorizontal);
TileClass *c;
if (hashmap_get(classes, buf, (any_t *)&c) == MAP_OK)
{
return c;
}

// tile class not found; create it
CCALLOC(c, sizeof *c);
CSTRDUP(c->Name, buf);
c->Pic = PicManagerGetPic(pm, buf);
c->IsDoor = true;
c->isOpaque = strcmp(key, "open") != 0;
c->canWalk = strcmp(key, "open") == 0;
c->shootable = strcmp(key, "open") != 0;

const int error = hashmap_put(classes, buf, c);
if (error != MAP_OK)
{
LOG(LM_MAIN, LL_ERROR, "failed to add door class %s: %d",
buf, error);
return NULL;
}
return c;
}
static void GetClassName(
char *buf, const char *style, const char *key, const bool isHorizontal)
{
// If the key is "wall", it doesn't include orientation
sprintf(
buf, "door/%s/%s%s", style, key,
strcmp(key, "wall") == 0 ? "" : (isHorizontal ? "_h" : "_v"));
return PicManagerGetNamedPic(pm, buf);
}

#define DOORSTYLE_COUNT 4
Expand Down
10 changes: 4 additions & 6 deletions src/cdogs/door.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2016, Cong Xu
Copyright (c) 2013-2016, 2018 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -54,11 +54,9 @@
void MapAddDoorGroup(
Map *map, const Mission *m, const struct vec2i v, const int keyFlags);

// Get the picture of a door
// style: office/dungeon/blast/alien, or custom
// key: normal/yellow/green/blue/red/wall/open
NamedPic *GetDoorPic(
const PicManager *pm, const char *style, const char *key,
const TileClass *DoorGetClass(
map_t classes, const PicManager *pm,
const char *style, const char *key,
const bool isHorizontal);

// Legacy door style int to str
Expand Down
Loading

0 comments on commit ec323f1

Please sign in to comment.