Skip to content

Commit

Permalink
Merge pull request #8 from CleverRaven/master
Browse files Browse the repository at this point in the history
  • Loading branch information
acidia committed Jul 7, 2014
2 parents 4b36438 + f64e68e commit 3bb29cf
Show file tree
Hide file tree
Showing 23 changed files with 740 additions and 396 deletions.
2 changes: 1 addition & 1 deletion data/json/items/comestibles.json
Original file line number Diff line number Diff line change
Expand Up @@ -12118,7 +12118,7 @@
"price" : 500,
"material" : "null",
"tool" : "null",
"volume" : 1,
"volume" : 0,
"cutting" : 0,
"phase" : "solid",
"charges" : 1,
Expand Down
5 changes: 3 additions & 2 deletions data/json/mapgen/evac_center.json
Original file line number Diff line number Diff line change
Expand Up @@ -839,11 +839,11 @@
" + bL0 ",
" + hL0 ",
" ## b## ",
"##=#### zz #######",
"##+#### zz #######",
" ## zz #######",
" ### ########",
" k6k ###### ###########",
" h #####EEEE##########",
" h #####EEE6##########",
" #####EEEE##########",
"##########EEEE##########",
"##########EEEE##########"
Expand All @@ -855,6 +855,7 @@
{ "item": "cleaning", "chance": 80, "x": [ 4, 4 ], "y": [ 8, 10 ]}
],
"place_specials": [
{ "type": "npc", "class": "evac_broker", "x": 2, "y": 20 },
{ "type": "npc", "class": "guard", "x": 15, "y": 15 }
],
"terrain": {
Expand Down
150 changes: 72 additions & 78 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,34 @@
#include <algorithm>
#include <numeric>
#include <cmath>

std::map<int, std::map<body_part, double> > Creature::default_hit_weights;
#include <map>

static std::map<int, std::map<body_part, double> > default_hit_weights = {
{ -1, /* attacker smaller */
{ { bp_eyes, 0.f },
{ bp_head, 0.f },
{ bp_torso, 55.f },
{ bp_arms, 35.f },
{ bp_legs, 55.f } } },
{ 0, /* attacker equal size */
{ { bp_eyes, 10.f },
{ bp_head, 20.f },
{ bp_torso, 55.f },
{ bp_arms, 55.f },
{ bp_legs, 35.f } } },
{ 1, /* attacker larger */
{ { bp_eyes, 5.f },
{ bp_head, 25.f },
{ bp_torso, 55.f },
{ bp_arms, 55.f },
{ bp_legs, 20.f } } } };

struct weight_compare {
bool operator() (const std::pair<body_part, double> &left,
const std::pair<body_part, double> &right) {
return left.second < right.second;
}
};

Creature::Creature()
{
Expand Down Expand Up @@ -510,55 +536,34 @@ void Creature::set_fake(const bool fake_value)
}

/*
* Effect-related functions
* Effect-related methods
*/
// Some utility functions for effects
class is_id_functor // functor for remove/has_effect, give c++11 lambdas pls
{
std::string id;
public:
is_id_functor(efftype_id rem_id) : id(rem_id) {}
bool operator() (effect &e) {
return e.get_id() == id;
}
};
bool is_expired_effect(effect &e) // utility function for process_effects
{
if (e.get_duration() <= 0) {
add_msg(e.get_effect_type()->lose_game_message_type(), e.get_effect_type()->get_remove_message().c_str());
g->u.add_memorial_log(pgettext("memorial_male", e.get_effect_type()->get_remove_memorial_log().c_str()),
pgettext("memorial_female", e.get_effect_type()->get_remove_memorial_log().c_str()));
return true;
} else {
return false;
}
}

void Creature::add_effect(efftype_id eff_id, int dur, int intensity, bool permanent)
{
// check if we already have it
std::vector<effect>::iterator first_eff =
std::find_if(effects.begin(), effects.end(), is_id_functor(eff_id));
auto found_effect = effects.find( eff_id );

if (first_eff != effects.end()) {
if (found_effect != effects.end()) {
effect &e = found_effect->second;
// if we do, mod the duration
first_eff->mod_duration(dur);
e.mod_duration(dur);
// Adding a permanent effect makes it permanent
if (first_eff->is_permanent()) {
first_eff->pause_effect();
if( e.is_permanent() ) {
e.pause_effect();
}
if (first_eff->get_intensity() + intensity <= first_eff->get_max_intensity()) {
first_eff->mod_intensity(intensity);
if( e.get_intensity() + intensity <= e.get_max_intensity() ) {
e.mod_intensity( intensity );
}
} else {
// if we don't already have it then add a new one
if (effect_types.find(eff_id) == effect_types.end()) {
return;
}
effect new_eff(&effect_types[eff_id], dur, intensity, permanent);
effects.push_back(new_eff);
effects[eff_id] = new_eff;
if (is_player()) { // only print the message if we didn't already have it
add_msg(effect_types[eff_id].gain_game_message_type(), effect_types[eff_id].get_apply_message().c_str());
add_msg( effect_types[eff_id].gain_game_message_type(),
effect_types[eff_id].get_apply_message().c_str() );
g->u.add_memorial_log(pgettext("memorial_male",
effect_types[eff_id].get_apply_memorial_log().c_str()),
pgettext("memorial_female",
Expand All @@ -583,30 +588,48 @@ void Creature::clear_effects()
void Creature::remove_effect(efftype_id rem_id)
{
// remove all effects with this id
effects.erase(std::remove_if(effects.begin(), effects.end(),
is_id_functor(rem_id)), effects.end());
effects.erase( rem_id );
}
bool Creature::has_effect(efftype_id eff_id)
bool Creature::has_effect(efftype_id eff_id) const
{
// return if any effect in effects has this id
return (std::find_if(effects.begin(), effects.end(), is_id_functor(eff_id)) !=
effects.end());
return effects.find( eff_id ) != effects.end();
}
void Creature::process_effects()
{
for (std::vector<effect>::iterator it = effects.begin();
it != effects.end(); ++it) {
if (!it->is_permanent() && it->get_duration() > 0) {
it->mod_duration(-1);
for( auto it = effects.begin(); it != effects.end(); ++it ) {
if( !it->second.is_permanent() ) {
it->second.mod_duration( -1 );
if (g->debugmon) {
debugmsg("Duration %d", it->get_duration());
debugmsg("Duration %d", it->second.get_duration());
}
if( it->second.get_duration() <= 0 ) {
const effect_type *type = it->second.get_effect_type();
add_msg( type->lose_game_message_type(), type->get_remove_message().c_str() );
g->u.add_memorial_log(
pgettext("memorial_male", type->get_remove_memorial_log().c_str() ),
pgettext("memorial_female", type->get_remove_memorial_log().c_str()) );
}
}
}
effects.erase(std::remove_if(effects.begin(), effects.end(),
is_expired_effect), effects.end());
}

// Methods for setting/getting misc key/value pairs.
void Creature::set_value( const std::string key, const std::string value )
{
values[ key ] = value;
}

void Creature::remove_value( const std::string key )
{
values.erase( key );
}

std::string Creature::get_value( const std::string key ) const
{
auto it = values.find( key );
return ( it == values.end() ) ? "" : it->second;
}

void Creature::mod_pain(int npain)
{
Expand Down Expand Up @@ -1056,7 +1079,7 @@ body_part Creature::select_body_part(Creature *source, int hit_roll)
}

double totalWeight = 0;
std::set<weight_pair, weight_compare> adjusted_weights;
std::set<std::pair<body_part, double>, weight_compare> adjusted_weights;
for(iter = hit_weights.begin(); iter != hit_weights.end(); ++iter) {
totalWeight += iter->second;
adjusted_weights.insert(*iter);
Expand All @@ -1065,7 +1088,7 @@ body_part Creature::select_body_part(Creature *source, int hit_roll)
double roll = rng_float(1, totalWeight);
body_part selected_part = bp_torso;

std::set<weight_pair, weight_compare>::iterator adj_iter;
std::set<std::pair<body_part, double>, weight_compare>::iterator adj_iter;
for(adj_iter = adjusted_weights.begin(); adj_iter != adjusted_weights.end(); ++adj_iter) {
roll -= adj_iter->second;
if(roll <= 0) {
Expand All @@ -1077,35 +1100,6 @@ body_part Creature::select_body_part(Creature *source, int hit_roll)
return selected_part;
}

void Creature::init_hit_weights()
{
std::map<body_part, double> attacker_equal_weights;
std::map<body_part, double> attacker_smaller_weights;
std::map<body_part, double> attacker_bigger_weights;

attacker_equal_weights[bp_eyes] = 10.f;
attacker_equal_weights[bp_head] = 20.f;
attacker_equal_weights[bp_torso] = 55.f;
attacker_equal_weights[bp_arms] = 55.f;
attacker_equal_weights[bp_legs] = 35.f;

attacker_smaller_weights[bp_eyes] = 0.f;
attacker_smaller_weights[bp_head] = 0.f;
attacker_smaller_weights[bp_torso] = 55.f;
attacker_smaller_weights[bp_arms] = 35.f;
attacker_smaller_weights[bp_legs] = 55.f;

attacker_bigger_weights[bp_eyes] = 5.f;
attacker_bigger_weights[bp_head] = 25.f;
attacker_bigger_weights[bp_torso] = 55.f;
attacker_bigger_weights[bp_arms] = 55.f;
attacker_bigger_weights[bp_legs] = 20.f;

default_hit_weights[-1] = attacker_smaller_weights;
default_hit_weights[0] = attacker_equal_weights;
default_hit_weights[1] = attacker_bigger_weights;
}

Creature& Creature::operator= (const Creature& rhs)
{
str_cur = rhs.str_cur;
Expand Down
30 changes: 11 additions & 19 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
#include "messages.h"
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <unordered_map>

class game;
class effect;

class Creature
{
Expand Down Expand Up @@ -140,7 +138,12 @@ class Creature
int intensity = 1, bool permanent = false); // gives chance to save via env resist, returns if successful
void remove_effect(efftype_id eff_id);
void clear_effects(); // remove all effects
bool has_effect(efftype_id eff_id);
bool has_effect(efftype_id eff_id) const;

// Methods for setting/getting misc key/value pairs.
void set_value( const std::string key, const std::string value );
void remove_value( const std::string key );
std::string get_value( const std::string key ) const;

virtual void process_effects(); // runs all the effects on the Creature

Expand Down Expand Up @@ -272,8 +275,6 @@ class Creature

void draw(WINDOW *w, int plx, int ply, bool inv);

static void init_hit_weights();

// Message related stuff
virtual void add_msg_if_player(const char *, ...){};
virtual void add_msg_if_player(game_message_type, const char *, ...){};
Expand All @@ -287,7 +288,9 @@ class Creature
protected:
Creature *killer; // whoever killed us. this should be NULL unless we are dead

std::vector<effect> effects;
std::unordered_map<std::string, effect> effects;
// Miscelaneous key/value pairs.
std::unordered_map<std::string, std::string> values;

// used for innate bonuses like effects. weapon bonuses will be
// handled separately
Expand Down Expand Up @@ -330,18 +333,7 @@ class Creature
virtual const std::string &symbol() const;
virtual bool is_symbol_highlighted();


//Hit weight work.
static std::map<int, std::map<body_part, double> > default_hit_weights;

typedef std::pair<body_part, double> weight_pair;

struct weight_compare {
bool operator() (const weight_pair &left, const weight_pair &right) { return left.second < right.second;}
};

body_part select_body_part(Creature *source, int hitroll);

body_part select_body_part(Creature *source, int hit_roll);
};

#endif
Expand Down
2 changes: 2 additions & 0 deletions src/dialogue.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct talk_function
void mission_favor (npc*);
void give_equipment (npc*);
void start_trade (npc*);
std::string bulk_trade_inquire (npc*, itype_id);
void bulk_trade_accept (npc*, itype_id);
void assign_base (npc*);
void assign_guard (npc*);
void stop_guard (npc*);
Expand Down
20 changes: 10 additions & 10 deletions src/effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ std::map<std::string, effect_type> effect_types;
effect_type::effect_type() {}
effect_type::effect_type(const effect_type &) {}

std::string effect_type::get_name()
std::string effect_type::get_name() const
{
return name;
}
std::string effect_type::get_desc()
std::string effect_type::get_desc() const
{
return desc;
}
effect_rating effect_type::get_rating()
effect_rating effect_type::get_rating() const
{
return rating;
}
game_message_type effect_type::gain_game_message_type()
game_message_type effect_type::gain_game_message_type() const
{
switch(rating) {
case e_good: return m_good;
Expand All @@ -31,7 +31,7 @@ game_message_type effect_type::gain_game_message_type()
default: return m_neutral; // should never happen
}
}
game_message_type effect_type::lose_game_message_type()
game_message_type effect_type::lose_game_message_type() const
{
switch(rating) {
case e_good: return m_bad;
Expand All @@ -41,23 +41,23 @@ game_message_type effect_type::lose_game_message_type()
default: return m_neutral; // should never happen
}
}
std::string effect_type::get_apply_message()
std::string effect_type::get_apply_message() const
{
return apply_message;
}
std::string effect_type::get_apply_memorial_log()
std::string effect_type::get_apply_memorial_log() const
{
return apply_memorial_log;
}
std::string effect_type::get_remove_message()
std::string effect_type::get_remove_message() const
{
return remove_message;
}
std::string effect_type::get_remove_memorial_log()
std::string effect_type::get_remove_memorial_log() const
{
return remove_memorial_log;
}
int effect_type::get_max_intensity()
int effect_type::get_max_intensity() const
{
return max_intensity;
}
Expand Down
Loading

0 comments on commit 3bb29cf

Please sign in to comment.