Skip to content

Commit

Permalink
#26: added parser support and Being variables and setters/getters for…
Browse files Browse the repository at this point in the history
… respawn settings
  • Loading branch information
crankycyclops committed May 27, 2013
1 parent 62e6a6b commit 3ff51e7
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 2 deletions.
25 changes: 25 additions & 0 deletions game.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@
<intelligence>10</intelligence>
</attributes>

<!-- settings governing player's ability to respawn when killed. This
section is optional, and if it doesn't exist, default behavior is
for respawn to be disabled. -->
<respawn>

<!-- 0 = false and 1 = true. If enabled, the player will come back
to life after being killed. Default is 0 for false. -->
<enabled>1</enabled>

<!-- Number of clock ticks (seconds) to wait before player respawns.
If set to 0, player will respawn immediately. (optional:
default = 0.) -->
<interval>0</interval>

<!-- Number of times the player can respawn before dying
permanently. If set to -1, player can respawn an unlimited
number of times. (optional: default = -1.) -->
<lives>-1</lives>

</respawn>

<!-- whether or not player starts out alive or dead (optional:
default is 1 for alive) -->
<alive>1</alive>
Expand Down Expand Up @@ -229,6 +250,10 @@ A smooth metallic sheen covers its surface, which looks to be of recent make and
<!-- Creatures -->
<creatures>

<!-- NOTE: a respawn section can also go here if you want your creature
to respawn. This section is identical to the one in the default
player configuration, so you would do well to look there for an
example ;) -->
<creature name="trogdor">

<title>Trogdor the Burninator</title>
Expand Down
8 changes: 8 additions & 0 deletions src/core/entities/being.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,12 @@ namespace core { namespace entity {
game->addEventListener(triggers);
game->event("afterDie", eventArgs);
}

/***************************************************************************/

void Being::respawn() {

cout << name << ": Respawn stub!" << endl;
}
}}

