Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import net, json, strutils, sequtils, sugar, PlayerInput
- type Bot* = object
- name*: string
- rootsocket*: Socket
- inputs*: PlayerInputs
- type PlayerInputs* = object
- steer*: float
- throttle*: float
- roll*: float
- pitch*: float
- yaw*: float
- jump*: bool
- boost*: bool
- use_item*: bool
- chat*: int
- proc tojson(inputs: PlayerInputs): JsonNode =
- ## Quick wrapper around the generic JObject constructor.
- result = %[
- ("steer", %inputs.steer),
- ("throttle", %inputs.throttle),
- ("roll", %inputs.roll),
- ("pitch", %inputs.pitch),
- ("yaw", %inputs.yaw),
- ("jump", %inputs.jump),
- ("boost", %inputs.boost),
- ("use_item", %inputs.use_item),
- ("chat", %inputs.chat)
- ]
- return result
- proc unrollBytes(n : uint16) : string =
- let shifts : seq[uint16] = @[0'u16, 8'u16]
- # shift each byte right by a certain amount and mask off the least-significant byte
- map(shifts, shift => $char((n shr shift) and 0x000000ff)).join
- proc rollBytes(bs : string) : uint16 =
- let shifts : seq[uint16] = @[0'u16, 8'u16]
- var n : uint16
- for pair in zip(shifts, bs):
- n = n or pair.b.uint16 shl pair.a
- return n
- proc try_connect(bot: Bot, add: string = "localhost", port: Port) =
- bot.rootsocket.connect(add, port)
- echo("Bot: ", bot.name, " connected to server on address: localhost, on port: ", port)
- proc get_inputs(inputs: var PlayerInputs, game_info: JsonNode) =
- var
- steer: float = 0.0
- throttle: float = 0.0
- roll: float = 0.0
- pitch: float = 0.0
- yaw: float = 0.0
- jump: bool = false
- boost: bool = false
- use_item: bool = false
- chat: int = 0
- inputs = PlayerInputs(steer: steer,
- throttle: throttle,
- roll: roll,
- pitch: pitch,
- yaw: yaw,
- jump: jump,
- boost: boost,
- use_item: use_item,
- chat: chat)
- proc recievePacket(bot: Bot): JsonNode =
- var recieved: string = bot.rootsocket.recv(1024)
- echo(recieved)#, " | ", recieved[2..^1], " | ", rollBytes(recieved[2..^2]), " | ", recieved.toHex()[2..3], recieved.toHex()[0..1])
- #var full_length: int = len("""[{"type":"Game","frame":0,"score":[0,0],"ball":{"position":[0.0,0.0,0.0],"velocity":[0.0,0.0,0.0],"euler_angles":[0.0,0.0,0.0],"angular_velocity":[0.0,0.0,0.0],"damage":0.0,"shape":0,"radius":-1.0,"height":-1.0},"cars":[{"position":[0.0,0.0,0.0],"velocity":[0.0,0.0,0.0],"euler_angles":[0.0,0.0,0.0],"angular_velocity":[0.0,0.0,0.0],"boost":0.0,"on_ground":true,"jumped":false,"double_jumped":false,"demolished":false,"is_bot":true,"team":-1,"name":"","body_type":0,"hitbox_offset":[0.0,0.0,0.0],"hitbox_dimensions":[0.0,0.0,0.0]},{"position":[0.0,0.0,0.0],"velocity":[0.0,0.0,0.0],"euler_angles":[0.0,0.0,0.0],"angular_velocity":[0.0,0.0,0.0],"boost":0.0,"on_ground":true,"jumped":false,"double_jumped":false,"demolished":false,"is_bot":true,"team":-1,"name":"","body_type":0,"hitbox_offset":[0.0,0.0,0.0],"hitbox_dimensions":[0.0,0.0,0.0]}],"goals":[],"pads":[],"time_left":0.0,"time_elapsed":0.0,"is_overtime":true,"is_round_active":true,"is_kickoff_paused":false,"is_match_ended":false,"is_unlimited_time":false,"gravity":0.0,"map":0,"mode":0}]""")
- let header: uint16 = rollBytes(recieved[0..2])
- recieved = recieved[2..^1]
- #echo(header, ", ", len(recieved).uint16)
- #echo("reciving data... data recieved so far: ", len(recieved), " data to recieve: ", header, " collecting: ", header.int - len(recieved) - 4, " bytes")
- recieved.add(bot.rootsocket.recv(header.int - len(recieved)))#((header2.parseHexInt() - len(recieved)) - 1))
- let packet: JsonNode = json.parseJson("{\"Game\":" & recieved & "}")
- #echo("data recieved: ")
- #echo(packet)
- return packet
- var Mybot: Bot = Bot(name: "Nimbot")
- var port: Port = Port(8085)
- Mybot.rootsocket = newSocket(AF_INET, SOCK_STREAM)
- MyBot.inputs = PlayerInputs(steer: 0.0,
- throttle: 0.0,
- roll: 0.0,
- pitch: 0.0,
- yaw: 0.0,
- jump: false,
- boost: false,
- use_item: false,
- chat: 0)
- proc send_inputs(bot: Bot, game_info: JsonNode) =
- var inputs = bot.inputs
- inputs.get_inputs(game_info)
- let strinputs: string = $tojson(inputs)
- let header: string = (strinputs.len + 2).uint16.unrollBytes
- let buffer: string = header & strinputs
- echo(buffer, " | ", len(strinputs), " | ", header, " | ")
- bot.rootsocket.send(buffer)
- proc recieve_and_respond(bot: Bot) =
- let game_info: JsonNode = recievePacket(bot)
- #echo(game_info)
- if game_info["Game"][0]["is_match_ended"].getBool() == true:
- raise newException(Exception, "Match Eneded")
- else:
- bot.send_inputs(game_info)
- Mybot.try_connect(port = port)
- var i: int = 0
- while true:
- echo(i)
- Mybot.recieve_and_respond()
- inc(i)
Add Comment
Please, Sign In to add comment