Skip to content

Commit

Permalink
Fix editor (#140)
Browse files Browse the repository at this point in the history
Change Character to hold CharBot locally, to eliminate memory issues
  • Loading branch information
cxong committed Nov 1, 2013
1 parent a4c8b4c commit 5b33b34
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 227 deletions.
3 changes: 1 addition & 2 deletions src/cdogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ void DrawObjectiveInfo(int idx, int x, int y, struct Mission *mission)
switch (mission->objectives[idx].type)
{
case OBJECTIVE_KILL:
cd = CharacterStoreGetOther(
&gCampaign.Setting.characters, mission->baddieCount);
cd = CharacterStoreGetSpecial(&gCampaign.Setting.characters, 0);
i = cd->looks.face;
table = &cd->table;
pic.picIndex = cHeadPic[i][DIRECTION_DOWN][STATE_IDLE];
Expand Down
5 changes: 0 additions & 5 deletions src/cdogs/actors.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@
#include "weapon.h"


#define CHARACTER_PLAYER1 0
#define CHARACTER_PLAYER2 1
#define CHARACTER_PRISONER 2
#define CHARACTER_OTHERS 3

#define FLAGS_PLAYER1 (1 << 0)
#define FLAGS_PLAYER2 (1 << 1)
#define FLAGS_PLAYERS (FLAGS_PLAYER1 | FLAGS_PLAYER2)
Expand Down
12 changes: 6 additions & 6 deletions src/cdogs/ai.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ static int DirectionOK(TActor * actor, int dir)
static int BrightWalk(TActor * actor, int roll)
{
if (!!(actor->flags & FLAGS_VISIBLE) &&
roll < actor->character->bot->probabilityToTrack)
roll < actor->character->bot.probabilityToTrack)
{
actor->flags &= ~FLAGS_DETOURING;
return Hunt(actor);
Expand Down Expand Up @@ -351,7 +351,7 @@ static int WillFire(TActor * actor, int roll)
{
if ((actor->flags & FLAGS_VISIBLE) != 0 &&
WeaponCanFire(&actor->weapon) &&
roll < actor->character->bot->probabilityToShoot)
roll < actor->character->bot.probabilityToShoot)
{
if ((actor->flags & FLAGS_GOOD_GUY) != 0)
return 1; //!FacingPlayer( actor);
Expand Down Expand Up @@ -511,7 +511,7 @@ void CommandBadGuys(int ticks)
{
cmd = Follow(actor);
}
actor->delay = actor->character->bot->actionDelay;
actor->delay = actor->character->bot.actionDelay;
}
else if (!!(actor->flags & FLAGS_SNEAKY) &&
!!(actor->flags & FLAGS_VISIBLE) &&
Expand All @@ -532,19 +532,19 @@ void CommandBadGuys(int ticks)
}
else
{
if (roll < actor->character->bot->probabilityToTrack)
if (roll < actor->character->bot.probabilityToTrack)
{
cmd = Hunt(actor);
}
else if (roll < actor->character->bot->probabilityToMove)
else if (roll < actor->character->bot.probabilityToMove)
{
cmd = DirectionToCmd(rand() & 7);
}
else
{
cmd = 0;
}
actor->delay = actor->character->bot->actionDelay * delayModifier;
actor->delay = actor->character->bot.actionDelay * delayModifier;
}
if (!bypass)
{
Expand Down
1 change: 0 additions & 1 deletion src/cdogs/campaigns.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ typedef struct
char description[200];
int missionCount;
struct Mission *missions;
int characterCount; // note: legacy, only used for editor
CharacterStore characters;
} CampaignSettingNew;

Expand Down
51 changes: 30 additions & 21 deletions src/cdogs/character.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,10 @@ void CharacterStoreInit(CharacterStore *store)
{
memset(store, 0, sizeof *store);
store->playerCount = CHARACTER_PLAYER_COUNT;
CCALLOC(store->players, sizeof *store->players * store->playerCount);
}

void CharacterStoreTerminate(CharacterStore *store)
{
int i;
for (i = 0; i < store->playerCount; i++)
{
CFREE(store->players[i].bot);
}
CFREE(store->players);
for (i = 0; i < store->otherCount; i++)
{
CFREE(store->others[i].bot);
}
CFREE(store->others);
CFREE(store->prisoners);
CFREE(store->baddies);
CFREE(store->specials);
Expand All @@ -135,10 +123,35 @@ void CharacterStoreResetOthers(CharacterStore *store)

Character *CharacterStoreAddOther(CharacterStore *store)
{
assert(!store->hasGet);
return CharacterStoreInsertOther(store, store->otherCount);
}
Character *CharacterStoreInsertOther(CharacterStore *store, int idx)
{
int i;
for (i = store->otherCount; i > idx; i--)
{
memcpy(
&store->others[i], &store->others[i - 1], sizeof store->others[i]);
}
store->otherCount++;
CREALLOC(store->others, store->otherCount * sizeof *store->others);
return &store->others[store->otherCount - 1];
return &store->others[idx];
}
void CharacterStoreDeleteOther(CharacterStore *store, int idx)
{
int i;
if (store->otherCount == 0)
{
return;
}
store->otherCount--;
memset(&store->others[idx], 0, sizeof store->others[idx]);
for (i = idx; i < store->otherCount; i++)
{
memcpy(
&store->others[i],
&store->others[i + 1],
sizeof store->others[i]);
}
}

void CharacterStoreAddPrisoner(CharacterStore *store, int character)
Expand All @@ -163,22 +176,18 @@ void CharacterStoreAddSpecial(CharacterStore *store, int character)

Character *CharacterStoreGetPrisoner(CharacterStore *store, int i)
{
store->hasGet = 1;
return store->prisoners[i];
}

Character *CharacterStoreGetOther(CharacterStore *store, int i)
Character *CharacterStoreGetSpecial(CharacterStore *store, int i)
{
store->hasGet = 1;
return &store->others[i];
return store->specials[i];
}
Character *CharacterStoreGetRandomBaddie(CharacterStore *store)
{
store->hasGet = 1;
return store->baddies[rand() % store->baddieCount];
}
Character *CharacterStoreGetRandomSpecial(CharacterStore *store)
{
store->hasGet = 1;
return store->specials[rand() % store->specialCount];
}
17 changes: 10 additions & 7 deletions src/cdogs/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,19 @@ typedef struct
int maxHealth;
int flags;
TranslationTable table;
CharBot *bot; // NULL for human players
CharBot bot;
} Character;

#define CHARACTER_PLAYER_COUNT 2
#define CHARACTER_OTHER_COUNT 64
#define CHARACTER_PLAYER1 0
#define CHARACTER_PLAYER2 1
typedef struct
{
int hasGet; // check that no adds occur after getting
int playerCount;
Character *players; // human players
Character players[CHARACTER_PLAYER_COUNT]; // human players
int otherCount;
Character *others; // both normal baddies and special chars
Character others[CHARACTER_OTHER_COUNT]; // both normal baddies and special chars

// References only
int prisonerCount;
Expand All @@ -79,19 +82,19 @@ typedef struct
Character **specials;
} CharacterStore;

#define CHARACTER_PLAYER_COUNT 2

void CharacterSetLooks(Character *c, const CharLooks *l);

void CharacterStoreInit(CharacterStore *store);
void CharacterStoreTerminate(CharacterStore *store);
void CharacterStoreResetOthers(CharacterStore *store);
Character *CharacterStoreAddOther(CharacterStore *store);
Character *CharacterStoreInsertOther(CharacterStore *store, int idx);
void CharacterStoreDeleteOther(CharacterStore *store, int idx);
void CharacterStoreAddPrisoner(CharacterStore *store, int character);
void CharacterStoreAddBaddie(CharacterStore *store, int character);
void CharacterStoreAddSpecial(CharacterStore *store, int character);
Character *CharacterStoreGetPrisoner(CharacterStore *store, int i);
Character *CharacterStoreGetOther(CharacterStore *store, int i);
Character *CharacterStoreGetSpecial(CharacterStore *store, int i);
Character *CharacterStoreGetRandomBaddie(CharacterStore *store);
Character *CharacterStoreGetRandomSpecial(CharacterStore *store);

Expand Down
55 changes: 38 additions & 17 deletions src/cdogs/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,25 +354,11 @@ void AddItemToDisplayList(TTileItem * t, TTileItem **list)
}
}

void DisplayPlayer(int x, const char *name, Character *c, int editingName)
static void DrawCharacter(Character *c, Vec2i pos)
{
TOffsetPic body, head;
char s[22];
direction_e dir = DIRECTION_DOWN;
int state = STATE_IDLE;
int y;

y = gGraphicsDevice.cachedConfig.ResolutionHeight / 10;

if (editingName)
{
sprintf(s, "%c%s%c", '\020', name, '\021');
CDogsTextStringAt(x, y, s);
}
else
{
CDogsTextStringAt(x, y, name);
}

body.dx = cBodyOffset[c->looks.unarmedBody][dir].dx;
body.dy = cBodyOffset[c->looks.unarmedBody][dir].dy;
Expand All @@ -388,11 +374,46 @@ void DisplayPlayer(int x, const char *name, Character *c, int editingName)
head.picIndex = cHeadPic[c->looks.face][dir][state];

DrawTTPic(
x + 20 + body.dx, y + 36 + body.dy,
pos.x + body.dx, pos.y + body.dy,
PicManagerGetOldPic(&gPicManager, body.picIndex),
c->table);
DrawTTPic(
x + 20 + head.dx, y + 36 + head.dy,
pos.x + head.dx, pos.y + head.dy,
PicManagerGetOldPic(&gPicManager, head.picIndex),
c->table);
}

void DisplayPlayer(int x, const char *name, Character *c, int editingName)
{
Vec2i pos = Vec2iNew(x, gGraphicsDevice.cachedConfig.ResolutionHeight / 10);
Vec2i playerPos = Vec2iAdd(pos, Vec2iNew(20, 36));

if (editingName)
{
char s[22];
sprintf(s, "%c%s%c", '\020', name, '\021');
CDogsTextStringAt(pos.x, pos.y, s);
}
else
{
CDogsTextStringAt(pos.x, pos.y, name);
}

DrawCharacter(c, playerPos);
}

void DisplayCharacter(int x, int y, int character, int hilite, int showGun)
{
Character *c = &gCampaign.Setting.characters.others[character];
DrawCharacter(c, Vec2iNew(x, y));
if (hilite)
{
CDogsTextGoto(x - 8, y - 16);
CDogsTextChar('\020');
if (showGun)
{
CDogsTextGoto(x - 8, y + 8);
CDogsTextString(gGunDescriptions[c->gun].gunName);
}
}
}
1 change: 1 addition & 0 deletions src/cdogs/draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ void LineOfSight(Vec2i center, DrawBuffer *buffer, int shadowFlag);
void FixBuffer(DrawBuffer *b, int isShadow);
void DrawBufferDraw(DrawBuffer *b, int xOffset);
void DisplayPlayer(int x, const char *name, Character *c, int editingName);
void DisplayCharacter(int x, int y, int character, int hilite, int showGun);

#endif
29 changes: 14 additions & 15 deletions src/cdogs/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,10 @@ void ConvertCharacter(Character *c, TBadGuy *b)
c->looks.unarmedBody = b->unarmedBodyPic;
c->looks.face = b->facePic;
c->speed = b->speed;
CMALLOC(c->bot, sizeof *c->bot);
c->bot->probabilityToMove = b->probabilityToMove;
c->bot->probabilityToTrack = b->probabilityToTrack;
c->bot->probabilityToShoot = b->probabilityToShoot;
c->bot->actionDelay = b->actionDelay;
c->bot.probabilityToMove = b->probabilityToMove;
c->bot.probabilityToTrack = b->probabilityToTrack;
c->bot.probabilityToShoot = b->probabilityToShoot;
c->bot.actionDelay = b->actionDelay;
c->gun = (gun_e)b->gun;
c->looks.skin = b->skinColor;
c->looks.arm = b->armColor;
Expand All @@ -297,10 +296,10 @@ TBadGuy ConvertTBadGuy(Character *e)
b.unarmedBodyPic = e->looks.unarmedBody;
b.facePic = e->looks.face;
b.speed = e->speed;
b.probabilityToMove = e->bot->probabilityToMove;
b.probabilityToTrack = e->bot->probabilityToTrack;
b.probabilityToShoot = e->bot->probabilityToShoot;
b.actionDelay = e->bot->actionDelay;
b.probabilityToMove = e->bot.probabilityToMove;
b.probabilityToTrack = e->bot.probabilityToTrack;
b.probabilityToShoot = e->bot.probabilityToShoot;
b.actionDelay = e->bot.actionDelay;
b.gun = e->gun;
b.skinColor = e->looks.skin;
b.armColor = e->looks.arm;
Expand Down Expand Up @@ -446,9 +445,9 @@ int SaveCampaign(const char *filename, CampaignSettingNew *setting)
CHECK_WRITE(fwrite(&setting->missions[i], sizeof(struct Mission), 1, f) == 1)
}

