player-virtual.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Simula virtualmente um robo
  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. from numpy.linalg import norm
  9. import numpy as np
  10. rid = randint(100, 1000)
  11. run = False
  12. board = None
  13. tagplayer = -1
  14. client = TagClient('localhost', 10318)
  15. client.info(rid, 8, 8, 4, 4)
  16. px, py, course = None, None, None
  17. # Sinal de ctrl-c
  18. def signal_handler(signal, frame):
  19. client.stop()
  20. sys.exit(0)
  21. signal.signal(signal.SIGINT, signal_handler)
  22. players = {}
  23. while True:
  24. time.sleep(0.1)
  25. # nao recebeu informacoes do campo
  26. if client.getBoard() is None:
  27. continue
  28. elif px is None:
  29. board = client.getBoard()
  30. px, py = board[0]*random(), board[1]*random()
  31. course = math.pi*2.0*random()
  32. client.setPosition(px, py, course)
  33. # jogo esta pausado
  34. if client.getStatus() is False:
  35. players = {}
  36. continue
  37. # nao recebeu informacoes do pegador
  38. if client.getTagInfo() is None:
  39. continue
  40. # verifica se ha posicoes novas
  41. newposs = client.getPosition()
  42. for info in newposs:
  43. posx, posy, course, idd, xsize, ysize, xtag, ytag = info
  44. if idd != rid:
  45. players[idd] = [posx, posy]
  46. tag, interval = client.getTagInfo()
  47. pos = [players[p] for p in players]
  48. if time.time() <= interval:
  49. # comportamento de fuga
  50. if tag in players and tag != rid:
  51. # foge se nao for pegador
  52. px = px + (px-players[tag][0])/90.0
  53. py = py + (py-players[tag][1])/90.0
  54. course = course+(random()-0.5)/5.0
  55. client.setPosition(px, py, course)
  56. else:
  57. if len(pos) >= 1 and tag == rid:
  58. move = np.array([px-pos[0][0], py-pos[0][1]])
  59. move = move/norm(move)*0.5
  60. px = px - move[0]
  61. py = py - move[1]
  62. if tag != rid:
  63. px = px+(random()-0.5)
  64. py = py+(random()-0.5)
  65. course = course+(random()-0.5)/5.0
  66. client.setPosition(px, py, course)