Skip to content

Commit

Permalink
Try forever placing baddies (fixes #152)
Browse files Browse the repository at this point in the history
If unable to place baddies away from player,
also try forever placing the baddie near the
player.

Increase max editor map size to 128x128
  • Loading branch information
cxong committed Sep 13, 2013
1 parent c8769d9 commit 3bde5ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 43 deletions.
55 changes: 14 additions & 41 deletions src/cdogs/ai.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ static int IsActorPositionValid(TActor *actor)
return MoveActor(actor, pos.x, pos.y);
}

static int TryPlaceBaddie(TActor *actor)
static void PlaceBaddie(TActor *actor)
{
int hasPlaced = 0;
int i;
Expand All @@ -405,18 +405,15 @@ static int TryPlaceBaddie(TActor *actor)
break;
}
}
if (!hasPlaced)
// Keep trying, but this time try spawning anywhere, even close to player
while (!hasPlaced)
{
// Keep trying, but this time try spawning anywhere, even close to player
for (i = 0; i < 100; i++) // Don't try forever trying to place baddie
actor->x = (rand() % (XMAX * TILE_WIDTH)) << 8;
actor->y = (rand() % (YMAX * TILE_HEIGHT)) << 8;
if (IsActorPositionValid(actor))
{
actor->x = (rand() % (XMAX * TILE_WIDTH)) << 8;
actor->y = (rand() % (YMAX * TILE_HEIGHT)) << 8;
if (IsActorPositionValid(actor))
{
hasPlaced = 1;
break;
}
hasPlaced = 1;
break;
}
}

Expand All @@ -434,8 +431,6 @@ static int TryPlaceBaddie(TActor *actor)
{
actor->flags &= ~FLAGS_SLEEPING;
}

return hasPlaced;
}

static void PlacePrisoner(TActor * actor)
Expand Down Expand Up @@ -622,14 +617,8 @@ void CommandBadGuys(int ticks)
rand() % gMission.missionData->baddieCount;
character = MIN(character, CHARACTER_COUNT);
baddie = AddActor(character);
if (!TryPlaceBaddie(baddie))
{
RemoveActor(baddie);
}
else
{
gBaddieCount++;
}
PlaceBaddie(baddie);
gBaddieCount++;
}
}

Expand All @@ -653,10 +642,7 @@ void InitializeBadGuys(void)
gMission.missionData->specialCount;
actor = AddActor(character);
actor->tileItem.flags |= ObjectiveToTileItem(i);
if (!TryPlaceBaddie(actor))
{
RemoveActor(actor);
}
PlaceBaddie(actor);
}
}
}
Expand All @@ -675,14 +661,7 @@ void InitializeBadGuys(void)
}
else
{
if (!TryPlaceBaddie(actor))
{
// Can't place prisoner when it's the objective
// Fatal error, can't recover
printf("Cannot place prisoner!\n");
assert(0);
exit(1);
}
PlaceBaddie(actor);
}
}
}
Expand All @@ -707,13 +686,7 @@ void CreateEnemies(void)
character = CHARACTER_OTHERS + rand() % gMission.missionData->baddieCount;
character = MIN(character, CHARACTER_COUNT);
enemy = AddActor(character);
if (!TryPlaceBaddie(enemy))
{
RemoveActor(enemy);
}
else
{
gBaddieCount++;
}
PlaceBaddie(enemy);
gBaddieCount++;
}
}
4 changes: 2 additions & 2 deletions src/cdogsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -940,10 +940,10 @@ static int Change(int yc, int xc, int d, int *mission)
case YC_MISSIONPROPS:
switch (xc) {
case XC_WIDTH:
currentMission->mapWidth = CLAMP(currentMission->mapWidth + d, 16, 64);
currentMission->mapWidth = CLAMP(currentMission->mapWidth + d, 16, XMAX);
break;
case XC_HEIGHT:
currentMission->mapHeight = CLAMP(currentMission->mapHeight + d, 16, 64);
currentMission->mapHeight = CLAMP(currentMission->mapHeight + d, 16, YMAX);
break;
case XC_WALLCOUNT:
currentMission->wallCount = CLAMP(currentMission->wallCount + d, 0, 200);
Expand Down

0 comments on commit 3bde5ad

Please sign in to comment.