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");
|
|
|
|
|
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-01-02 18:25:20 +01:00
|
|
|
mLevelName = "";
|
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();
|
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-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);
|
|
|
|
|
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;
|
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) {
|
|
|
|
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 () {
|
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);
|
|
|
|
|
|
|
|
if (!boost::filesystem::exists(level_dir)) {
|
2010-12-03 00:15:26 +01:00
|
|
|
Engine::LogError ("Could not init level list: \todo %s does not exist!");
|
2010-04-18 11:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!boost::filesystem::is_directory(level_dir)) {
|
2010-12-03 00:15:26 +01:00
|
|
|
Engine::LogError ("Could not init level list: \todo %s is not a directory!");
|
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();
|
|
|
|
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);
|
|
|
|
AddLocalHighscoreEntry ("Darth Vader", 800000);
|
|
|
|
AddLocalHighscoreEntry ("Luke Skywalker", 600000);
|
|
|
|
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-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);
|
|
|
|
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]);
|
|
|
|
|
|
|
|
Engine::LogMessage ("Pulling global highscore from server %s (%d.%d.%d.%d)",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Model::SubmitGlobalHigscoreEntry (const std::string &name, const unsigned int points) {
|
|
|
|
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]);
|
|
|
|
|
|
|
|
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()
|
|
|
|
+ std::string("&player_name=") + name
|
|
|
|
+ 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");
|
|
|
|
|
|
|
|
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) {
|
|
|
|
Engine::LogMessage ("Loading level from %s", filename);
|
|
|
|
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 ("");
|
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;
|
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 ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
SubmitHighscoreEntry (mPlayerName, mPoints);
|
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;
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if (GetPlayerEntityId() != Engine::NullEntityId)
|
|
|
|
mPoints += 150 + asteroid->mSubAsteroidsCount * 75;
|
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;
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|