i = setting->characterCount;
i = setting->characters.otherCount;
CHECK_WRITE(fwrite32(f, &i))
for (i = 0; i < setting->characterCount; i++)
for (i = 0; i < setting->characters.otherCount; i++)
{
TBadGuy b = ConvertTBadGuy(&setting->characters.others[i]);
CHECK_WRITE(fwrite(&b, sizeof(TBadGuy), 1, f) == 1)
Expand Down Expand Up @@ -503,8 +502,8 @@ void SaveCampaignAsC(
return;
}
fprintf(f, "TBadGuy %s_badguys[ %d] =\n{\n", name,
setting->characterCount);
for (i = 0; i < setting->characterCount; i++)
setting->characters.otherCount);
for (i = 0; i < setting->characters.otherCount; i++)
{
TBadGuy b = ConvertTBadGuy(&setting->characters.others[i]);
fprintf(f,
Expand All @@ -525,7 +524,7 @@ void SaveCampaignAsC(
b.hairColor,
b.health,
b.flags,
i < setting->characterCount - 1 ? "," : "");
i < setting->characters.otherCount - 1 ? "," : "");
}
fprintf(f, "};\n\n");

Expand Down Expand Up @@ -649,7 +648,7 @@ void SaveCampaignAsC(
fprintf(f, ",\n");
fprintf(f, " %d, %s_missions, %d, %s_badguys\n};\n",
setting->missionCount, name,
setting->characterCount, name);
setting->characters.otherCount, name);

printf("Saved to %s\n", filename);
fclose(f);
Expand Down
Loading

0 comments on commit 5b33b34

Please sign in to comment.