Skip to content

Commit

Permalink
Refactor|libdoom: Moved "Boss brain" to new source files
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Feb 9, 2014
1 parent 2f4e8dd commit 0aa892f
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 126 deletions.
2 changes: 2 additions & 0 deletions doomsday/plugins/doom/doom.pro
Expand Up @@ -31,6 +31,7 @@ INCLUDEPATH += include

HEADERS += \
include/acfnlink.h \
include/bossbrain.h \
include/d_api.h \
include/d_config.h \
include/d_console.h \
Expand Down Expand Up @@ -70,6 +71,7 @@ HEADERS += \

SOURCES += \
src/acfnlink.c \
src/bossbrain.cpp \
src/d_api.c \
src/d_console.c \
src/d_items.c \
Expand Down
62 changes: 62 additions & 0 deletions doomsday/plugins/doom/include/bossbrain.h
@@ -0,0 +1,62 @@
/** @file bossbrain.h Playsim "Boss Brain" management.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 1999 Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman (PrBoom 2.2.6)
* @authors Copyright © 1999-2000 Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze (PrBoom 2.2.6)
* @authors Copyright © 1993-1996 id Software, Inc.
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBDOOM_PLAY_BOSSBRAIN_H
#define LIBDOOM_PLAY_BOSSBRAIN_H

#ifndef __JDOOM__
# error "Using jDoom headers without __JDOOM__"
#endif

#include "doomsday.h"
#include "p_mobj.h"

/**
* Global state of boss brain.
*/
typedef struct bossbrain_s {
int easy;
int targetOn;
int numTargets;
int maxTargets;
mobj_t **targets;
} bossbrain_t;

DENG_EXTERN_C bossbrain_t bossBrain;

