diff options
author | Stijn Buys <ingar@osirion.org> | 2013-11-07 13:39:49 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2013-11-07 13:39:49 +0000 |
commit | 703822461023cf09b2e0efa4a1e7a60f47afe602 (patch) | |
tree | 469f11c4970a14cff69944b58473bf81e1321309 /src | |
parent | a98197b22f592803e5204c0263bb6bfee9a0fc24 (diff) |
Load reputation from factions.ini.
Diffstat (limited to 'src')
-rw-r--r-- | src/game/base/faction.cc | 81 | ||||
-rw-r--r-- | src/game/base/faction.h | 24 | ||||
-rw-r--r-- | src/game/base/game.cc | 12 | ||||
-rw-r--r-- | src/game/base/savegame.cc | 9 |
4 files changed, 120 insertions, 6 deletions
diff --git a/src/game/base/faction.cc b/src/game/base/faction.cc index 80dfc9f..73e5140 100644 --- a/src/game/base/faction.cc +++ b/src/game/base/faction.cc @@ -11,6 +11,7 @@ namespace game { core::InfoType *Faction::faction_infotype = 0; +Faction *Faction::faction_default = 0; void func_list_faction(const std::string &args) { @@ -48,24 +49,34 @@ bool Faction::init() con_print << "^BLoading factions..." << std::endl; + core::Func *func = core::Func::add("list_faction", func_list_faction); + func->set_info("list available factions"); + size_t count = 0; Faction *faction = 0; std::string strvalue; + float floatvalue; math::Color colorvalue; bool b = false; + std::string factionlabel; + while (inifile.getline()) { if (inifile.got_section()) { - faction = 0; - if (inifile.got_section("faction")) { count++; faction = new Faction(); + + } else if (inifile.got_section("reputation")) { + factionlabel.clear(); } else { inifile.unknown_section(); + + faction = 0; + factionlabel.clear(); } } else if (inifile.got_key()) { @@ -74,24 +85,48 @@ bool Faction::init() if (inifile.got_key_label("label", strvalue)) { faction->set_label(strvalue); + continue; } else if (inifile.got_key_string("name", strvalue)) { faction->set_name(strvalue); + continue; } else if (inifile.got_key_string("info", strvalue)) { faction->add_text(strvalue); + continue; } else if (inifile.got_key_color("color", colorvalue)) { faction->set_color(colorvalue); + continue; } else if (inifile.got_key_color("colorsecond", colorvalue)) { faction->set_color_second(colorvalue); + continue; } else if (inifile.got_key_bool("lawfull", b)) { faction->set_lawfull(b); + continue; + } else { inifile.unknown_key(); } + } else if (inifile.in_section("reputation")) { + + if (faction) { + if (inifile.got_key_label("faction", factionlabel)) { + continue; + + } else if (inifile.got_key_float("reputation", floatvalue)) { + if (factionlabel.size()) { + faction->reputation().set_reputation(factionlabel, floatvalue); + } else { + inifile.unknown_error("reputation key without faction"); + } + continue; + } else { + inifile.unknown_key(); + } + } } } } @@ -102,18 +137,51 @@ bool Faction::init() con_warn << "No factions found!" << std::endl; return true; // non-fatal } + + // create default faction + faction_default = Faction::find("default"); + if (!faction_default) { + faction_default = new Faction(); + faction_default->set_label("default"); + faction_default->set_name("Default"); + } else { + faction_default->reputation().clear(); + } + + // validate reputation + for (core::Info::Registry::iterator it = core::Info::registry().begin(); it != core::Info::registry().end(); it++) { + core::Info *info = (*it); + + if (info->type() == faction_infotype) { + faction = static_cast<Faction *>(info); + core::Reputation::FactionReps::iterator rip = faction->reputation().factionreps().begin(); + while (rip != faction->reputation().factionreps().end()) { + Faction *otherfaction = Faction::find((*rip)->label()); + + if (!otherfaction) { + con_warn << " faction '" << faction->label() << "': unknown faction '" << (*rip)->label() << "' in reputation" << std::endl; + faction->reputation().factionreps().erase(rip++); + } else { + if (otherfaction == faction_default) { + faction_default->reputation().set_reputation(faction, (*rip)->reputation()); + } + (*rip)->set_faction(otherfaction); + ++rip; + } + } + } + } con_debug << " " << inifile.name() << " " << count << " factions" << std::endl; - core::Func *func = core::Func::add("list_faction", func_list_faction); - func->set_info("list available factions"); - return true; } void Faction::done() { core::Func::remove("list_faction"); + + faction_default = 0; } Faction::Faction() : @@ -135,6 +203,9 @@ void Faction::apply(core::Entity *entity) const // set secondary color entity->set_color_second(color_second()); + + // set entity faction + entity->set_faction(this); } } // namespace game diff --git a/src/game/base/faction.h b/src/game/base/faction.h index 3775043..6f995dc 100644 --- a/src/game/base/faction.h +++ b/src/game/base/faction.h @@ -10,7 +10,9 @@ #include <list> #include "math/color.h" +#include "core/entity.h" #include "core/info.h" +#include "core/reputation.h" namespace game { @@ -34,6 +36,24 @@ public: return faction_lawfull; } + /** + * @brief faction reputation + * */ + inline core::Reputation & reputation() { + return faction_reputation; + } + + /** + * @brief player reputation with a specific faction + * */ + inline const float reputation(const Info *faction) const { + if (faction == this) { + return 100.0f; + } else { + return faction_reputation.reputation(faction); + } + } + /* --- actors ----------------------------------------------------- */ /** @@ -80,9 +100,13 @@ private: bool faction_lawfull; + core::Reputation faction_reputation; + /* --- static ----------------------------------------------------- */ static core::InfoType *faction_infotype; + + static Faction *faction_default; }; } // namespace game diff --git a/src/game/base/game.cc b/src/game/base/game.cc index f227c2f..cbcafaf 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -100,6 +100,18 @@ void Game::func_join(core::Player *player, std::string const &args) player->send("^BYou received " + aux::article(Default::shipmodel->name())); player->sound("game/buy-ship"); + + // apply default reputation + Faction *faction_default = Faction::find("default"); + if (faction_default) { + player->reputation().assign(faction_default->reputation()); + } + } + + + // DEBUG + for (core::Reputation::FactionReps::iterator rip = player->reputation().factionreps().begin(); rip != player->reputation().factionreps().end(); ++rip) { + con_debug << " reputation with " << (*rip)->label() << " " << (*rip)->reputation() << std::endl; } player->set_dirty(); diff --git a/src/game/base/savegame.cc b/src/game/base/savegame.cc index 9b9e6f6..5100064 100644 --- a/src/game/base/savegame.cc +++ b/src/game/base/savegame.cc @@ -6,6 +6,7 @@ #include <cassert> +#include "base/faction.h" #include "base/game.h" #include "base/savegame.h" #include "base/ship.h" @@ -267,7 +268,13 @@ void SaveGame::load_game(core::Player *player, filesystem::IniFile & inifile) player->set_control(ship); } - } + } + + // FIXME load reputation from savegame + Faction *faction_default = Faction::find("default"); + if (faction_default) { + player->reputation().assign(faction_default->reputation()); + } } void SaveGame::player_to_stream(core::Player *player, std::ostream & os) |