diff --git a/CMakeLists.txt b/CMakeLists.txt index 97a47ba..50cc6a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ else() endif() install( FILES "${CMAKE_BINARY_DIR}/ttrts.6" DESTINATION ${MANPAGE_LOC} ) +install( FILES "${CMAKE_SOURCE_DIR}/api/lua/ttrts.lua" DESTINATION share/lua/5.1/ ) # Subprojects add_subdirectory( source ) diff --git a/README.md b/README.md index a6b299b..34e8362 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,13 @@ TTRTS is from the ground up designed to be a fun way to practice programming. An #### Requirements * CMake - our build system uses cmake * Linux/OSX - currently no support for Windows, tracked with [Issue #9](https://github.com/mdiluz/ttrts/issues/9) +* perl 5.0 or newer - for the launch script #### To Build $ git clone https://github.com/mdiluz/ttrts.git $ cd ttrts $ ./bootstrap.sh - $ ./ttrts # To launch binary and display usage + $ man ttrts # for full usage and guide ----------------------------------------------------------- ## Development @@ -80,6 +81,6 @@ TTRTS is from the ground up designed to be a fun way to practice programming. An ----------------------------------------------------------- ## Further Information -See the ttrts binary [readme](source/ttrts/README.md) for full usage and game rules +See the ttrts [README](source/README.md) for full usage and game rules See [ttrts-players](https://github.com/mdiluz/ttrts-players) for examples of AIs diff --git a/api/java/ttrts.java b/api/java/ttrts.java new file mode 100644 index 0000000..9413c37 --- /dev/null +++ b/api/java/ttrts.java @@ -0,0 +1,12 @@ + + +class ttrts +{ + + public static void main(String[] args) + { + System.out.println("I'm the ttrts Java module"); + + } + +} \ No newline at end of file diff --git a/api/lua/ttrts.lua b/api/lua/ttrts.lua new file mode 100755 index 0000000..c6b5297 --- /dev/null +++ b/api/lua/ttrts.lua @@ -0,0 +1,153 @@ +#! /usr/bin/lua + +-- need socket for server communications +require "socket" + +local ttrts = {} + +--[[ Attempt to connect to the server ]] +function ttrts.ConnectToHost(host) + + + ttrts.socket = socket.connect( host, 11715 ) + if not ttrts.socket then error("Failed to connect to " .. host .. " on port 11715") end + + -- Porform the hanshake + local line = ttrts.socket:receive('*l') + + local player, name = string.match(line, "player (%d+) name ([%a%d]+)") + + -- bail out if handshake information failed + assert( player and name , "Handshake failed with incorrect player and name") + + -- return the handshake line with a new line (removed by the *l call to receive) and string end + ttrts.socket:send( line .. "\n" .. "\0" ) + + return player, name +end + +-- [[ Recieve gamestate info ]] +function ttrts.GetStateFromHost() + + if not ttrts.socket then error("ttrts.socket must be set") end + + function newLine() + return ttrts.socket:receive('*l') + end + + local line = "" + local state = {} + + -- INVESTIGATE WHY THIS IS NEEDED + newLine() + + local thename = newLine() + state.name = string.match( thename, "NAME:([%a%d]+)" ) + assert( state.name, "Gamestate file format missmatch (no name)") + + state.size = {} + local sizes = newLine() + state.size.x, state.size.y = string.match( sizes, "SIZE:%[(%d+),(%d+)%]" ) + assert( state.size.x and state.size.y , "Gamestate file format missmatch (size failure)") + + state.turn = string.match( newLine(), "TURN:(%d+)" ) + assert( state.turn, "Gamestate file format missmatch (no turn)") + + -- Get the wall line + state.walls = {} + local wallline = newLine() + + assert(string.match( wallline, "WALL:"), "Gamestate file format missmatch (no wall line)") + + -- Grab all walls on the line + for wallstring in string.gmatch(wallline, "%[%d+,%d+%]") do + local wall = {} + wall.x, wall.y = string.match( wallstring, "%[(%d+),(%d+)%]" ) + table.insert(state.walls,wall) + end + + assert( newLine() == "~~~~", "Gamestate file format missmatch (missing ~~~~)" ) + + -- get the units + state.units = {} + + local unitline = newLine() + while unitline ~= "END" do + + local unit = {} + unit.pos = {} + + -- Parse the unit line + unit.id, unit.player, unit.vis, unit.dir, unit.pos.x, unit.pos.y = + string.match( unitline, "UNIT:(%d+) pl:(%d+) vs:([^%s]+) dr:(%a+) ps:%[(%d+),(%d+)%]") + + assert(unit.id and unit.player and unit.vis and unit.dir and unit.pos.x and unit.pos.y, "gamestate file format missmatch (error with unit)") + + table.insert(state.units,unit) + + unitline = newLine() + end + + assert(unitline == "END", "Gamestate file format missmatch (didn't end in END)") + + return state +end + +-- [[ Get Random Orders from gamestate for a particular player ]] +function ttrts.GetRandomOrders( id, state ) + + local orders = "" + local possibleorders = { "F", "L", "R", "A" } + + local units = state.units + for key,unit in pairs(units) do + if unit.id == id then + local order = "F" -- TODO MAKE RANDOM + orders = orders .. "ORDER:" .. order .. " id:" .. tostring(id) .. "\n" + end + end + + orders = orders .. "END\n" + + return orders +end + +-- [[ Send the orders to the server through the socket ]] +function ttrts.SendOrdersToHost( orders ) + assert( string.match(orders,"END"), "Cannot send orders without END" ) + ttrts.socket:send( orders .. "\0" ) +end + +-- [[ using method from http://lua-users.org/wiki/AlternativeGetOpt ]] +function ttrts.getopt( arg, options ) + local tab = {} + for k, v in ipairs(arg) do + if string.sub( v, 1, 2) == "--" then + local x = string.find( v, "=", 1, true ) + if x then tab[ string.sub( v, 3, x-1 ) ] = string.sub( v, x+1 ) + else tab[ string.sub( v, 3 ) ] = true + end + elseif string.sub( v, 1, 1 ) == "-" then + local y = 2 + local l = string.len(v) + local jopt + while ( y <= l ) do + jopt = string.sub( v, y, y ) + if string.find( options, jopt, 1, true ) then + if y < l then + tab[ jopt ] = string.sub( v, y+1 ) + y = l + else + tab[ jopt ] = arg[ k + 1 ] + end + else + tab[ jopt ] = true + end + y = y + 1 + end + end + end + return tab +end + +return ttrts \ No newline at end of file diff --git a/api/perl/ttrts.pm b/api/perl/ttrts.pm index 793269b..b22bde5 100644 --- a/api/perl/ttrts.pm +++ b/api/perl/ttrts.pm @@ -3,7 +3,7 @@ use strict; use warnings; our $ttrts_perlai_versioncompat_major = 0; -our $ttrts_perlai_versioncompat_minor = 3; +our $ttrts_perlai_versioncompat_minor = 4; our $headerDelimiter="~~~~"; @@ -303,4 +303,4 @@ sub WaitForFile } } -return 1; \ No newline at end of file +return 1; diff --git a/api/python/ttrts.py b/api/python/ttrts.py new file mode 100755 index 0000000..1a92a6d --- /dev/null +++ b/api/python/ttrts.py @@ -0,0 +1,9 @@ +#! /usr/bin/python +# python ttrts module + +# Import the python regex module +import re + +headerRegex = re.compile( + "==== ttrts v(\\d+)\\.(\\d+)\\.(\\d+)+ ====\nNAME:(.+)\nSIZE:\\[(\\d+),(\\d+)\\]\nTURN:(\\d+)\n(WALL:.*?)" + ,re.MULTILINE ) \ No newline at end of file diff --git a/api/ruby/ttrts.rb b/api/ruby/ttrts.rb new file mode 100755 index 0000000..cc8a843 --- /dev/null +++ b/api/ruby/ttrts.rb @@ -0,0 +1,5 @@ +#!/usr/bin/ruby + +module TTRTS + +end \ No newline at end of file diff --git a/source/launcher/ttrts.pl b/source/launcher/ttrts.pl index 886fed6..4e1b43a 100755 --- a/source/launcher/ttrts.pl +++ b/source/launcher/ttrts.pl @@ -41,4 +41,4 @@ $res = system("ttrts-client $host") if $client and $host; $res = system("ttrts-server $map") if $server and $map; $res = system("ttrts-local $map") if not $server and not $client and $map; -return $res \ No newline at end of file +exit $res diff --git a/source/system/net.cpp b/source/system/net.cpp index 29c23b9..c115cd8 100644 --- a/source/system/net.cpp +++ b/source/system/net.cpp @@ -108,7 +108,10 @@ void PerformServerHandshake(const ClientInfo &client, const std::string &game) // Verify handshake if ( std::string(buffer) != std::string(handshake) ) + { + std::cerr<<"Handshake was \""< #include -#define TTRTS_HANDSHAKE_FORMAT "player %u name %s" +#define TTRTS_HANDSHAKE_FORMAT "player %u name %s\n" //====================================================================================================================== // Structs for net management diff --git a/source/ttrts/formatters.cpp b/source/ttrts/formatters.cpp index 332c5ac..962f435 100644 --- a/source/ttrts/formatters.cpp +++ b/source/ttrts/formatters.cpp @@ -52,7 +52,7 @@ std::string GetStringFromGame( const CTTRTSGame& game ) state += '\n'; state += GAME_HEADER_DELIMITER; state += units; - state += "END"; + state += "END\n"; return state; }