#ifdef __cplusplus
extern "C" {
#endif

void P_BrainInitForMap(void);
void P_BrainShutdown(void);
void P_BrainClearTargets(void);
void P_BrainWrite(Writer *writer);
void P_BrainRead(Reader *reader, int mapVersion);
void P_BrainAddTarget(mobj_t *mo);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // LIBDOOM_PLAY_BOSSBRAIN_H
1 change: 1 addition & 0 deletions doomsday/plugins/doom/include/jdoom.h
Expand Up @@ -32,6 +32,7 @@
#define __JDOOM_CONVENIENCE_H__

#include "../../doom/include/acfnlink.h"
#include "../../doom/include/bossbrain.h"
#include "../../doom/include/d_api.h"
#include "../../doom/include/d_config.h"
#include "../../doom/include/d_console.h"
Expand Down
19 changes: 0 additions & 19 deletions doomsday/plugins/doom/include/p_enemy.h
Expand Up @@ -26,29 +26,10 @@

#include "jdoom.h"

/**
* Global state of boss brain.
*/
typedef struct braindata_s {
int easy;
int targetOn;
int numTargets;
int maxTargets;
mobj_t **targets;
} braindata_t;

DENG_EXTERN_C braindata_t brain;

#ifdef __cplusplus
extern "C" {
#endif

void P_BrainInitForMap(void);
void P_BrainShutdown(void);
void P_BrainClearTargets(void);
void P_BrainWrite(Writer *writer);
void P_BrainRead(Reader *reader, int mapVersion);
void P_BrainAddTarget(mobj_t *mo);
void P_NoiseAlert(mobj_t *target, mobj_t *emmiter);
int P_Massacre(void);

Expand Down
123 changes: 123 additions & 0 deletions doomsday/plugins/doom/src/bossbrain.cpp
@@ -0,0 +1,123 @@
/** @file bossbrain.cpp Playsim "Boss Brain" management.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 1999 Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman (PrBoom 2.2.6)
* @authors Copyright © 1999-2000 Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze (PrBoom 2.2.6)
* @authors Copyright © 1993-1996 id Software, Inc.
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#include "jdoom.h"
#include "bossbrain.h"

#include "p_saveg.h"

void P_BrainInitForMap()
{
bossBrain.easy = 0; // Always init easy to 0.
// Calling shutdown rather than clear allows us to free up memory.
P_BrainShutdown();
}

void P_BrainClearTargets()
{
bossBrain.numTargets = 0;
bossBrain.targetOn = 0;
}

void P_BrainWrite(Writer *writer)
{
DENG_ASSERT(writer != 0);

// Not for us?
if(!IS_SERVER) return;

Writer_WriteByte(writer, 1); // Write a version byte.

Writer_WriteInt16(writer, bossBrain.numTargets);
Writer_WriteInt16(writer, bossBrain.targetOn);
Writer_WriteByte(writer, bossBrain.easy!=0? 1:0);

// Write the mobj references using the mobj archive.
for(int i = 0; i < bossBrain.numTargets; ++i)
{
Writer_WriteInt16(writer, SV_ThingArchiveId(bossBrain.targets[i]));
}
}

void P_BrainRead(Reader *reader, int mapVersion)
{
DENG_ASSERT(reader != 0);

// Not for us?
if(!IS_SERVER) return;

// No brain data before version 3.
if(mapVersion < 3) return;

P_BrainClearTargets();

int ver = (mapVersion >= 8? Reader_ReadByte(reader) : 0);
int numTargets;
if(ver >= 1)
{
numTargets = Reader_ReadInt16(reader);
bossBrain.targetOn = Reader_ReadInt16(reader);
bossBrain.easy = (dd_bool)Reader_ReadByte(reader);
}
else
{
numTargets = Reader_ReadByte(reader);
bossBrain.targetOn = Reader_ReadByte(reader);
bossBrain.easy = false;
}

for(int i = 0; i < numTargets; ++i)
{
P_BrainAddTarget(SV_GetArchiveThing((int) Reader_ReadInt16(reader), 0));
}
}

void P_BrainShutdown()
{
Z_Free(bossBrain.targets); bossBrain.targets = 0;
bossBrain.numTargets = 0;
bossBrain.maxTargets = -1;
bossBrain.targetOn = 0;
}

void P_BrainAddTarget(mobj_t *mo)
{
DENG_ASSERT(mo != 0);

if(bossBrain.numTargets >= bossBrain.maxTargets)
{
// Do we need to alloc more targets?
if(bossBrain.numTargets == bossBrain.maxTargets)
{
bossBrain.maxTargets *= 2;
bossBrain.targets = (mobj_t **)Z_Realloc(bossBrain.targets, bossBrain.maxTargets * sizeof(*bossBrain.targets), PU_APPSTATIC);
}
else
{
bossBrain.maxTargets = 32;
bossBrain.targets = (mobj_t **)Z_Malloc(bossBrain.maxTargets * sizeof(*bossBrain.targets), PU_APPSTATIC, NULL);
}
}

bossBrain.targets[bossBrain.numTargets++] = mo;
}
113 changes: 6 additions & 107 deletions doomsday/plugins/doom/src/p_enemy.c
Expand Up @@ -42,7 +42,7 @@
#define SKULLSPEED (20)
#define TRACEANGLE (0xc000000)

braindata_t brain; // Global state of boss brain.
bossbrain_t bossBrain; // Global state of boss brain.

// Eight directional movement speeds.
#define MOVESPEED_DIAGONAL (0.71716309)
Expand Down Expand Up @@ -1730,107 +1730,6 @@ void C_DECL A_BabyMetal(mobj_t *mo)
A_Chase(mo);
}

void P_BrainInitForMap(void)
{
brain.easy = 0; // Always init easy to 0.
// Calling shutdown rather than clear allows us to free up memory.
P_BrainShutdown();
}

void P_BrainClearTargets(void)
{
brain.numTargets = 0;
brain.targetOn = 0;
}

void P_BrainWrite(Writer *writer)
{
int i;

DENG_ASSERT(writer != 0);

// Not for us?
if(!IS_SERVER) return;

Writer_WriteByte(writer, 1); // Write a version byte.

Writer_WriteInt16(writer, brain.numTargets);
Writer_WriteInt16(writer, brain.targetOn);
Writer_WriteByte(writer, brain.easy!=0? 1:0);

// Write the mobj references using the mobj archive.
for(i = 0; i < brain.numTargets; ++i)
{
Writer_WriteInt16(writer, SV_ThingArchiveId(brain.targets[i]));
}
}

void P_BrainRead(Reader *reader, int mapVersion)
{
int ver, numTargets;
int i;

DENG_ASSERT(reader != 0);

// Not for us?
if(!IS_SERVER) return;

// No brain data before version 3.
if(mapVersion < 3) return;

P_BrainClearTargets();

ver = (mapVersion >= 8? Reader_ReadByte(reader) : 0);
numTargets;
if(ver >= 1)
{
numTargets = Reader_ReadInt16(reader);
brain.targetOn = Reader_ReadInt16(reader);
brain.easy = (dd_bool)Reader_ReadByte(reader);
}
else
{
numTargets = Reader_ReadByte(reader);
brain.targetOn = Reader_ReadByte(reader);
brain.easy = false;
}

for(i = 0; i < numTargets; ++i)
{
P_BrainAddTarget(SV_GetArchiveThing((int) Reader_ReadInt16(reader), 0));
}
}

void P_BrainShutdown(void)
{
if(brain.targets)
Z_Free(brain.targets);
brain.targets = 0;
brain.numTargets = 0;
brain.maxTargets = -1;
brain.targetOn = 0;
}

void P_BrainAddTarget(mobj_t* mo)
{
if(brain.numTargets >= brain.maxTargets)
{
// Do we need to alloc more targets?
if(brain.numTargets == brain.maxTargets)
{
brain.maxTargets *= 2;
brain.targets = Z_Realloc(brain.targets, brain.maxTargets * sizeof(*brain.targets), PU_APPSTATIC);
}
else
{
brain.maxTargets = 32;
brain.targets = Z_Malloc(brain.maxTargets * sizeof(*brain.targets), PU_APPSTATIC, NULL);
}
}

brain.targets[brain.numTargets++] = mo;
}

void C_DECL A_BrainAwake(mobj_t *mo)
{
S_StartSound(SFX_BOSSIT, NULL);
Expand Down Expand Up @@ -1900,16 +1799,16 @@ void C_DECL A_BrainSpit(mobj_t* mo)
mobj_t* targ;
mobj_t* newmobj;

if(!brain.numTargets)
if(!bossBrain.numTargets)
return; // Ignore if no targets.

brain.easy ^= 1;
if(gameRules.skill <= SM_EASY && (!brain.easy))
bossBrain.easy ^= 1;
if(gameRules.skill <= SM_EASY && (!bossBrain.easy))
return;

// Shoot a cube at current target.
targ = brain.targets[brain.targetOn++];
brain.targetOn %= brain.numTargets;
targ = bossBrain.targets[bossBrain.targetOn++];
bossBrain.targetOn %= bossBrain.numTargets;

// Spawn brain missile.
newmobj = P_SpawnMissile(MT_SPAWNSHOT, mo, targ);
Expand Down

0 comments on commit 0aa892f

Please sign in to comment.