forked from Draradech/NewtonWars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwbot.py
65 lines (56 loc) · 1.36 KB
/
nwbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import socket
import struct
import math
import random
def readall(s):
timeout = s.gettimeout()
s.settimeout(1)
while True:
try:
s.recv(4096)
except socket.timeout:
break
s.settimeout(timeout)
def recvall(sock, count):
buf = b''
while count:
newbuf = sock.recv(count)
if not newbuf: return None
buf += newbuf
count -= len(newbuf)
return buf
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.2.107", 3490))
s.send("n Stupobot\n")
readall(s)
s.send("b\n")
angle = random.uniform(-180, 180)
speed = 10
mypid = None
s.send("v %f\n%f\n" % (speed, angle))
while True:
data = recvall(s, 8)
typ, pid = struct.unpack("II", data)
if typ == 1:
mypid = pid
print "My id: %d" % mypid
elif typ == 2:
print "Player %d left" % pid
s.send("r\n")
elif typ == 3:
data = recvall(s, 8)
fx, fy = struct.unpack("ff", data)
print "Player %d at %f, %f" % (pid, fx, fy)
s.send("r\n")
elif typ == 4:
data = recvall(s, 4)
n = struct.unpack("I", data)[0]
print "Player %d finished shot with %d segments" % (pid, n)
while n > 0:
n -= 1
recvall(s, 8)
if pid == mypid:
angle += 361 / 3.0
s.send("v %f\n%f\n" % (speed, angle))
else:
print "Unexpected data: %s" % repr(data)