Skip to content

Commit

Permalink
Refactor map build, extract leave free #475
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Nov 23, 2018
1 parent 613fafb commit 2a7dfe5
Show file tree
Hide file tree
Showing 27 changed files with 795 additions and 743 deletions.
1 change: 1 addition & 0 deletions src/cdogs/actors.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "ai_context.h"
#include "animation.h"
#include "emitter.h"
#include "game_mode.h"
#include "grafx.h"
#include "mathc/mathc.h"
#include "player.h"
Expand Down
8 changes: 6 additions & 2 deletions src/cdogs/c_array.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2013-2017 Cong Xu
Copyright (c) 2013-2018 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -89,7 +89,7 @@ void CArrayInsert(CArray *a, const size_t idx, const void *elem)
CArrayGet(a, idx),
a->elemSize * (a->size - 1 - idx));
}
memcpy(CArrayGet(a, idx), elem, a->elemSize);
CArraySet(a, idx, elem);
}
void CArrayDelete(CArray *a, const size_t idx)
{
Expand Down Expand Up @@ -124,6 +124,10 @@ void *CArrayGet(const CArray *a, const size_t idx)
CASSERT(idx < a->size, "array index out of bounds");
return &((char *)a->data)[idx * a->elemSize];
}
void CArraySet(CArray *a, const size_t idx, const void *elem)
{
memcpy(CArrayGet(a, idx), elem, a->elemSize);
}