97 changes: 96 additions & 1 deletion src/core/include/entities/being.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace core { namespace entity {
static const int DEFAULT_ATTRIBUTE_DEXTERITY = 10;
static const int DEFAULT_ATTRIBUTE_INTELLIGENCE = 10;

static const bool DEFAULT_RESPAWN_ENABLED = false;
static const int DEFAULT_RESPAWN_INTERVAL = 0;
static const int DEFAULT_RESPAWN_LIVES = -1;

static const int DEFAULT_INVENTORY_WEIGHT = 0;

static const bool DEFAULT_ATTACKABLE = true;
Expand All @@ -48,6 +52,12 @@ namespace core { namespace entity {
double woundRate; // max probability of being hit when attacked
int damageBareHands; // damage done during combat without a weapon

struct {
bool enabled;
int interval;
int lives;
} respawnSettings;

struct {
int strength;
int dexterity;
Expand Down Expand Up @@ -162,6 +172,10 @@ namespace core { namespace entity {
attackable = DEFAULT_ATTACKABLE;
damageBareHands = DEFAULT_DAMAGE_BARE_HANDS;

respawnSettings.enabled = DEFAULT_RESPAWN_ENABLED;
respawnSettings.interval = DEFAULT_RESPAWN_INTERVAL;
respawnSettings.lives = DEFAULT_RESPAWN_LIVES;

attributes.strength = DEFAULT_ATTRIBUTE_STRENGTH;
attributes.dexterity = DEFAULT_ATTRIBUTE_DEXTERITY;
attributes.intelligence = DEFAULT_ATTRIBUTE_INTELLIGENCE;
Expand Down Expand Up @@ -191,6 +205,10 @@ namespace core { namespace entity {
woundRate = b.woundRate;
damageBareHands = b.damageBareHands;

respawnSettings.enabled = b.respawnSettings.enabled;
respawnSettings.interval = b.respawnSettings.interval;
respawnSettings.lives = b.respawnSettings.lives;

attributes.strength = b.attributes.strength;
attributes.dexterity = b.attributes.dexterity;
attributes.intelligence = b.attributes.intelligence;
Expand Down Expand Up @@ -265,6 +283,41 @@ namespace core { namespace entity {
*/
inline bool isAlive() const {return alive;}

/*
Indicates whether or not respawning is enabled for this Being.
Input:
(none)
Output:
true or false (bool)
*/
inline bool getRespawnEnabled() const {return respawnSettings.enabled;}

/*
Returns the number of clock ticks that should pass before Being
respawns after dying.
Input:
(none)
Output:
number of clock ticks (int)
*/
inline int getRespawnInterval() const {return respawnSettings.interval;}

/*
Returns the number of times a Being can respawn before dying
permanently.
Input:
(none)
Output:
number of lives left; -1 indicates unlimited lives (int)
*/
inline int getRespawnLives() const {return respawnSettings.lives;}

/*
Returns the maximum weight of the Being's inventory.
Expand Down Expand Up @@ -426,6 +479,36 @@ namespace core { namespace entity {
*/
inline void setDamageBareHands(int d) {damageBareHands = d;}

/*
Setters for respawn settings.
Input:
values (bool or int)
Output:
(none)
*/
inline void setRespawnEnabled(bool b) {respawnSettings.enabled = b;}
inline void setRespawnInterval(int i) {respawnSettings.interval = i;}
inline void setRespawnLives(int i) {respawnSettings.lives = i;}

/*
Increments the Being's number of lives.
Input:
(none)
Output:
(none)
*/
inline void incRespawnLives() {

// only increment number of lives if not unlimited
if (respawnSettings.lives > -1) {
respawnSettings.lives++;
}
}

/*
Sets the Being's attributes.
Expand Down Expand Up @@ -588,7 +671,7 @@ namespace core { namespace entity {
void removeHealth(int down, bool allowDeath = true);

/*
Make the Being die. Triggers an event.
Make the Being die. Triggers beforeDie and afterDie events.
Input:
(none)
Expand All @@ -597,6 +680,18 @@ namespace core { namespace entity {
(none)
*/
void die();

/*
The opposite of die :) Triggers beforeRespawn and afterRespawn
events.
Input:
(none)
Output:
(none)
*/
void respawn();
};
}}

Expand Down
17 changes: 16 additions & 1 deletion src/core/include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,26 @@ namespace core {
*/
bool parseCreatureAutoAttackRepeat();

/*
Parses a Being's respawn settings.
Input:
Pointer to the being whose respawn settings are being configured
Depth in the XML tree
Output:
(none)
*/
void parseBeingRespawn(entity::Being *being, int depth);
bool parseBeingRespawnEnabled();
int parseBeingRespawnInterval();
int parseBeingRespawnLives();

/*
Parses a Being's inventory settings.
Input:
Reference to the being whose inventory is being configured
Pointer to the being whose inventory is being configured
Whether or not to allow a set of objects initialized in game.xml
Output:
Expand Down
66 changes: 66 additions & 0 deletions src/core/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ namespace core {
parseBeingInventory(defaultPlayer, false);
}

else if (0 == getTagName().compare("respawn")) {
Parser::parseBeingRespawn(defaultPlayer, 4);
}

else if (0 == getTagName().compare("attributes")) {
parseBeingAttributes(defaultPlayer);
}
Expand Down Expand Up @@ -616,6 +620,10 @@ namespace core {
creature->setCounterAttack(parseCreatureCounterAttack());
}

else if (0 == getTagName().compare("respawn")) {
Parser::parseBeingRespawn(creature, 4);
}

else if (0 == getTagName().compare("autoattack")) {
parseCreatureAutoAttack(creature, 4);
}
Expand Down Expand Up @@ -960,6 +968,64 @@ namespace core {

/***************************************************************************/

void Parser::parseBeingRespawn(Being *being, int depth) {

stringstream s;

while (nextTag() && depth == getDepth()) {

if (0 == getTagName().compare("enabled")) {
being->setRespawnEnabled(parseBeingRespawnEnabled());
}

else if (0 == getTagName().compare("interval")) {
being->setRespawnInterval(parseBeingRespawnInterval());
}

else if (0 == getTagName().compare("lives")) {
being->setRespawnLives(parseBeingRespawnLives());
}

else {
s << filename << ": invalid tag <" << getTagName() << "> in "
<< "being respawn section (line "
<< xmlTextReaderGetParserLineNumber(reader) << ")";
throw s.str();
}
}

checkClosingTag("respawn");
}

/***************************************************************************/

bool Parser::parseBeingRespawnEnabled() {

bool enabled = parseBool();
checkClosingTag("enabled");
return enabled;
}

/***************************************************************************/

int Parser::parseBeingRespawnInterval() {

int interval = parseInt();
checkClosingTag("interval");
return interval;
}

/***************************************************************************/

int Parser::parseBeingRespawnLives() {

int lives = parseInt();
checkClosingTag("lives");
return lives;
}

/***************************************************************************/

void Parser::parseBeingInventory(entity::Being *being, bool allowObjects) {

stringstream s;
Expand Down

0 comments on commit 3ff51e7

Please sign in to comment.