Compare commits

...
Sign in to create a new pull request.

9 commits
master ... dev

Author SHA1 Message Date
Marc Di Luzio
0ab3da69fc Add line endings to formats.
Lua reads from sockets line by line, so this simplifies a whole lot of lua code
2015-01-25 13:17:47 +00:00
Marc Di Luzio
f17b3a17fe Add more verbose error for handshake failures 2015-01-25 13:17:14 +00:00
Marc Di Luzio
89fc5302ea Add lua ttrts api 2015-01-25 13:16:48 +00:00
Marc Di Luzio
a1e425258d exit from perl, don't return outside of subroutine 2015-01-24 11:13:28 +00:00
mdiluzio
3b5196830c Add stub files for java, python and ruby apis 2015-01-13 20:52:29 +00:00
Marc Di Luzio
b01c93a000 Update perl module to 0.4 2015-01-10 18:10:14 +00:00
Marc Di Luzio
c9fb6896d2 Update README.md 2015-01-10 17:33:25 +00:00
Marc Di Luzio
f71bd4b1d6 Update README.md 2015-01-10 17:32:36 +00:00
Marc Di Luzio
11a8873dc5 Update readme and fix unneeded ./ 2015-01-10 17:31:43 +00:00
11 changed files with 191 additions and 7 deletions

View file

@ -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 )

View file

@ -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

12
api/java/ttrts.java Normal file
View file

@ -0,0 +1,12 @@
class ttrts
{
public static void main(String[] args)
{
System.out.println("I'm the ttrts Java module");
}
}

153
api/lua/ttrts.lua Executable file
View file

@ -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

View file

@ -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;
return 1;

9
api/python/ttrts.py Executable file
View file

@ -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 )

5
api/ruby/ttrts.rb Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/ruby
module TTRTS
end

View file

@ -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
exit $res

View file

@ -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 \""<<handshake<<"\" but recieved \""<<buffer<<"\""<<std::endl;
fatal_error("Error in client handshake");
}
std::clog<<"TTRTS: Success on handshake with player "<<(int)client.player<< std::endl;
}

View file

@ -16,7 +16,7 @@
#include <stdlib.h>
#include <iostream>
#define TTRTS_HANDSHAKE_FORMAT "player %u name %s"
#define TTRTS_HANDSHAKE_FORMAT "player %u name %s\n"
//======================================================================================================================
// Structs for net management

View file

@ -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;
}