2010-04-05 23:38:59 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2010-04-18 11:41:29 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/filesystem.hpp>
|
2011-03-16 17:03:41 +01:00
|
|
|
#include <SDL/SDL_net.h>
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
#include "Model.h"
|
|
|
|
#include "Physics.h"
|
|
|
|
#include "PhysicsBase.h"
|
|
|
|
|
|
|
|
#include "EntityFactory.h"
|
2010-04-18 11:41:29 +02:00
|
|
|
#include "AsteroidsEvents.h"
|
2010-05-02 22:39:49 +02:00
|
|
|
#include "AsteroidEntity.h"
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
namespace asteroids {
|
|
|
|
|
|
|
|
static Model* ModelInstance = NULL;
|
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
Engine::Variable Model::HighscoreServerName ("highscore_server_name", "asteroids.fysx.org");
|
|
|
|
Engine::Variable Model::HighscoreServerPath ("highscore_server_path", "/highscore/highscore.php?format=raw");
|
|
|
|
Engine::Variable Model::UseServerHighscore ("use_server_highscore", "false");
|
2011-04-30 16:41:51 +02:00
|
|
|
// makes sure we have asked whether the player wants to submit the highscore data
|
|
|
|
Engine::Variable Model::UseServerHighscoreAsked ("use_server_highscore_asked", "false");
|
2011-03-16 17:03:41 +01:00
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
/*
|
|
|
|
* Inherited Module functions
|
|
|
|
*/
|
|
|
|
int Model::OnInit (int argc, char* argv[]) {
|
|
|
|
int result = Engine::ModelBase::OnInit (argc, argv);
|
|
|
|
|
2010-06-10 23:30:29 +02:00
|
|
|
Engine::LogMessage ("Model Initialization!");
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
ModelInstance = this;
|
|
|
|
|
2010-11-22 20:00:44 +01:00
|
|
|
mGameState = GameStatePaused;
|
|
|
|
mLastGameState = GameStatePaused;
|
2010-04-05 23:38:59 +02:00
|
|
|
|
2010-04-18 11:41:29 +02:00
|
|
|
/// \TODO use <optional> or similar for initialization of mCurrentLevelIndex
|
|
|
|
mCurrentLevelIndex = 99999;
|
|
|
|
|
2011-04-19 12:16:52 +02:00
|
|
|
mGameModified = false;
|
|
|
|
mCurrentLevelModified = false;
|
|
|
|
|
|
|
|
// populate the level hashes
|
|
|
|
mLevelHashes["level01_lets_get_started.map"] = "1165bac2b862118fb9c830e2f2f87c47782dded737954d81430f93adcadd3567";
|
|
|
|
mLevelHashes["level02_sing_along.map"] = "206e1e8b60a75a97de2bd91f5422ccaade43463daf5d5d4df971f0c3d9ef3f28";
|
|
|
|
mLevelHashes["level03_spacewalk.map"] = "e6c615af0b7ece9e9c826d2f431a0de080c199e2685b4f0772bb54e16894e9de";
|
|
|
|
mLevelHashes["level04_spiessrutenlauf.map"] = "4ba5c830f38e2b07b7b899d744fc4cefbd40921904492dfb396f24897a9925c0";
|
|
|
|
mLevelHashes["level05_WolleNew1_might_want.map"] = "7289454d67c74a8d5e1afe02996e786458655182355b19fe2cd1a51f4139d1da";
|
|
|
|
mLevelHashes["level06_WolleNew3_watch_your.map"] = "e5e86667623b12a491a2395a9b361617ee961edf8d349b7419942f0dbf76b769";
|
|
|
|
mLevelHashes["level07_circled.map"] = "98aa36bf2c8f6be8cd23c7c284d6ff413db992f09e5d5866e5a3aa393a93b367";
|
|
|
|
mLevelHashes["level08_highway.map"] = "bf5e678750d3f4d47c7bfec5ae62d852db1f475146ef013040d1e67308708a66";
|
2011-05-08 17:46:20 +02:00
|
|
|
mLevelHashes["level09_WolleNew4_wheres_the_brain.map"] = "94af432a967ed68389a41c2907b3500de9b6a8744816c025a7269ff2ef0509c9";
|
2011-04-19 12:16:52 +02:00
|
|
|
mLevelHashes["level10_WolleWhatever_whatever.map"] = "2f97984e86bd0969909032fa44bcf883dd181fbe61782ee7dd704b0b63dfd0e1";
|
|
|
|
mLevelHashes["level11_Strahlensatz.map"] = "3f293d4b0ea23688c15761a54b2ee097cc6a41b30750cc8fc5fccf94c20bf018";
|
2011-04-30 17:12:00 +02:00
|
|
|
mLevelHashes["level12_almost_there_test5.map"] = "5cefdc7dadbbfc30f32580f3e50cfbb6cae9ca5f432fd73e777bdbb835f3660a";
|
2011-04-19 12:16:52 +02:00
|
|
|
mLevelHashes["level13_WolleHarder1_tribute_to_lw.map"] = "1b52ccfb9f6211eb07d880cab6bf23b3ac05646561b555077bae14ad8f40c80c";
|
|
|
|
mLevelHashes["level14_asteroid_field_lets_hide.map"] = "a9e2ddca00eef71a51f564f7d7cdd482e8a4d4fc7b70d4d2bf3d5f496188fce1";
|
|
|
|
mLevelHashes["level15_WolleNew6_smack_my.map"] = "1d7e1223bbd8a68492108c2416c1a8ba828d293601c02e489dbf6b5db6a1aaa2";
|
|
|
|
mLevelHashes["level16_Wolle7_todesspirale.map"] = "d82d8f6c21806238a7b6a87c1217a0dff42f4e59e73f28e7af3f0c8afd908e38";
|
|
|
|
mLevelHashes["level17_WolleEasyNew1_twin_supernova.map"] = "4e177f82a0ba8f909ff1af1adf13caf82ab41cf09d69504c87f3e0551b9b73ed";
|
2011-05-08 17:46:20 +02:00
|
|
|
mLevelHashes["level18_WolleNew2_dont_drink.map"] = "09c32e7bc6bc3c8ef7bd96b0f60094dd46276227d653abfa3c300687d31776fd";
|
2011-04-19 12:16:52 +02:00
|
|
|
mLevelHashes["level19_Geisterfahrer.map"] = "2036741d6fd4a863324210e53b903d7a898c4478e5eb9607a43a110bb6999ace";
|
|
|
|
mLevelHashes["level20_billiard.map"] = "23dcffbf61bd887db7613b02d0c9ad04d77d800af631edbcf5822a169dcc2a9d";
|
|
|
|
mLevelHashes["level21_final.map"] = "6384b1565c0c844d7083e744d6cfbaf9de42f5ad5ecb0cb668c3a2458860c8ac";
|
|
|
|
mLevelHashes["level22_bonus.map"] = "a5c9455284fb2311687b5076693ab0df50c7766044561d79f4c46896842a6b44";
|
|
|
|
|
2010-04-18 11:41:29 +02:00
|
|
|
if (InitLevelList() == 0)
|
|
|
|
Engine::LogError ("No levels found!");
|
|
|
|
|
2010-11-13 18:45:15 +01:00
|
|
|
// Reset the newest highscore entry index which may be used for highlighting
|
|
|
|
// the newest entry.
|
2010-12-01 10:48:10 +01:00
|
|
|
mNewestHighscoreEntryIndex = std::numeric_limits<unsigned int>::max();
|
2011-03-27 22:16:19 +02:00
|
|
|
mHighscoreIsOnline = false;
|
2010-11-13 18:45:15 +01:00
|
|
|
|
2010-06-10 23:30:29 +02:00
|
|
|
// initialize event handlers and register them
|
2010-07-28 18:02:12 +02:00
|
|
|
Engine::RegisterListener (this, EventGameOver);
|
2010-11-22 20:00:44 +01:00
|
|
|
Engine::RegisterListener (this, EventShipExplode);
|
2010-06-10 23:30:29 +02:00
|
|
|
|
|
|
|
mPlayerName = "Player";
|
2010-04-05 23:38:59 +02:00
|
|
|
|
2011-02-12 15:43:06 +01:00
|
|
|
mLevelAuthor = "";
|
|
|
|
mLevelTitle = "";
|
2011-03-30 10:45:22 +02:00
|
|
|
mLevelName = "";
|
|
|
|
mLevelParTimeSeconds = -1.f;
|
2011-04-30 16:41:51 +02:00
|
|
|
mPoints = 0;
|
2011-02-12 15:43:06 +01:00
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
// initialize SDL_net to be able to retrieve highscore from the internet
|
|
|
|
if (SDLNet_Init() == -1) {
|
|
|
|
Engine::LogError("SDLNet_Init: %s\n", SDLNet_GetError());
|
|
|
|
}
|
|
|
|
|
2010-10-18 00:40:50 +02:00
|
|
|
Engine::RegisterListener (this, EventAccelerateStart);
|
|
|
|
Engine::RegisterListener (this, EventAccelerateStop);
|
2011-04-05 22:51:55 +02:00
|
|
|
Engine::RegisterListener (this, EventLevelComplete);
|
2010-10-18 00:40:50 +02:00
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
void Model::OnDestroy() {
|
|
|
|
Engine::ModelBase::OnDestroy();
|
|
|
|
mAsteroids.clear();
|
|
|
|
mLevelList.clear();
|
|
|
|
SaveLocalHighscoreList();
|
|
|
|
|
|
|
|
SDLNet_Quit();
|
|
|
|
}
|
|
|
|
|
2010-07-28 18:02:12 +02:00
|
|
|
bool Model::OnReceiveEvent (const Engine::EventBasePtr &event) {
|
|
|
|
switch (event->mEventType) {
|
2010-10-18 00:40:50 +02:00
|
|
|
case EventAccelerateStart:
|
2010-12-03 00:15:26 +01:00
|
|
|
Engine::PlaySoundLoop(Engine::GetResourceFullPath("/data/sounds/thrust.wav"), -1);
|
2010-10-18 00:40:50 +02:00
|
|
|
break;
|
|
|
|
case EventAccelerateStop:
|
2010-12-03 00:15:26 +01:00
|
|
|
Engine::HaltSoundLoop(Engine::GetResourceFullPath("/data/sounds/thrust.wav"));
|
2010-10-18 00:40:50 +02:00
|
|
|
break;
|
2010-11-22 20:00:44 +01:00
|
|
|
case EventShipExplode:
|
|
|
|
OnShipExplode();
|
|
|
|
break;
|
2011-04-05 22:51:55 +02:00
|
|
|
case EventLevelComplete:
|
|
|
|
OnLevelComplete ();
|
|
|
|
break;
|
2010-07-28 18:02:12 +02:00
|
|
|
case EventGameOver:
|
|
|
|
return OnGameOver();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: Engine::LogWarning ("Received Event with type %d but don't know what to do with it!", event->mEventType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Module specific functions
|
|
|
|
*/
|
2010-04-05 23:38:59 +02:00
|
|
|
void Model::Process () {
|
|
|
|
if (mLastGameState == mGameState) {
|
|
|
|
if (mGameState == GameStateRunning) {
|
2011-03-30 10:45:22 +02:00
|
|
|
// Only if we are still controlling the ship we increase the level
|
|
|
|
// timer
|
|
|
|
if (Engine::GetPlayerEntityId() != Engine::NullEntityId)
|
|
|
|
mLevelTimeSeconds += Engine::GetFrameDuration();
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
Engine::ModelBase::Process();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when we are here we know that something has changed so we need to take
|
|
|
|
// some action.
|
|
|
|
Engine::LogDebug ("Switching from %s->%s", GetStringGameState(mLastGameState), GetStringGameState(mGameState));
|
|
|
|
|
|
|
|
// ... and we have to set the last game state to the current gamestate
|
2010-06-10 23:30:29 +02:00
|
|
|
// otherwise we end up in an infinite loop of performing the switching
|
2010-04-05 23:38:59 +02:00
|
|
|
// action.
|
|
|
|
mLastGameState = mGameState;
|
|
|
|
}
|
|
|
|
|
2010-04-18 11:41:29 +02:00
|
|
|
unsigned int Model::InitLevelList () {
|
2011-05-04 18:33:44 +02:00
|
|
|
boost::filesystem::path level_dir_path;
|
2010-12-03 00:15:26 +01:00
|
|
|
std::string level_dir_name = Engine::GetResourceFullPath("/data/levels/");
|
|
|
|
Engine::LogDebug ("Searching for levels in %s", level_dir_name.c_str());
|
2010-04-18 11:41:29 +02:00
|
|
|
|
|
|
|
mLevelList.clear();
|
|
|
|
|
|
|
|
boost::filesystem::path level_dir(level_dir_name);
|
2011-05-04 18:33:44 +02:00
|
|
|
|
|
|
|
if (!boost::filesystem::exists(level_dir.file_string())) {
|
2011-05-06 14:31:28 +02:00
|
|
|
Engine::LogError ("Could not init level list: %s does not exist!", level_dir.filename().c_str());
|
2010-04-18 11:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!boost::filesystem::is_directory(level_dir)) {
|
2011-05-04 18:33:44 +02:00
|
|
|
Engine::LogError ("Could not init level list: %s is not a directory!", level_dir_name.c_str());
|
2010-04-18 11:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
boost::filesystem::directory_iterator end_iter;
|
|
|
|
for (boost::filesystem::directory_iterator dir_iter(level_dir);
|
|
|
|
dir_iter != end_iter;
|
|
|
|
++dir_iter) {
|
|
|
|
if (boost::filesystem::is_regular_file (dir_iter->status())) {
|
2010-05-02 22:39:49 +02:00
|
|
|
std::string level_relative_path (level_dir_name);
|
|
|
|
level_relative_path += dir_iter->path().filename();
|
2011-04-19 12:16:52 +02:00
|
|
|
|
|
|
|
// check whether we found an official level
|
2011-05-04 18:33:44 +02:00
|
|
|
std::string map_name (dir_iter->filename());
|
|
|
|
|
2011-04-19 12:16:52 +02:00
|
|
|
if (mLevelHashes.find(map_name) == mLevelHashes.end()) {
|
|
|
|
Engine::LogDebug ("Skipping unofficial level %s", std::string(level_relative_path).c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-02 22:39:49 +02:00
|
|
|
mLevelList.push_back (level_relative_path);
|
2010-04-18 11:41:29 +02:00
|
|
|
Engine::LogDebug ("Found level %s", mLevelList[mLevelList.size()-1].c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(mLevelList.begin(), mLevelList.end());
|
|
|
|
|
|
|
|
return mLevelList.size();
|
|
|
|
}
|
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
void Model::ParseHighscoreStream (std::istream &highscore_stream) {
|
|
|
|
std::string line;
|
2010-06-10 23:30:29 +02:00
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
while (getline(highscore_stream, line)) {
|
2010-06-10 23:30:29 +02:00
|
|
|
std::string name;
|
|
|
|
unsigned int points;
|
|
|
|
|
|
|
|
std::string::size_type delimiter = line.find ('\t');
|
|
|
|
if (delimiter == std::string::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
name = line.substr(0, delimiter);
|
|
|
|
|
|
|
|
std::istringstream points_stream(line.substr(delimiter + 1, line.size()));
|
|
|
|
points_stream >> points;
|
|
|
|
|
|
|
|
Engine::LogDebug ("Read Highscore Entry Name: %s Points: %d", name.c_str(), points);
|
2011-03-16 17:03:41 +01:00
|
|
|
AddLocalHighscoreEntry (name, points);
|
2010-06-10 23:30:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
void Model::LoadHighscoreList () {
|
|
|
|
mHighscoreList.clear();
|
|
|
|
|
2011-03-16 23:21:22 +01:00
|
|
|
if (mHighscoreList.size() < 10) {
|
|
|
|
AddLocalHighscoreEntry ("Imperator", 1000000);
|
2011-04-21 23:21:01 +02:00
|
|
|
AddLocalHighscoreEntry ("Luke Skywalker", 800000);
|
|
|
|
AddLocalHighscoreEntry ("Darth Vader", 600000);
|
2011-03-16 23:21:22 +01:00
|
|
|
AddLocalHighscoreEntry ("Han Solo", 400000);
|
|
|
|
AddLocalHighscoreEntry ("Princess Leia", 200000);
|
|
|
|
AddLocalHighscoreEntry ("C3PO", 100000);
|
|
|
|
AddLocalHighscoreEntry ("R2-D2", 50000);
|
|
|
|
AddLocalHighscoreEntry ("Chewy", 10000);
|
|
|
|
AddLocalHighscoreEntry ("Mr. Ewok", 5000);
|
|
|
|
AddLocalHighscoreEntry ("Jabba the Hutt", 1000);
|
|
|
|
}
|
|
|
|
|
2011-03-27 22:16:19 +02:00
|
|
|
mHighscoreIsOnline = false;
|
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
if (Model::UseServerHighscore.GetBoolValue()) {
|
2011-03-16 23:21:22 +01:00
|
|
|
Engine::LogDebug ("Retrieving Highscore from server");
|
2011-03-16 17:03:41 +01:00
|
|
|
std::stringstream global_highscore_stream;
|
|
|
|
|
|
|
|
if (PullGlobalHighscore(global_highscore_stream)) {
|
|
|
|
ParseHighscoreStream(global_highscore_stream);
|
2011-03-27 22:16:19 +02:00
|
|
|
mHighscoreIsOnline = true;
|
2011-03-16 17:03:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-03-16 23:21:22 +01:00
|
|
|
|
|
|
|
Engine::LogMessage ("Could not load highscore from server, falling back to local.");
|
2011-03-16 17:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadLocalHighscoreList();
|
2011-03-16 23:21:22 +01:00
|
|
|
SaveLocalHighscoreList();
|
2011-03-16 17:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Model::LoadLocalHighscoreList () {
|
2011-03-16 23:21:22 +01:00
|
|
|
std::string highscore_filename = Engine::GetUserDirFullPath("/highscore.dat").c_str();
|
|
|
|
boost::filesystem::path highscore_file(highscore_filename);
|
2011-03-16 17:03:41 +01:00
|
|
|
|
|
|
|
// if the file does not exist, we create it and write standard values into
|
|
|
|
// it.
|
2011-03-16 23:21:22 +01:00
|
|
|
if (!boost::filesystem::exists(highscore_file)) {
|
|
|
|
Engine::LogDebug ("Local highscore file not found!");
|
|
|
|
return;
|
|
|
|
}
|
2011-03-16 17:03:41 +01:00
|
|
|
|
|
|
|
if (!boost::filesystem::is_regular_file(highscore_file)) {
|
2011-03-16 23:21:22 +01:00
|
|
|
Engine::LogError ("Could not load highscore file: %s is not a regular file!", highscore_file.filename().c_str());
|
2011-03-16 17:03:41 +01:00
|
|
|
}
|
|
|
|
|
2011-03-16 23:21:22 +01:00
|
|
|
Engine::LogDebug ("Loading highscore file '%s'", highscore_filename.c_str());
|
2011-03-16 17:03:41 +01:00
|
|
|
|
2011-03-16 23:21:22 +01:00
|
|
|
std::ifstream score_stream (highscore_filename.c_str());
|
2011-03-16 17:03:41 +01:00
|
|
|
ParseHighscoreStream (score_stream);
|
|
|
|
|
|
|
|
score_stream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Model::SaveLocalHighscoreList () {
|
|
|
|
Engine::LogDebug ("Saving local highscore file");
|
2010-12-03 00:15:26 +01:00
|
|
|
std::ofstream highscore_file (Engine::GetUserDirFullPath("/highscore.dat").c_str());
|
2010-06-10 23:30:29 +02:00
|
|
|
|
|
|
|
std::list<HighscoreEntry>::iterator iter = mHighscoreList.begin();
|
|
|
|
|
|
|
|
while (iter != mHighscoreList.end()) {
|
|
|
|
highscore_file << iter->name << "\t" << iter->points << std::endl;
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
highscore_file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool highscore_cmp (Model::HighscoreEntry a, Model::HighscoreEntry b) {
|
|
|
|
return a.points > b.points;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Addes an entry to the highscore list and takes care that the list stays valid
|
|
|
|
*
|
|
|
|
* \TODO Re-think usage of mNewestHighscoreEntryIndex variable in this function
|
|
|
|
*/
|
2011-03-16 17:03:41 +01:00
|
|
|
unsigned int Model::AddLocalHighscoreEntry(const std::string &name, const unsigned int points) {
|
2010-06-10 23:30:29 +02:00
|
|
|
HighscoreEntry entry;
|
|
|
|
entry.name = name;
|
|
|
|
entry.points = points;
|
|
|
|
|
2011-03-16 23:21:22 +01:00
|
|
|
Engine::LogDebug ("Adding entry %s points = %d", name.c_str(), points);
|
|
|
|
|
2010-06-10 23:30:29 +02:00
|
|
|
unsigned int counter = 0;
|
|
|
|
std::list<HighscoreEntry>::iterator iter = mHighscoreList.begin();
|
|
|
|
while (iter != mHighscoreList.end()) {
|
2011-03-16 23:21:22 +01:00
|
|
|
if (name == iter->name && points == iter->points)
|
|
|
|
break;
|
|
|
|
|
2010-06-10 23:30:29 +02:00
|
|
|
if (points >= iter->points) {
|
|
|
|
mHighscoreList.insert (iter, entry);
|
|
|
|
mNewestHighscoreEntryIndex = counter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
counter ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHighscoreList.size() < 10) {
|
|
|
|
mHighscoreList.push_back(entry);
|
|
|
|
mNewestHighscoreEntryIndex = mHighscoreList.size();
|
|
|
|
return mHighscoreList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mHighscoreList.size() > 10) {
|
|
|
|
mHighscoreList.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (counter < 10)
|
|
|
|
return counter;
|
|
|
|
|
|
|
|
mNewestHighscoreEntryIndex = 99999;
|
|
|
|
|
|
|
|
return 99999;
|
|
|
|
}
|
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
bool Model::PullGlobalHighscore(std::stringstream &highscore_stream) {
|
|
|
|
highscore_stream.str("");
|
|
|
|
|
|
|
|
IPaddress server_address;
|
|
|
|
|
|
|
|
if (SDLNet_ResolveHost (&server_address, Model::HighscoreServerName.GetStringValue().c_str(), 80) == -1) {
|
2011-03-16 23:21:22 +01:00
|
|
|
Engine::LogWarning ("Could not retrieve global highscore list: host name resolution for server %s failed!",
|
|
|
|
Model::HighscoreServerName.GetStringValue().c_str());
|
2011-03-16 17:03:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char ip_address_char[4];
|
|
|
|
memcpy (&ip_address_char[0], &server_address.host, sizeof(char) * 4);
|
|
|
|
|
|
|
|
int ip_address[4];
|
|
|
|
ip_address[0] = static_cast<int>(ip_address_char[0]);
|
|
|
|
ip_address[1] = static_cast<int>(ip_address_char[1]);
|
|
|
|
ip_address[2] = static_cast<int>(ip_address_char[2]);
|
|
|
|
ip_address[3] = static_cast<int>(ip_address_char[3]);
|
|
|
|
|
2011-04-05 22:51:55 +02:00
|
|
|
if (ip_address[0] < 0)
|
|
|
|
ip_address[0] += 256;
|
|
|
|
if (ip_address[1] < 0)
|
|
|
|
ip_address[1] += 256;
|
|
|
|
if (ip_address[2] < 0)
|
|
|
|
ip_address[2] += 256;
|
|
|
|
if (ip_address[3] < 0)
|
|
|
|
ip_address[3] += 256;
|
|
|
|
|
2011-04-10 22:27:07 +02:00
|
|
|
Engine::LogDebug ("Pulling global highscore from server %s (%d.%d.%d.%d)",
|
2011-03-16 17:03:41 +01:00
|
|
|
Model::HighscoreServerName.GetStringValue().c_str(),
|
|
|
|
ip_address[0],
|
|
|
|
ip_address[1],
|
|
|
|
ip_address[2],
|
|
|
|
ip_address[3]
|
|
|
|
);
|
|
|
|
|
|
|
|
TCPsocket server_socket = SDLNet_TCP_Open (&server_address);
|
|
|
|
if (!server_socket) {
|
|
|
|
Engine::LogError ("SDL_net tcp open: %s", SDLNet_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string http_query_string;
|
|
|
|
http_query_string = std::string ("GET ") + Model::HighscoreServerPath.GetStringValue() + std::string(" HTTP/1.1\r\nHost: asteroids.fysx.org\r\nConnection: close\r\n\r\n");
|
|
|
|
|
|
|
|
int bytes_sent;
|
|
|
|
bytes_sent = SDLNet_TCP_Send (server_socket, http_query_string.c_str(), http_query_string.size());
|
|
|
|
|
|
|
|
if (bytes_sent != http_query_string.size()) {
|
|
|
|
Engine::LogError ("SDL_net tcp send: %s", SDLNet_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char receive_buffer[255];
|
|
|
|
std::string http_result;
|
|
|
|
|
|
|
|
bool receiving = true;
|
|
|
|
while (receiving) {
|
|
|
|
// reset the buffer
|
|
|
|
bzero (&receive_buffer[0], 255);
|
|
|
|
|
|
|
|
// read the data
|
|
|
|
int received_bytes = SDLNet_TCP_Recv (server_socket, receive_buffer, 255);
|
|
|
|
|
|
|
|
if (received_bytes <= 0) {
|
|
|
|
receiving = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_result.append (receive_buffer, received_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have to strip the whitespaces twice to cut out the content
|
|
|
|
http_result = strip_whitespaces (http_result);
|
|
|
|
http_result = http_result.substr (http_result.find ("\n\r"), http_result.size());
|
|
|
|
http_result = strip_whitespaces (http_result);
|
|
|
|
|
|
|
|
Engine::LogDebug ("Received Highscore: %s", http_result.c_str());
|
|
|
|
|
|
|
|
SDLNet_TCP_Close (server_socket);
|
|
|
|
|
|
|
|
highscore_stream.str(http_result);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-10 22:27:07 +02:00
|
|
|
bool Model::SubmitGlobalHigscoreEntry (std::string name, const unsigned int points) {
|
2011-03-16 17:03:41 +01:00
|
|
|
IPaddress server_address;
|
|
|
|
|
|
|
|
if (SDLNet_ResolveHost (&server_address, Model::HighscoreServerName.GetStringValue().c_str(), 80) == -1) {
|
2011-03-16 23:21:22 +01:00
|
|
|
Engine::LogWarning ("Cannot submit highscore entry: host name resolution for server %s failed!",
|
|
|
|
Model::HighscoreServerName.GetStringValue().c_str());
|
2011-03-16 17:03:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char ip_address_char[4];
|
|
|
|
memcpy (&ip_address_char[0], &server_address.host, sizeof(char) * 4);
|
|
|
|
|
|
|
|
int ip_address[4];
|
|
|
|
ip_address[0] = static_cast<int>(ip_address_char[0]);
|
|
|
|
ip_address[1] = static_cast<int>(ip_address_char[1]);
|
|
|
|
ip_address[2] = static_cast<int>(ip_address_char[2]);
|
|
|
|
ip_address[3] = static_cast<int>(ip_address_char[3]);
|
|
|
|
|
2011-04-05 22:51:55 +02:00
|
|
|
if (ip_address[0] < 0)
|
|
|
|
ip_address[0] += 256;
|
|
|
|
if (ip_address[1] < 0)
|
|
|
|
ip_address[1] += 256;
|
|
|
|
if (ip_address[2] < 0)
|
|
|
|
ip_address[2] += 256;
|
|
|
|
if (ip_address[3] < 0)
|
|
|
|
ip_address[3] += 256;
|
|
|
|
|
2011-04-10 22:27:07 +02:00
|
|
|
std::string escaped_name = name;
|
|
|
|
|
|
|
|
while (escaped_name.find(" ") != std::string::npos) {
|
|
|
|
escaped_name.replace (escaped_name.find(" "), 1, "%20");
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine::LogDebug ("Escaped name (%u) = '%s'", escaped_name.size(), escaped_name.c_str());
|
2011-04-05 22:51:55 +02:00
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
Engine::LogDebug ("Submitting highscore player_name='%s' score_value='%d' to server %s (%d.%d.%d.%d)",
|
|
|
|
name.c_str(),
|
|
|
|
points,
|
|
|
|
Model::HighscoreServerName.GetStringValue().c_str(),
|
|
|
|
ip_address[0],
|
|
|
|
ip_address[1],
|
|
|
|
ip_address[2],
|
|
|
|
ip_address[3]
|
|
|
|
);
|
|
|
|
|
|
|
|
TCPsocket server_socket = SDLNet_TCP_Open (&server_address);
|
|
|
|
if (!server_socket) {
|
|
|
|
Engine::LogError ("SDL_net tcp open: %s", SDLNet_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream points_stringstream;
|
|
|
|
points_stringstream << points;
|
|
|
|
|
|
|
|
std::stringstream hash_input;
|
|
|
|
hash_input << name << ":" << points << ":" << sha256_hash ("asteroids rule");
|
|
|
|
|
|
|
|
std::string key = sha256_hash (hash_input.str());
|
|
|
|
|
|
|
|
std::string http_query_string;
|
|
|
|
http_query_string = std::string ("GET ")
|
|
|
|
+ Model::HighscoreServerPath.GetStringValue()
|
2011-04-10 22:27:07 +02:00
|
|
|
+ std::string("&player_name=") + escaped_name
|
2011-03-16 17:03:41 +01:00
|
|
|
+ std::string("&score_value=") + points_stringstream.str()
|
|
|
|
+ std::string("&key=") + key
|
|
|
|
+ std::string(" HTTP/1.1\r\nHost: asteroids.fysx.org\r\nConnection: close\r\n\r\n");
|
|
|
|
|
2011-04-10 22:27:07 +02:00
|
|
|
Engine::LogDebug ("http submitting query: %s", http_query_string.c_str());
|
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
int bytes_sent;
|
|
|
|
bytes_sent = SDLNet_TCP_Send (server_socket, http_query_string.c_str(), http_query_string.size());
|
|
|
|
|
|
|
|
if (bytes_sent != http_query_string.size()) {
|
|
|
|
Engine::LogError ("SDL_net tcp send: %s", SDLNet_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char receive_buffer[255];
|
|
|
|
std::string http_result;
|
|
|
|
|
|
|
|
bool receiving = true;
|
|
|
|
while (receiving) {
|
|
|
|
// reset the buffer
|
|
|
|
bzero (&receive_buffer[0], 255);
|
|
|
|
|
|
|
|
// read the data
|
|
|
|
int received_bytes = SDLNet_TCP_Recv (server_socket, receive_buffer, 255);
|
|
|
|
|
|
|
|
if (received_bytes <= 0) {
|
|
|
|
receiving = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_result.append (receive_buffer, received_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have to strip the whitespaces twice to cut out the content
|
|
|
|
http_result = strip_whitespaces (http_result);
|
|
|
|
http_result = http_result.substr (http_result.find ("\n\r"), http_result.size());
|
|
|
|
http_result = strip_whitespaces (http_result);
|
|
|
|
|
|
|
|
SDLNet_TCP_Close (server_socket);
|
|
|
|
|
|
|
|
if (http_result == "OK") {
|
|
|
|
Engine::LogDebug ("Submission successful: %s", http_result.c_str());
|
|
|
|
LoadHighscoreList();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Engine::LogMessage ("Submission unsuccessful: %s", http_result.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Model::SubmitHighscoreEntry (const std::string &name, const unsigned int points) {
|
|
|
|
if (Model::UseServerHighscore.GetBoolValue()) {
|
2011-03-16 23:21:22 +01:00
|
|
|
if (SubmitGlobalHigscoreEntry (name, points)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-03-16 17:03:41 +01:00
|
|
|
|
2011-03-16 23:21:22 +01:00
|
|
|
Engine::LogWarning ("Could not send highscore entry to server!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it did not succeed or we do not want to use the global highscore
|
|
|
|
// fall back to the local highscore system.
|
|
|
|
AddLocalHighscoreEntry (name, points);
|
|
|
|
SaveLocalHighscoreList();
|
2011-03-16 17:03:41 +01:00
|
|
|
}
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
int Model::DoLoadLevel (const char* filename) {
|
2011-04-19 12:16:52 +02:00
|
|
|
// verify the hash of the map
|
|
|
|
std::string map_name (filename);
|
2011-05-04 18:33:44 +02:00
|
|
|
map_name = boost::filesystem::path(map_name).filename();
|
2011-04-19 12:16:52 +02:00
|
|
|
std::string map_hash = sha256_hash_file (filename);
|
|
|
|
if (map_hash != mLevelHashes[map_name]) {
|
|
|
|
Engine::LogMessage ("Map verification for file %s failed!", map_name.c_str());
|
|
|
|
mGameModified = true;
|
|
|
|
mCurrentLevelModified = true;
|
|
|
|
}
|
2011-05-03 16:10:23 +02:00
|
|
|
Engine::LogDebug ("Verification for level %s OK: loading level.", map_name.c_str());
|
2011-04-19 12:16:52 +02:00
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
std::fstream level_file (filename, std::ios::in);
|
|
|
|
|
|
|
|
if (!level_file) {
|
2011-01-02 21:04:11 +01:00
|
|
|
Engine::LogError ("Unable to open file %s for reading!", filename);
|
2010-04-05 23:38:59 +02:00
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearEntities();
|
|
|
|
mAsteroids.clear();
|
|
|
|
|
|
|
|
std::string entity_type_str;
|
|
|
|
|
|
|
|
int entity_count = 0;
|
2011-02-12 16:34:47 +01:00
|
|
|
SetLevelTitle ("");
|
|
|
|
SetLevelAuthor ("");
|
2011-03-30 10:45:22 +02:00
|
|
|
mLevelTimeSeconds = 0.f;
|
|
|
|
mLevelParTimeSeconds = -1.f;
|
|
|
|
mLevelPoints = 0;
|
2011-04-05 22:51:55 +02:00
|
|
|
mLevelTimeBonusPoints = 0;
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
while (level_file >> entity_type_str) {
|
|
|
|
if (entity_type_str[0] == '#') {
|
|
|
|
getline (level_file, entity_type_str);
|
2010-04-06 00:12:26 +02:00
|
|
|
Engine::LogDebug ("Read Comment: %s", entity_type_str.c_str());
|
2010-04-05 23:38:59 +02:00
|
|
|
continue;
|
2011-02-12 15:43:06 +01:00
|
|
|
} else if (entity_type_str == "Title") {
|
|
|
|
std::string level_title;
|
|
|
|
|
|
|
|
getline (level_file, level_title);
|
|
|
|
level_title = strip_whitespaces (level_title);
|
|
|
|
SetLevelTitle (level_title);
|
|
|
|
continue;
|
|
|
|
} else if (entity_type_str == "Author") {
|
|
|
|
std::string level_author;
|
|
|
|
|
|
|
|
getline (level_file, level_author);
|
|
|
|
level_author = strip_whitespaces (level_author);
|
|
|
|
SetLevelAuthor (level_author);
|
|
|
|
continue;
|
2011-03-30 10:45:22 +02:00
|
|
|
} else if (entity_type_str == "ParTime") {
|
|
|
|
std::string partime_string;
|
|
|
|
|
|
|
|
getline (level_file, partime_string);
|
|
|
|
partime_string = strip_whitespaces (partime_string);
|
|
|
|
std::stringstream partime_stream (partime_string);
|
|
|
|
|
|
|
|
float partime = -1.;
|
|
|
|
partime_stream >> partime;
|
|
|
|
|
|
|
|
if (partime > 0.)
|
|
|
|
mLevelParTimeSeconds = partime;
|
|
|
|
|
|
|
|
continue;
|
2010-04-05 23:38:59 +02:00
|
|
|
}
|
|
|
|
|
2011-03-30 10:45:22 +02:00
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
GameEntityType entity_type = GameEntityTypeUnknown;
|
|
|
|
|
|
|
|
if (entity_type_str == "GameEntityTypeShip")
|
|
|
|
entity_type = GameEntityTypeShip;
|
|
|
|
else if (entity_type_str == "GameEntityTypeAsteroid")
|
|
|
|
entity_type = GameEntityTypeAsteroid;
|
|
|
|
else {
|
|
|
|
Engine::LogError ("Unknown Entity type: %s", entity_type_str.c_str());
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine::EntityBase* entity = CreateEntity (entity_type);
|
|
|
|
|
|
|
|
bool is_player;
|
|
|
|
level_file >> is_player;
|
|
|
|
|
2010-11-27 20:56:38 +01:00
|
|
|
if (is_player) {
|
2010-04-05 23:38:59 +02:00
|
|
|
mPlayerEntityId = entity->mId;
|
2010-11-27 20:56:38 +01:00
|
|
|
Engine::LogDebug ("Entity with id '%d' is player", entity->mId);
|
|
|
|
}
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
level_file >> entity->mPhysicState->mPosition[0];
|
|
|
|
level_file >> entity->mPhysicState->mPosition[1];
|
|
|
|
level_file >> entity->mPhysicState->mPosition[2];
|
|
|
|
|
|
|
|
level_file >> entity->mPhysicState->mOrientation[0];
|
|
|
|
level_file >> entity->mPhysicState->mOrientation[1];
|
|
|
|
level_file >> entity->mPhysicState->mOrientation[2];
|
|
|
|
|
|
|
|
level_file >> entity->mPhysicState->mVelocity[0];
|
|
|
|
level_file >> entity->mPhysicState->mVelocity[1];
|
|
|
|
level_file >> entity->mPhysicState->mVelocity[2];
|
|
|
|
|
|
|
|
level_file >> entity->mPhysicState->mAngleVelocity;
|
|
|
|
|
|
|
|
entity_count ++;
|
|
|
|
}
|
|
|
|
|
2011-03-30 10:45:22 +02:00
|
|
|
if (mLevelParTimeSeconds == -1.f) {
|
|
|
|
Engine::LogWarning ("Level %s has no par time set. Setting to 120s.", filename);
|
|
|
|
mLevelParTimeSeconds = 120.f;
|
|
|
|
}
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
level_file.close();
|
|
|
|
|
|
|
|
Engine::LogDebug ("%d Entities loaded!", mEntities.size());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Model::DoSaveLevel (const char* filename) {
|
|
|
|
Engine::LogMessage ("Saving level to %s", filename);
|
|
|
|
std::fstream level_file (filename, std::ios::out);
|
|
|
|
|
|
|
|
if (!level_file) {
|
|
|
|
Engine::LogError ("Unable to open file %s for writing!", filename);
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
level_file << "# Format" << std::endl;
|
|
|
|
level_file << "# <Type> <player?> <xpos> <ypos> <zpos> <zrot> <yrot> <xrot> <xvel> <yvel> <zvel> <rotvel>" << std::endl;
|
|
|
|
|
2011-02-12 15:43:06 +01:00
|
|
|
if (GetLevelTitle() != "") {
|
|
|
|
level_file << "Title " << mLevelTitle << std::endl;
|
|
|
|
}
|
|
|
|
if (GetLevelAuthor() != "") {
|
|
|
|
level_file << "Author " << mLevelAuthor << std::endl;
|
|
|
|
}
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
std::map<unsigned int, Engine::EntityBase*>::iterator iter = mEntities.begin();
|
|
|
|
unsigned int player_id = GetPlayerEntityId();
|
|
|
|
|
|
|
|
for (iter = mEntities.begin(); iter != mEntities.end(); iter++) {
|
|
|
|
Engine::EntityBase* game_entity = iter->second;
|
|
|
|
|
|
|
|
level_file << GetStringGameEntityType((GameEntityType)game_entity->mType) << "\t"
|
|
|
|
// this stores the player id
|
|
|
|
<< (game_entity->mId == player_id) << "\t"
|
|
|
|
<< game_entity->mPhysicState->mPosition[0] << "\t"
|
|
|
|
<< game_entity->mPhysicState->mPosition[1] << "\t"
|
|
|
|
<< game_entity->mPhysicState->mPosition[2] << "\t"
|
|
|
|
<< game_entity->mPhysicState->mOrientation[0] << "\t"
|
|
|
|
<< game_entity->mPhysicState->mOrientation[1] << "\t"
|
|
|
|
<< game_entity->mPhysicState->mOrientation[2] << "\t"
|
|
|
|
<< game_entity->mPhysicState->mVelocity[0] << "\t"
|
|
|
|
<< game_entity->mPhysicState->mVelocity[1] << "\t"
|
|
|
|
<< game_entity->mPhysicState->mVelocity[2] << "\t"
|
|
|
|
<< game_entity->mPhysicState->mAngleVelocity << "\t"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
level_file.close();
|
|
|
|
return 0;
|
|
|
|
}
|
2010-09-11 16:09:15 +02:00
|
|
|
|
2011-03-23 08:13:27 +01:00
|
|
|
void Model::LoadLevelFromIndex (unsigned int level_index) {
|
|
|
|
if (level_index < mLevelList.size()) {
|
|
|
|
mCurrentLevelIndex = level_index;
|
|
|
|
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-28 00:20:58 +01:00
|
|
|
void Model::ReloadLevel () {
|
|
|
|
Engine::LogDebug ("Reloading level %d", mCurrentLevelIndex + 1);
|
|
|
|
|
|
|
|
if (mCurrentLevelIndex < 0 || mCurrentLevelIndex >= mLevelList.size())
|
|
|
|
Engine::LogError ("Invalid level index: %u", mCurrentLevelIndex);
|
|
|
|
|
|
|
|
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
|
|
|
|
}
|
|
|
|
|
2010-11-27 20:56:38 +01:00
|
|
|
void Model::ProceedToNextLevel () {
|
|
|
|
Engine::LogDebug ("Proceeding to next level %d", mCurrentLevelIndex + 1);
|
|
|
|
mCurrentLevelIndex++;
|
|
|
|
|
2010-11-29 10:15:08 +01:00
|
|
|
if (mCurrentLevelIndex == mLevelList.size()) {
|
2010-11-27 20:56:38 +01:00
|
|
|
Engine::EventBasePtr gameover_event (new Engine::EventBase());
|
|
|
|
gameover_event->mEventType = EventGameOver;
|
|
|
|
QueueEvent (gameover_event);
|
|
|
|
} else {
|
|
|
|
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-11 02:28:50 +02:00
|
|
|
void Model::SetGameState (const unsigned int &state) {
|
|
|
|
mLastGameState = mGameState;
|
|
|
|
mGameState = state;
|
|
|
|
}
|
2010-04-05 23:38:59 +02:00
|
|
|
|
2010-06-10 23:30:29 +02:00
|
|
|
bool Model::OnGameOver() {
|
2011-03-16 17:03:41 +01:00
|
|
|
Engine::LogDebug ("Points = %d lowest = %d", mPoints, mHighscoreList.back().points );
|
2010-11-22 20:00:44 +01:00
|
|
|
|
2010-06-10 23:30:29 +02:00
|
|
|
if (mPoints > mHighscoreList.back().points) {
|
|
|
|
Engine::LogMessage ("New Highscore!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
void Model::OnNewGame() {
|
2010-11-27 08:38:32 +01:00
|
|
|
ClearEntities();
|
|
|
|
|
2010-12-01 10:33:44 +01:00
|
|
|
mNewestHighscoreEntryIndex = std::numeric_limits<unsigned int>::max();
|
|
|
|
mPlayerLives = 3;
|
2010-06-10 23:30:29 +02:00
|
|
|
mCurrentLevelIndex = 0;
|
|
|
|
mPoints = 0;
|
2011-03-30 10:45:22 +02:00
|
|
|
mLevelPoints = 0;
|
2010-11-27 08:38:32 +01:00
|
|
|
|
2010-06-10 23:30:29 +02:00
|
|
|
DoLoadLevel (mLevelList[mCurrentLevelIndex].c_str());
|
2010-04-18 11:41:29 +02:00
|
|
|
}
|
|
|
|
|
2010-11-22 20:00:44 +01:00
|
|
|
void Model::OnShipExplode () {
|
2011-03-16 17:03:41 +01:00
|
|
|
Engine::PlaySound(Engine::GetResourceFullPath("/data/sounds/ship_destroyed.wav"));
|
2011-02-13 15:31:42 +01:00
|
|
|
|
2011-04-10 22:27:07 +02:00
|
|
|
if (GetPlayerEntityId() == Engine::NullEntityId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-22 20:00:44 +01:00
|
|
|
mPlayerLives --;
|
|
|
|
|
|
|
|
if (mPlayerLives == 0) {
|
2010-11-28 00:20:58 +01:00
|
|
|
Engine::EventBasePtr gameover_event (new Engine::EventBase());
|
2010-11-22 20:00:44 +01:00
|
|
|
gameover_event->mEventType = EventGameOver;
|
|
|
|
QueueEvent (gameover_event);
|
2010-11-28 00:20:58 +01:00
|
|
|
} else {
|
|
|
|
Engine::EventBasePtr playerdied_event (new Engine::EventBase());
|
|
|
|
playerdied_event->mEventType = EventPlayerDied;
|
|
|
|
QueueEvent (playerdied_event);
|
2010-11-22 20:00:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-05 22:51:55 +02:00
|
|
|
void Model::OnLevelComplete() {
|
|
|
|
// calculate the bonus points
|
|
|
|
float level_time = roundf(mLevelTimeSeconds);
|
|
|
|
float level_par_time = roundf(mLevelParTimeSeconds);
|
|
|
|
int bonus_points = 0;
|
|
|
|
|
|
|
|
if (level_time <= level_par_time) {
|
|
|
|
// secret time bonus formula
|
2011-04-10 22:27:07 +02:00
|
|
|
float a = 2,
|
2011-04-05 22:51:55 +02:00
|
|
|
b = -4.2,
|
2011-04-10 22:27:07 +02:00
|
|
|
c = 2.2;
|
2011-04-05 22:51:55 +02:00
|
|
|
float t_n = mLevelTimeSeconds / mLevelParTimeSeconds;
|
|
|
|
float bonus_points_f = roundf ( (a * t_n * t_n + b * t_n + c) * static_cast<float>( mLevelPoints ));
|
|
|
|
if (bonus_points_f < 0.)
|
|
|
|
bonus_points_f = 0.;
|
|
|
|
|
2011-04-10 22:27:07 +02:00
|
|
|
// round to the closest decimal number
|
2011-04-05 22:51:55 +02:00
|
|
|
bonus_points_f = roundf (bonus_points_f / 10.) * 10. + 100.;
|
|
|
|
mLevelTimeBonusPoints = static_cast<unsigned int> (bonus_points_f);
|
|
|
|
|
2011-04-10 22:27:07 +02:00
|
|
|
Engine::LogDebug("Bonus Points: t_n = %f bonus_points_f = %f bonus_points = %u", t_n, bonus_points_f, mLevelTimeBonusPoints);
|
2011-04-05 22:51:55 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
mLevelTimeBonusPoints = 0;
|
|
|
|
}
|
|
|
|
|
2011-04-19 13:20:46 +02:00
|
|
|
int extra_lives = floor ((mPoints + mLevelPoints) / 400000.f) - floor (mPoints / 400000.f);
|
2011-04-19 10:58:13 +02:00
|
|
|
if (extra_lives > 0) {
|
|
|
|
mPlayerLives += extra_lives;
|
|
|
|
}
|
|
|
|
|
2011-04-05 22:51:55 +02:00
|
|
|
mLevelPoints += mLevelTimeBonusPoints;
|
|
|
|
mPoints += mLevelPoints;
|
2011-04-19 12:16:52 +02:00
|
|
|
|
|
|
|
if (mGameModified)
|
|
|
|
mPoints = 0;
|
2011-04-05 22:51:55 +02:00
|
|
|
}
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
void Model::OnCreateEntity (const int type, const unsigned int id) {
|
|
|
|
GameEntityType entity_type = (GameEntityType) type;
|
|
|
|
|
|
|
|
if (entity_type == GameEntityTypeAsteroid) {
|
|
|
|
mAsteroids.push_back (id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Model::OnKillEntity (const Engine::EntityBase *entity) {
|
|
|
|
GameEntityType entity_type = (GameEntityType) entity->mType;
|
|
|
|
|
|
|
|
if (entity_type == GameEntityTypeAsteroid) {
|
2010-12-03 00:15:26 +01:00
|
|
|
Engine::PlaySound(Engine::GetResourceFullPath("/data/sounds/rock_destroyed.wav"));
|
2010-10-18 00:40:50 +02:00
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
unsigned int i;
|
2010-05-02 22:39:49 +02:00
|
|
|
const AsteroidEntity *asteroid = static_cast<const AsteroidEntity*>(entity);
|
2010-11-28 00:20:58 +01:00
|
|
|
|
2011-03-30 10:45:22 +02:00
|
|
|
if (GetPlayerEntityId() != Engine::NullEntityId) {
|
2011-04-19 10:38:59 +02:00
|
|
|
mLevelPoints += (150 + asteroid->mSubAsteroidsCount * 75) * 6;
|
2011-03-30 10:45:22 +02:00
|
|
|
}
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
for (i = 0; i < mAsteroids.size(); i++) {
|
|
|
|
if (mAsteroids.at(i) == entity->mId) {
|
|
|
|
std::vector<unsigned int>::iterator entity_iter = mAsteroids.begin() + i;
|
|
|
|
mAsteroids.erase (entity_iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAsteroids.size() == 0) {
|
2010-04-18 11:41:29 +02:00
|
|
|
Engine::EventBasePtr level_complete_event (new Engine::EventBase());
|
|
|
|
level_complete_event->mEventType = EventLevelComplete;
|
2011-04-05 22:51:55 +02:00
|
|
|
|
2010-11-27 20:56:38 +01:00
|
|
|
QueueEvent (level_complete_event);
|
2010-04-05 23:38:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float Model::GetWorldWidth () {
|
|
|
|
return static_cast<Physics*>(mPhysics)->GetWorldWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Model::GetWorldHeight () {
|
|
|
|
return static_cast<Physics*>(mPhysics)->GetWorldHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|