void CArrayClear(CArray *a)
{
Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/c_array.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2013-2017 Cong Xu
Copyright (c) 2013-2018 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -45,6 +45,7 @@ void CArrayInsert(CArray *a, const size_t index, const void *elem);
void CArrayDelete(CArray *a, const size_t index);
void CArrayResize(CArray *a, const size_t size, const void *value);
void *CArrayGet(const CArray *a, const size_t index); // gets address
void CArraySet(CArray *a, const size_t idx, const void *elem);
void CArrayClear(CArray *a);
void CArrayRemoveIf(CArray *a, bool(*removeIf)(const void *));
void CArrayFill(CArray *a, const void *elem);
Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/collision/collision.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, 2017 Cong Xu
Copyright (c) 2013-2015, 2017-2018 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -50,6 +50,7 @@

#include "actors.h"
#include "algorithms.h"
#include "campaigns.h"
#include "config.h"
#include "minkowski_hex.h"
#include "objs.h"
Expand Down
1 change: 1 addition & 0 deletions src/cdogs/damage.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "damage.h"

#include "actors.h"
#include "campaigns.h"
#include "game_events.h"


Expand Down
39 changes: 20 additions & 19 deletions src/cdogs/door.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,17 @@ static TWatch *CreateCloseDoorWatch(
static Trigger *CreateOpenDoorTrigger(
Map *map, const Mission *m, const struct vec2i v,
const bool isHorizontal, const int doorGroupCount, const int keyFlags);
void MapAddDoorGroup(
Map *map, const Mission *m, const struct vec2i v, const int keyFlags)
void MapAddDoorGroup(MapBuilder *mb, const struct vec2i v, const int keyFlags)
{
const unsigned short tileLeftType =
IMapGet(map, svec2i(v.x - 1, v.y)) & MAP_MASKACCESS;
IMapGet(mb->Map, svec2i(v.x - 1, v.y)) & MAP_MASKACCESS;
const unsigned short tileRightType =
IMapGet(map, svec2i(v.x + 1, v.y)) & MAP_MASKACCESS;
IMapGet(mb->Map, svec2i(v.x + 1, v.y)) & MAP_MASKACCESS;
const bool isHorizontal =
tileLeftType == MAP_WALL || tileRightType == MAP_WALL ||
tileLeftType == MAP_DOOR || tileRightType == MAP_DOOR ||
tileLeftType == MAP_NOTHING || tileRightType == MAP_NOTHING;
const int doorGroupCount = GetDoorCountInGroup(map, v, isHorizontal);
const int doorGroupCount = GetDoorCountInGroup(mb->Map, v, isHorizontal);
const struct vec2i dv = svec2i(isHorizontal ? 1 : 0, isHorizontal ? 0 : 1);
const struct vec2i dAside = svec2i(dv.y, dv.x);

Expand All @@ -87,46 +86,48 @@ void MapAddDoorGroup(
default: doorKey = "normal"; break;
}
const TileClass *doorClass = DoorGetClass(
&gTileClasses, &gPicManager, m->DoorStyle, doorKey, isHorizontal);
&gTileClasses, &gPicManager, mb->mission->DoorStyle, doorKey,
isHorizontal);
const TileClass *doorClassOpen = DoorGetClass(
&gTileClasses, &gPicManager, m->DoorStyle, "open", isHorizontal);
&gTileClasses, &gPicManager, mb->mission->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 *tile = MapGetTile(mb->Map, vI);
tile->ClassAlt = doorClass;
tile->Class = doorClassOpen;
if (isHorizontal)
{
const struct vec2i vB = svec2i_add(vI, dAside);
Tile *tileB = MapGetTile(map, vB);
Tile *tileB = MapGetTile(mb->Map, vB);
CASSERT(
TileCanWalk(MapGetTile(
map, svec2i(vI.x - dAside.x, vI.y - dAside.y)
mb->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;
const bool isFloor = IMapGet(mb->Map, vB) == MAP_FLOOR;
tileB->Class = TileClassesGetMaskedTile(
&gTileClasses,
&gPicManager,
&gTileFloor,
isFloor ? m->FloorStyle : m->RoomStyle,
isFloor ? mb->mission->FloorStyle : mb->mission->RoomStyle,
"shadow",
isFloor ? m->FloorMask : m->RoomMask,
m->AltMask
isFloor ? mb->mission->FloorMask : mb->mission->RoomMask,
mb->mission->AltMask
);
}
}

TWatch *w = CreateCloseDoorWatch(
map, m, v, isHorizontal, doorGroupCount, doorClass);
mb->Map, mb->mission, v, isHorizontal, doorGroupCount, doorClass);
Trigger *t = CreateOpenDoorTrigger(
map, m, v, isHorizontal, doorGroupCount, keyFlags);
mb->Map, mb->mission, v, isHorizontal, doorGroupCount, keyFlags);
// Connect trigger and watch up
Action *a = TriggerAddAction(t);
a->Type = ACTION_ACTIVATEWATCH;
Expand All @@ -139,11 +140,11 @@ void MapAddDoorGroup(
for (int i = 0; i < doorGroupCount; i++)
{
const struct vec2i vI = svec2i_add(v, svec2i_scale(dv, (float)i));
IMapSet(map, vI, IMapGet(map, vI) | MAP_LEAVEFREE);
MapBuilderSetLeaveFree(mb, vI, true);
const struct vec2i vI1 = svec2i_add(vI, dAside);
IMapSet(map, vI1, IMapGet(map, vI1) | MAP_LEAVEFREE);
MapBuilderSetLeaveFree(mb, vI1, true);
const struct vec2i vI2 = svec2i_subtract(vI, dAside);
IMapSet(map, vI2, IMapGet(map, vI2) | MAP_LEAVEFREE);
MapBuilderSetLeaveFree(mb, vI2, true);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/cdogs/door.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@
*/
#pragma once

#include "map.h"
#include "map_build.h"


void MapAddDoorGroup(
Map *map, const Mission *m, const struct vec2i v, const int keyFlags);
void MapAddDoorGroup(MapBuilder *mb, const struct vec2i v, const int keyFlags);

const TileClass *DoorGetClass(
TileClasses *c, const PicManager *pm,
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs/grafx_bg.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "game_events.h"
#include "handle_game_events.h"
#include "log.h"
#include "map_build.h"
#include "objs.h"
#include "pickup.h"
#include "quick_play.h"
Expand Down Expand Up @@ -149,8 +150,7 @@ void GrafxMakeBackground(
{
CampaignAndMissionSetup(co, mo);
GameEventsInit(&gGameEvents);
MapLoad(map, mo, co);
MapLoadDynamic(map, mo, &co->Setting.characters);
MapBuild(map, mo->missionData, co);
InitializeBadGuys();
CreateEnemies();
MapMarkAllAsVisited(map);
Expand Down
8 changes: 4 additions & 4 deletions src/cdogs/los.c
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) 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 @@ -33,14 +33,14 @@
#include "net_util.h"


void LOSInit(Map *map, const struct vec2i size)
void LOSInit(Map *map)
{
CArrayInit(&map->LOS.LOS, sizeof(bool));
CArrayInit(&map->LOS.Explored, sizeof(bool));
struct vec2i v;
for (v.y = 0; v.y < size.y; v.y++)
for (v.y = 0; v.y < map->Size.y; v.y++)
{
for (v.x = 0; v.x < size.x; v.x++)
for (v.x = 0; v.x < map->Size.x; v.x++)
{
const bool f = false;
CArrayPushBack(&map->LOS.LOS, &f);
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs/los.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) 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 @@ -32,7 +32,7 @@
#include "map.h"


void LOSInit(Map *map, const struct vec2i size);
void LOSInit(Map *map);
void LOSTerminate(LineOfSight *los);
void LOSReset(LineOfSight *los);
void LOSSetAllVisible(LineOfSight *los);
Expand Down
Loading

0 comments on commit 2a7dfe5

Please sign in to comment.