player-lejos.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import bluetooth
  2. from robot import Robot
  3. from tag import TagClient
  4. from random import randint, random
  5. import signal, sys
  6. import math
  7. import time
  8. serverMACAddress = '00:16:53:18:2E:17'
  9. port = 1
  10. s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  11. s.connect((serverMACAddress, port))
  12. #while 1:
  13. # text = bytes([1])
  14. # if text == "quit":
  15. # break
  16. # s.send(bytes([1]))
  17. #sock.close()
  18. rid = 11
  19. run = False
  20. board = None
  21. tagplayer = -1
  22. client = TagClient('localhost', 10318)
  23. client.info(rid, 8, 8, 4, 4)
  24. px, py, course = None, None, None
  25. # Sinal de ctrl-c
  26. def signal_handler(signal, frame):
  27. client.stop()
  28. sock.close()
  29. sys.exit(0)
  30. signal.signal(signal.SIGINT, signal_handler)
  31. players = {}
  32. while not client.quit:
  33. time.sleep(0.1)
  34. # nao recebeu informacoes do campo
  35. if client.getBoard() is None:
  36. continue
  37. elif px is None:
  38. board = client.getBoard()
  39. px, py = board[0]*random(), board[1]*random()
  40. course = math.pi*2.0*random()
  41. # jogo esta pausado
  42. if client.getStatus() is False:
  43. players = {}
  44. continue
  45. # nao recebeu informacoes do pegador
  46. if client.getTagInfo() is None:
  47. continue
  48. # verifica se ha posicoes novas
  49. newposs = client.getPosition()
  50. for info in newposs:
  51. posx, posy, courser, idd, xsize, ysize, xtag, ytag = info
  52. if idd != rid:
  53. players[idd] = [posx, posy]
  54. else:
  55. px, py, course = posx, posy, courser
  56. tag, interval = client.getTagInfo()
  57. pos = [players[p] for p in players]
  58. if time.time() <= interval:
  59. # comportamento de fuga
  60. if tag in players and tag != rid:
  61. # foge se nao for pegador
  62. x1 = px-players[tag][0]
  63. y1 = py-players[tag][1]
  64. x2 = math.cos(course)
  65. y2 = math.sin(course)
  66. dot = x1*x2 + y1*y2 # dot product between [x1, y1] and [x2, y2]
  67. det = x1*y2 - y1*x2 # determinant
  68. angle = math.atan2(det, dot)
  69. if angle < 0.3 or angle > math.pi-0.3:
  70. s.send(bytes([1]))
  71. elif angle > math.pi:
  72. s.send(bytes([3]))
  73. else:
  74. s.send(bytes([4]))
  75. else:
  76. if len(pos) >= 1 and tag == rid:
  77. x1 = px-pos[0][0]
  78. y1 = py-pos[0][1]
  79. x2 = math.cos(course)
  80. y2 = math.sin(course)
  81. dot = x1*x2 + y1*y2 # dot product between [x1, y1] and [x2, y2]
  82. det = x1*y2 - y1*x2 # determinant
  83. angle = math.atan2(det, dot)
  84. if (angle < 0.3) or (angle > math.pi-0.3):
  85. s.send(bytes([1]))
  86. elif angle > math.pi:
  87. s.send(bytes([3]))
  88. else:
  89. s.send(bytes([4]))
  90. if tag != rid:
  91. x1 = px-players[tag][0]
  92. y1 = py-players[tag][1]
  93. x2 = math.cos(course)
  94. y2 = math.sin(course)
  95. dot = x1*x2 + y1*y2 # dot product between [x1, y1] and [x2, y2]
  96. det = x1*y2 - y1*x2 # determinant
  97. angle = math.atan2(det, dot)
  98. if angle < 0.3 or angle > math.pi-0.3:
  99. s.send(bytes([1]))
  100. elif angle > math.pi:
  101. s.send(bytes([3]))
  102. else:
  103. s.send(bytes([4]))