<commit_before>#!/usr/bin/env luajit
--
-- Bridge between TorchCraft 1.3 (lua prototype)
--
local argparse = require("argparse")
local socket = require("socket")
local uuid = require("uuid")
require("torch")
torch.setdefaulttensortype('torch.FloatTensor')
require("sys")
local tc = require("torchcraft")
local utils = require("torchcraft.utils")
-- Debug can take values 0, 1, 2 (from no output to most verbose) 
local DEBUG = 0 
tc.DEBUG = DEBUG
-- gen random seed
uuid.randomseed(socket.gettime()*10000)
-- Spawn UUID
local spawn_uuid = uuid()
-- CLI argument parser
local parser = argparse() {
    name = "bridge",
    description = "",
    epilog = ""
}
parser:option("-t --hostname", "Give hostname / ip pointing to VM", "127.0.0.1")
parser:option("-p --port", "Port for TorchCraft", 11111)
-- Your system variables
local nrestarts = -1
-- Skip bwapi frames
local skip_frames = 7
-- Parse your arguments
local args = parser:parse()
local hostname = args['hostname']
local port = args['port'] 
-- Do your own stuff
local function get_closest(position, unitsTable)
    local min_d = 1E30
    local closest_uid = nil
    for uid, ut in pairs(unitsTable) do
        local tmp_d = utils.distance(position, ut['position'])
        if tmp_d < min_d then
            min_d = tmp_d
            closest_uid = uid
        end
    end
    return closest_uid
end
-- main system loop 
while nrestarts < 10 do
    nrestarts = nrestarts + 1
    tc:init(hostname, port)
    local frames_in_battle = 1
    local nloop = 1
    local update = tc:connect(port)
    if DEBUG > 1 then
        print('Received init: ', update)
    end
    assert(tc.state.replay == false)
    -- first message to BWAPI's side is setting up variables
    local setup = {
        tc.command(tc.set_speed, 0), tc.command(tc.set_gui, 1),
        tc.command(tc.set_cmd_optim, 1),
    }
    tc:send({table.concat(setup, ':')})

    local built_spool = 0

    local tm = torch.Timer()
    -- main battle loop
    while not tc.state.game_ended do
        -- reset timer
        tm:reset()
        -- receive update from game engine
        update = tc:receive()
        if DEBUG > 1 then
            print('Received update: ', update)
        end

        nloop = nloop + 1
        local actions = {}
        if tc.state.game_ended then
            break
        else
            if tc.state.battle_frame_count % skip_frames == 0 then
                for uid, ut in pairs(tc.state.units_myself) do
                    if tc:isbuilding(ut.type) then -- tests production
                        if ut.type == tc.unittypes.Terran_Barracks then
                            table.insert(actions,
                            tc.command(tc.command_unit, uid, tc.cmd.Train,
                            tc.unittypes.Terran_Marine))
                        end
                    elseif tc:isworker(ut.type) then
                        if tc.state.resources_myself.ore >= 150
                            and tc.state.frame_from_bwapi - built_spool > 240 then -- tests building
                            built_spool = tc.state.frame_from_bwapi
                            local _, pos = next(tc:filter_type(
                            tc.state.units_myself,<commit_msg>clean, clean, clean<commit_after>#!/usr/bin/env luajit
--
-- Bridge between TorchCraft 1.3 (lua prototype)
--
local argparse = require("argparse")
local socket = require("socket")
local uuid = require("uuid")
require("torch")
torch.setdefaulttensortype('torch.FloatTensor')
require("sys")
local tc = require("torchcraft")
local utils = require("torchcraft.utils")
-- Debug can take values 0, 1, 2 (from no output to most verbose) 
local DEBUG = 0 
tc.DEBUG = DEBUG
-- gen random seed
uuid.randomseed(socket.gettime()*10000)
-- Spawn UUID
local spawn_uuid = uuid()
-- CLI argument parser
local parser = argparse() {
    name = "bridge",
    description = "",
    epilog = ""
}
parser:option("-t --hostname", "Give hostname / ip pointing to VM", "127.0.0.1")
parser:option("-p --port", "Port for TorchCraft", 11111)
-- Your system variables
local nrestarts = -1
-- Skip bwapi frames
local skip_frames = 7
-- Parse your arguments
local args = parser:parse()
local hostname = args['hostname']
local port = args['port'] 
-- Do your things 
local function get_closest(position, unitsTable)
    local min_d = 1E30
    local closest_uid = nil
    for uid, ut in pairs(unitsTable) do
        local tmp_d = utils.distance(position, ut['position'])
        if tmp_d < min_d then
            min_d = tmp_d
            closest_uid = uid
        end
    end
    return closest_uid
end
-- main loop 
while nrestarts < 10 do
    nrestarts = nrestarts + 1
    tc:init(hostname, port)
    local frames_in_battle = 1
    local nloop = 1
    local update = tc:connect(port)
    if DEBUG > 1 then
        print('Received init: ', update)
    end
    assert(tc.state.replay == false)
    -- first message to BWAPI's side is setting up variables
    local setup = {
        tc.command(tc.set_speed, 0), tc.command(tc.set_gui, 1),
        tc.command(tc.set_cmd_optim, 1),
    }
    tc:send({table.concat(setup, ':')})

    local built_spool = 0

    local tm = torch.Timer()
    -- game loop
    while not tc.state.game_ended do
        -- reset timer
        tm:reset()
        -- receive update from game engine
        update = tc:receive()
        if DEBUG > 1 then
            print('Received update: ', update)
        end

        nloop = nloop + 1
        local actions = {}
        if tc.state.game_ended then
            break
        else
            if tc.state.battle_frame_count % skip_frames == 0 then
                for uid, ut in pairs(tc.state.units_myself) do
                    if tc:isbuilding(ut.type) then -- tests production
                        if ut.type == tc.unittypes.Terran_Barracks then
                            table.insert(actions,
                            tc.command(tc.command_unit, uid, tc.cmd.Train,
                            tc.unittypes.Terran_Marine))
                        end
                    elseif tc:isworker(ut.type) then
                        if tc.state.resources_myself.ore >= 150
                            and tc.state.frame_from_bwapi - built_spool > 240 then -- tests building
                            built_spool = tc.state.frame_from_bwapi
                            local _, pos = next(tc:filter_type(
                            tc.state.units_myself,
{'fraction_non_alphanumeric': 0.10392034847542003, 'fraction_numerical': 0.01431238332296204, 'mean_word_length': 2.0225669957686883, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3781919', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1932, 'n_words': 546}