player_status.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SuperTux
  2. // Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
  3. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "supertux/player_status.hpp"
  18. #include <sstream>
  19. #include "audio/sound_manager.hpp"
  20. #include "supertux/globals.hpp"
  21. #include "supertux/game_session.hpp"
  22. #include "util/log.hpp"
  23. #include "util/reader_mapping.hpp"
  24. #include "util/writer.hpp"
  25. static const int START_COINS = 100;
  26. static const int MAX_COINS = 9999;
  27. PlayerStatus::PlayerStatus(int num_players) :
  28. m_num_players(num_players),
  29. coins(START_COINS),
  30. bonus(num_players),
  31. max_fire_bullets(num_players),
  32. max_ice_bullets(num_players),
  33. max_air_time(num_players),
  34. max_earth_time(num_players),
  35. worldmap_sprite("images/worldmap/common/tux.sprite"),
  36. last_worldmap(),
  37. title_level()
  38. {
  39. reset(num_players);
  40. // FIXME: Move sound handling into PlayerStatusHUD
  41. if (SoundManager::current()) {
  42. SoundManager::current()->preload("sounds/coin.wav");
  43. SoundManager::current()->preload("sounds/lifeup.wav");
  44. }
  45. }
  46. void
  47. PlayerStatus::take_checkpoint_coins()
  48. {
  49. int subtract_value = std::max(coins / 10, 25);
  50. if (coins - subtract_value >= 0)
  51. coins -= subtract_value;
  52. else
  53. coins = 0;
  54. }
  55. void PlayerStatus::reset(int num_players)
  56. {
  57. coins = START_COINS;
  58. // Keep in sync with a section in read()
  59. bonus.clear();
  60. bonus.resize(num_players, NO_BONUS);
  61. max_fire_bullets.clear();
  62. max_fire_bullets.resize(num_players, 0);
  63. max_ice_bullets.clear();
  64. max_ice_bullets.resize(num_players, 0);
  65. max_air_time.clear();
  66. max_air_time.resize(num_players, 0);
  67. max_earth_time.clear();
  68. max_earth_time.resize(num_players, 0);
  69. m_num_players = num_players;
  70. }
  71. int
  72. PlayerStatus::get_max_coins() const
  73. {
  74. return MAX_COINS;
  75. }
  76. bool
  77. PlayerStatus::can_reach_checkpoint() const
  78. {
  79. return GameSession::current()->get_active_checkpoint_spawnpoint();
  80. }
  81. bool
  82. PlayerStatus::respawns_at_checkpoint() const
  83. {
  84. return GameSession::current()->get_last_spawnpoint().is_checkpoint ||
  85. GameSession::current()->reset_checkpoint_button;
  86. }
  87. void
  88. PlayerStatus::add_coins(int count, bool play_sound)
  89. {
  90. coins = std::min(coins + count, MAX_COINS);
  91. if (!play_sound)
  92. return;
  93. static float sound_played_time = 0;
  94. if (count >= 100)
  95. SoundManager::current()->play("sounds/lifeup.wav");
  96. else if (g_real_time > sound_played_time + 0.010f) {
  97. SoundManager::current()->play("sounds/coin.wav");
  98. sound_played_time = g_real_time;
  99. }
  100. }
  101. void
  102. PlayerStatus::write(Writer& writer)
  103. {
  104. writer.write("num_players", m_num_players);
  105. for (int i = 0; i < m_num_players; i++)
  106. {
  107. if (i != 0)
  108. {
  109. writer.start_list("tux" + std::to_string(i + 1));
  110. }
  111. switch (bonus[i]) {
  112. case NO_BONUS:
  113. writer.write("bonus", "none");
  114. break;
  115. case GROWUP_BONUS:
  116. writer.write("bonus", "growup");
  117. break;
  118. case FIRE_BONUS:
  119. writer.write("bonus", "fireflower");
  120. break;
  121. case ICE_BONUS:
  122. writer.write("bonus", "iceflower");
  123. break;
  124. case AIR_BONUS:
  125. writer.write("bonus", "airflower");
  126. break;
  127. case EARTH_BONUS:
  128. writer.write("bonus", "earthflower");
  129. break;
  130. default:
  131. log_warning << "Unknown bonus type." << std::endl;
  132. writer.write("bonus", "none");
  133. }
  134. writer.write("fireflowers", max_fire_bullets[i]);
  135. writer.write("iceflowers", max_ice_bullets[i]);
  136. writer.write("airflowers", max_air_time[i]);
  137. writer.write("earthflowers", max_earth_time[i]);
  138. if (i != 0)
  139. {
  140. writer.end_list("tux" + std::to_string(i + 1));
  141. }
  142. }
  143. writer.write("coins", coins);
  144. writer.write("worldmap-sprite", worldmap_sprite, false);
  145. writer.write("last-worldmap", last_worldmap, false);
  146. writer.write("title-level", title_level);
  147. }
  148. void
  149. PlayerStatus::read(const ReaderMapping& mapping)
  150. {
  151. int num_players_in_file = 1;
  152. mapping.get("num_players", num_players_in_file);
  153. reset(std::max(m_num_players, num_players_in_file));
  154. auto iter = mapping.get_iter();
  155. while (iter.next())
  156. {
  157. try
  158. {
  159. if (iter.get_key().size() > 3 && iter.get_key().substr(0, 3) == "tux")
  160. {
  161. int id = std::stoi(iter.get_key().substr(3)) - 1;
  162. if (id >= m_num_players)
  163. {
  164. log_warning << "ID larger than amount of players when reading player state: " << id << std::endl;
  165. // Keep this in sync with reset()
  166. if (bonus.size() < static_cast<size_t>(id))
  167. bonus.resize(id, NO_BONUS);
  168. if (max_fire_bullets.size() < static_cast<size_t>(id))
  169. max_fire_bullets.resize(id, 0);
  170. if (max_ice_bullets.size() < static_cast<size_t>(id))
  171. max_ice_bullets.resize(id, 0);
  172. if (max_air_time.size() < static_cast<size_t>(id))
  173. max_air_time.resize(id, 0);
  174. if (max_earth_time.size() < static_cast<size_t>(id))
  175. max_earth_time.resize(id, 0);
  176. }
  177. else if (id == 0)
  178. {
  179. log_warning << "Refusing to parse player 1 when reading player state,"
  180. "please don't put player 1 data in a (tux1 ...)"
  181. "wrapper for retrocompatibiility" << std::endl;
  182. }
  183. auto map = iter.as_mapping();
  184. parse_bonus_mapping(map, id);
  185. }
  186. }
  187. catch (const std::exception& e)
  188. {
  189. log_warning << "Couldn't parse player from player status save: " << e.what() << std::endl;
  190. }
  191. }
  192. parse_bonus_mapping(mapping, 0);
  193. mapping.get("coins", coins);
  194. mapping.get("worldmap-sprite", worldmap_sprite);
  195. mapping.get("last-worldmap", last_worldmap);
  196. mapping.get("title-level", title_level);
  197. }
  198. void
  199. PlayerStatus::parse_bonus_mapping(const ReaderMapping& map, int id)
  200. {
  201. std::string bonusname;
  202. if (map.get("bonus", bonusname)) {
  203. if (bonusname == "none") {
  204. bonus[id] = NO_BONUS;
  205. } else if (bonusname == "growup") {
  206. bonus[id] = GROWUP_BONUS;
  207. } else if (bonusname == "fireflower") {
  208. bonus[id] = FIRE_BONUS;
  209. } else if (bonusname == "iceflower") {
  210. bonus[id] = ICE_BONUS;
  211. } else if (bonusname == "airflower") {
  212. bonus[id] = AIR_BONUS;
  213. } else if (bonusname == "earthflower") {
  214. bonus[id] = EARTH_BONUS;
  215. } else {
  216. log_warning << "Unknown bonus '" << bonusname << "' in savefile for player " << (id + 1) << std::endl;
  217. bonus[id] = NO_BONUS;
  218. }
  219. }
  220. map.get("fireflowers", max_fire_bullets[id]);
  221. map.get("iceflowers", max_ice_bullets[id]);
  222. map.get("airflowers", max_air_time[id]);
  223. map.get("earthflowers", max_earth_time[id]);
  224. }
  225. std::string
  226. PlayerStatus::get_bonus_prefix(int player_id) const
  227. {
  228. switch (bonus[player_id]) {
  229. default:
  230. case NO_BONUS:
  231. return "small";
  232. case GROWUP_BONUS:
  233. return "big";
  234. case FIRE_BONUS:
  235. return "fire";
  236. case ICE_BONUS:
  237. return "ice";
  238. case AIR_BONUS:
  239. return "air";
  240. case EARTH_BONUS:
  241. return "earth";
  242. }
  243. }
  244. void
  245. PlayerStatus::add_player()
  246. {
  247. m_num_players++;
  248. bonus.resize(m_num_players, NO_BONUS);
  249. max_fire_bullets.resize(m_num_players, 0);
  250. max_ice_bullets.resize(m_num_players, 0);
  251. max_air_time.resize(m_num_players, 0);
  252. max_earth_time.resize(m_num_players, 0);
  253. }
  254. void
  255. PlayerStatus::remove_player(int player_id)
  256. {
  257. m_num_players--;
  258. for (int i = player_id; i < m_num_players; i++)
  259. {
  260. bonus[i] = bonus[i + 1];
  261. max_fire_bullets[i] = max_fire_bullets[i + 1];
  262. max_ice_bullets[i] = max_ice_bullets[i + 1];
  263. max_air_time[i] = max_air_time[i + 1];
  264. max_earth_time[i] = max_earth_time[i + 1];
  265. }
  266. bonus.resize(m_num_players, NO_BONUS);
  267. max_fire_bullets.resize(m_num_players, 0);
  268. max_ice_bullets.resize(m_num_players, 0);
  269. max_air_time.resize(m_num_players, 0);
  270. max_earth_time.resize(m_num_players, 0);
  271. }
  272. /* EOF */