From 0ead12c7dd68097ffbb0ab6fdc9e287f972f31a9 Mon Sep 17 00:00:00 2001 From: mdiluzio Date: Sat, 10 Jan 2015 16:55:30 +0000 Subject: [PATCH] Huge refactor, pulling server and local out into their own binaries --- source/client/CMakeLists.txt | 30 +------- source/client/README.md | 98 ------------------------ source/client/client.cpp | 8 +- source/client/client.h | 6 -- source/client/main.cpp | 34 -------- source/client/server.h | 6 -- source/local/CMakeLists.txt | 23 ++++++ source/local/local.cpp | 62 +++++++++++++++ source/server/CMakeLists.txt | 23 ++++++ source/{client => server}/server.cpp | 6 +- source/system/CMakeLists.txt | 21 +++++ source/system/error.h | 25 ++++++ source/{client => system}/filesystem.cpp | 52 +------------ source/{client => system}/filesystem.h | 0 source/{client => system}/net.cpp | 1 + source/{client => system}/net.h | 17 ---- 16 files changed, 165 insertions(+), 247 deletions(-) delete mode 100644 source/client/README.md delete mode 100644 source/client/client.h delete mode 100644 source/client/main.cpp delete mode 100644 source/client/server.h create mode 100644 source/local/CMakeLists.txt create mode 100644 source/local/local.cpp create mode 100644 source/server/CMakeLists.txt rename source/{client => server}/server.cpp (95%) create mode 100644 source/system/CMakeLists.txt create mode 100644 source/system/error.h rename source/{client => system}/filesystem.cpp (82%) rename source/{client => system}/filesystem.h (100%) rename source/{client => system}/net.cpp (99%) rename source/{client => system}/net.h (87%) diff --git a/source/client/CMakeLists.txt b/source/client/CMakeLists.txt index 7f26ed1..1ae8054 100644 --- a/source/client/CMakeLists.txt +++ b/source/client/CMakeLists.txt @@ -3,45 +3,21 @@ project( ttrts-client ) include_directories( - ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ../system ../ttrts ) # Add the sources set( SOURCES - main.cpp client.cpp - server.cpp - net.cpp - filesystem.cpp ) -# Set defaults for ttrts variables -set( TTRTS_MAPS "/usr/local/share/ttrts/maps/" ) -set( TTRTS_GAMES "/tmp/" ) -set( TTRTS_PORT 11715 ) - -# define these defaults in code -set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTTRTS_MAPS=${TTRTS_MAPS}" ) -set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTTRTS_GAMES=${TTRTS_GAMES}" ) -set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTTRTS_PORT=${TTRTS_PORT}" ) - # Add the executable add_executable( ${PROJECT_NAME} ${SOURCES} ) -# Set our output name to ttrts -set_target_properties( ${PROJECT_NAME} PROPERTIES OUTPUT_NAME ttrts ) - # dependent on main ttrts libary -target_link_libraries( ${PROJECT_NAME} ttrts pthread ) +target_link_libraries( ${PROJECT_NAME} ttrts ttrts-system ) # Installation target install( TARGETS ${PROJECT_NAME} DESTINATION bin ) - -# Run the gen_usage script to generate our usage header -add_custom_target( - ttrts-client-usage - cd ${CMAKE_CURRENT_SOURCE_DIR} && ${CMAKE_SOURCE_DIR}/scripts/gen_usage.sh "${CMAKE_CURRENT_BINARY_DIR}/usage.h" -) - -add_dependencies(${PROJECT_NAME} ttrts-client-usage) diff --git a/source/client/README.md b/source/client/README.md deleted file mode 100644 index 9e8cf27..0000000 --- a/source/client/README.md +++ /dev/null @@ -1,98 +0,0 @@ -# NAME - ttrts - Tiny Terminal RTS - -# SYNOPSIS - ttrts MAPFILE - -# DESCRIPTION - ttrts is a tiny terminal based RTS that uses text files as order lists to control the units - - This means that any user, program or cat that can read and write to text files can play the game - -# RETURN VALUE - ttrts will return -1 on error, or the winning player on completion - -# OPTIONS - MAPFILE - File to read in the initial game state. Local or in ${TTRTS_MAPS} - -# USAGE - When invoked, ttrts will set up the game in a directory within ${TTRTS_GAMES} by the name of the map - - The files in this directory can be read and interpreted by human, robot or cat - - ttrts will then await order files from each participant - - Once all order files have been received ttrts will calculate the turn and output a new gamestate file - - This process repeats until the game is over - -# ENVIRONMENT - ${TTRTS_MAPS} - Map file lookup location, defaults to `/usr/share/ttrts/maps/` - - ${TTRTS_GAMES} - Game directory for I/O, defaults to `/tmp/` - ------------------------------------------------------------ -# FILES - `/usr/share/ttrts/maps/` holds a sample set of maps - -## Gamestate File - Turn_{TURNNUMBER}.txt - -### Contents - ===== ttrts v{MAJOR}.{MINOR}.{PATCH} ===== - NAME:{GAMENAME} - SIZE:[{X},{Y}] - TURN:{TURNNUMBER} - WALL:[{X},{Y}][{X},{Y}][{X},{Y}]...{repeat for all walls} - ~~~~ - UNIT:{ID} pl:{PLAYER} vs:{VIS} dr:{DIR(NESW)} ps:[{X},{Y}] - ... {continue for all units} - END - -## Order File - Player_{PLAYER_ID}_Turn_{TURN_NUMBER}.txt - -### Contents - ORDER:{ORDER_CHAR} id:{UNIT_ID} - ... {continue for all orders} - END - ------------------------------------------------------------ -# GAMEPLAY - - The game takes place in a series of simultaneous turns on an arbitrarily sized 2D board - - Each turn, the client outputs a gamestate file and waits for an order file from each player - - All commands are evaluated simultaneously with friendly fire enabled by default - - The game is over when any of three conditions are met - - * All remaining units are controlled by a single player - * No units are left (draw) - * All units left are unable to move (draw) - -# UNITS - Each unit occupies a single tile on the board, facing in a compass direction (NESW) - - Units will only accept orders from their owner - - Units can receive only a single order each turn - - Units cannot occupy the same tile as other units/walls - -# ORDERS -### F - Move unit [F]orward one space, leaving a wall - - This wall will remain until the end of the game, blocking movement to that tile - - Movement orders have no effect if impossible, eg. - * Attempting to move outside of map - * Attempting to move on to tile occupied by unit/wall - -### L/R - Rotate unit [L]eft or [R]ight - - Unit will rotate clockwise or counter-clockwise, this order cannot fail - -### A - [A]ttack in straight line in front of unit - - Attack will continue forward until unit can't progress, all units within the path of the attack are destroyed. diff --git a/source/client/client.cpp b/source/client/client.cpp index d5de77f..116a5a0 100644 --- a/source/client/client.cpp +++ b/source/client/client.cpp @@ -1,17 +1,15 @@ -#include "client.h" - #include #include "net.h" - #include "game.h" +#include "error.h" #include "filesystem.h" -int runClient(int argc, char* argv[]) +int main(int argc, char* argv[]) { // must provide information if (argc < 2) - fatal_error("Usage: ttrts client HOST"); + fatal_error("Usage: ttrts-client HOST"); std::string hostname = argv[1]; diff --git a/source/client/client.h b/source/client/client.h deleted file mode 100644 index 201c0d9..0000000 --- a/source/client/client.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _TTRTS_CLIENT_H_ -#define _TTRTS_CLIENT_H_ - -int runClient(int argc, char* argv[]); - -#endif \ No newline at end of file diff --git a/source/client/main.cpp b/source/client/main.cpp deleted file mode 100644 index a436d6a..0000000 --- a/source/client/main.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "game.h" -#include "filesystem.h" -#include "server.h" -#include "client.h" - -#include - -static const char* sk_usage = -#include "usage.h" -; - -// Main program entry point -int main(int argc, char* argv[]) -{ - // If no args, print usage - if ( argc == 1 ) - { - std::cout< + +// ===================================================================================================================== +int main(int argc, char* argv[]) +{ + // must provide information + if (argc < 2) + fatal_error("Usage: ttrts-local MAPFILE"); + + std::string gamefile = argv[1]; + + std::cout<<"TTRTS: Launching with "< #include @@ -80,11 +80,11 @@ void RunServerForGame(CTTRTSGame &game) SendGamestateToClients(myClients, game, gameMutex); } -int runServer(int argc, char* argv[]) +int main(int argc, char* argv[]) { // argv[1] needs to be a valid game file if( argc < 2 ) - fatal_error("must provide game file argument"); + fatal_error("Usage: ttrts-server MAPFILE"); // Set up game CTTRTSGame game = GetGameFromFile(argv[1]); diff --git a/source/system/CMakeLists.txt b/source/system/CMakeLists.txt new file mode 100644 index 0000000..c17548e --- /dev/null +++ b/source/system/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 2.8.7) + +# Main ttrts library +project( ttrts-system ) + +# Include the maths +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ../ttrts +) + +# Add our sources +set( SOURCES + net.cpp + filesystem.cpp +) + +# Add this library +add_library( ${PROJECT_NAME} ${SOURCES} ) + +target_link_libraries( ${PROJECT_NAME} ttrts pthread ) diff --git a/source/system/error.h b/source/system/error.h new file mode 100644 index 0000000..2d9cde5 --- /dev/null +++ b/source/system/error.h @@ -0,0 +1,25 @@ +#ifndef _TTRTS_ERROR_H_ +#define _TTRTS_ERROR_H_ + +#include +#include +#include + +//====================================================================================================================== +// Error functions + +// For local fatal errors +inline void fatal_error(const char *msg) +{ + std::cerr< @@ -244,55 +245,4 @@ int OutputgameEnd(const CTTRTSGame &game) { } return (int)winningPlayer; -} - -// ===================================================================================================================== -int runFromFilesystem(int argc, char* argv[]) -{ - std::string gamefile = argv[1]; - - std::cout<<"TTRTS: Launching with "< diff --git a/source/client/net.h b/source/system/net.h similarity index 87% rename from source/client/net.h rename to source/system/net.h index 4d0cc33..bfcc97e 100644 --- a/source/client/net.h +++ b/source/system/net.h @@ -74,23 +74,6 @@ std::string WaitForGamestateMessage(int sockfd); // Send orders to the server int SendOrdersToServer(int sockfd, const std::string &orders); -//====================================================================================================================== -// Error functions - -// For local fatal errors -inline void fatal_error(const char *msg) -{ - std::cerr<