added switch for developer mode and fixed crash with removing missiles

This commit is contained in:
Andreas Ortmann 2016-09-30 19:03:22 +02:00
parent b5efac002e
commit 5a4e6aaaed
6 changed files with 65 additions and 21 deletions

View file

@ -14,13 +14,10 @@ uint64_t optionsFlags;
using asio::ip::tcp; using asio::ip::tcp;
using namespace std; int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) { bool devMode = false;
char port[]="3490"; char port[]="3490";
char c;
static struct option long_options[] = static struct option long_options[] =
{ {
@ -35,13 +32,14 @@ int main(int argc, char *argv[]) {
{"autorun", required_argument, 0, 'a'}, {"autorun", required_argument, 0, 'a'},
{"port", required_argument, 0, 'p'}, {"port", required_argument, 0, 'p'},
{"fps", no_argument, 0, 'f'}, {"fps", no_argument, 0, 'f'},
{"dev", no_argument, 0, 'd'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
int option_index = 0; int option_index = 0;
while(1){ while(1){
c = getopt_long (argc, argv, "p:fa", char c = getopt_long (argc, argv, "p:fad",
long_options, &option_index); long_options, &option_index);
if (c == -1) if (c == -1)
break; break;
@ -51,6 +49,11 @@ int main(int argc, char *argv[]) {
case 'f': case 'f':
SET_FLAG(SHOW_FPS,true); SET_FLAG(SHOW_FPS,true);
break; break;
case 'd':
std::cout<<"enabling developer mode" << std::endl;
devMode = true;
break;
case 'p': case 'p':
strcpy(port,optarg); strcpy(port,optarg);
break; break;
@ -69,6 +72,7 @@ int main(int argc, char *argv[]) {
} }
Game game; Game game;
game.state()->setDeveloperMode(devMode);
asio::io_service io_service; asio::io_service io_service;
Server s(io_service, game.state(), atoi(port) ); Server s(io_service, game.state(), atoi(port) );

View file

@ -6,12 +6,37 @@
#include <iostream> #include <iostream>
using namespace std; #include <sstream>
bool Session::parse(string s) void trim(std::string &str)
{ {
s.erase(std::find_if(s.rbegin(), s.rend(), const std::string ws = " \t\r\n";
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end()); size_t endpos = str.find_last_not_of(ws);
if (endpos != std::string::npos) {
str = str.substr(0, endpos+1);
}
// trim leading spaces
size_t startpos = str.find_first_not_of(ws);
if (startpos != std::string::npos) {
str = str.substr(startpos);
}
}
bool Session::parse(std::string s)
{
// strip leading/trailing whitespace/newlines as they are always unintional
trim(s);
std::istringstream iss(s);
std::string token;
iss >> token;
// skip token + next whitespace
std::string rest = s.substr(std::min(s.size(), token.size()+1));
//std::cout<<"[session] token='" << token << "' rest='" << rest << "'" << std::endl;
// TODO: // TODO:
// from knoc: // from knoc:
@ -23,21 +48,29 @@ bool Session::parse(string s)
m_socket.close(); m_socket.close();
m_state->quitPlayer(m_pid); m_state->quitPlayer(m_pid);
return false; return false;
} else if (s.size()==1&&s[0]=='c') { } else if (s.size()==1&&s[0]=='c') {
//clear //clear
m_state->clear(m_pid); m_state->clear(m_pid);
} else if (s.size()>=3&&s[0]=='n'&&s[1]==' ') { } else if (s.size()>=3&&s[0]=='n'&&s[1]==' ') {
//set name //set name
m_state->setName(m_pid, s.substr(2)); m_state->setName(m_pid, s.substr(2));
} else if (s.size()>=3&&s[0]=='v'&&s[1]==' ') { } else if (s.size()>=3&&s[0]=='v'&&s[1]==' ') {
//set speed //set speed
double speed = atof(s.substr(2).c_str()); double speed = atof(s.substr(2).c_str());
m_state->setSpeed(m_pid, speed); m_state->setSpeed(m_pid, speed);
} else if (token == "dev") {
m_state->commandForPlayer(m_pid, new game::DeveloperCommand(rest));
} else { } else {
//shoot //shoot
double angle = atof(s.c_str()); double angle = atof(s.c_str());
m_state->commandForPlayer(m_pid, new game::ShootCommand(angle)); m_state->commandForPlayer(m_pid, new game::ShootCommand(angle));
} }
return true; return true;
} }

View file

@ -25,7 +25,7 @@ namespace endofthejedi {
return; return;
} }
std::cout<<"program: " << shader->program() << std::endl; //std::cout<<"program: " << shader->program() << std::endl;
m_shader = shader; m_shader = shader;
@ -50,9 +50,9 @@ namespace endofthejedi {
<< "') is invalid!" << std::endl; << "') is invalid!" << std::endl;
} else { } else {
std::cout<<"[polygonmodel] attribute location #" //std::cout<<"[polygonmodel] attribute location #"
<< i << " (for '" << name // << i << " (for '" << name
<< "') is " << m_attr_locations[i] << std::endl; // << "') is " << m_attr_locations[i] << std::endl;
} }
} }
} }

View file

@ -23,7 +23,7 @@ namespace game {
std::string name; std::string name;
std::list<Missile*> missiles; std::list<Missile*> missiles;
Player(int id) : id(id), alive(true), speed(0.01), energy(0.0), ship(nullptr), name("<unnamed>") Player(int id) : id(id), alive(true), speed(1.0), energy(0.0), ship(nullptr), name("<unnamed>")
{ {
} }

View file

@ -184,10 +184,10 @@ namespace game {
void State::advancePlayerMissiles(float dt) void State::advancePlayerMissiles(float dt)
{ {
std::vector<Missile*> rm;
// advance missiles // advance missiles
for (Player *player : players) { for (Player *player : players) {
std::vector<Missile*> rm;
for (Missile *missile : player->missiles) { for (Missile *missile : player->missiles) {
//std::cout<<"missile: " << (long unsigned int) missile << std::endl; //std::cout<<"missile: " << (long unsigned int) missile << std::endl;
@ -339,8 +339,6 @@ namespace game {
void State::deleteTrace(Trace *trace) void State::deleteTrace(Trace *trace)
{ {
traces.remove(trace);
if (trace->missile != nullptr) { if (trace->missile != nullptr) {
// delete backlink. // delete backlink.
// XXX: there's a missile without a trace now. // XXX: there's a missile without a trace now.
@ -373,7 +371,7 @@ namespace game {
for (Trace *trace : rm) { for (Trace *trace : rm) {
traces.remove(trace); traces.remove(trace);
delete(trace); deleteTrace(trace);
} }
} }
} }

9
test/100players..py Executable file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env python3
import os
import random
for i in range(100):
a = random.randint(0, 360)
print("spawn #%d +shoot %d" % (i, a))
os.system("echo %d | ncat 192.168.0.191 3490" % a)