No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

robot_test.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package server
  2. import (
  3. "log"
  4. "math"
  5. "testing"
  6. v "hackerbots.us/vector"
  7. )
  8. func init() {
  9. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  10. }
  11. func TestCollision(t *testing.T) {
  12. tests := []struct {
  13. g Game
  14. r Robot
  15. target v.Vector2d
  16. location *v.Point2d
  17. collision bool
  18. }{
  19. // should intersect with first box
  20. {
  21. g: Game{
  22. width: 800,
  23. height: 400,
  24. obstacles: []Obstacle{
  25. Obstacle{
  26. Bounds: v.AABB2d{
  27. A: v.Point2d{X: 200, Y: 100},
  28. B: v.Point2d{X: 300, Y: 200},
  29. },
  30. },
  31. Obstacle{
  32. Bounds: v.AABB2d{
  33. A: v.Point2d{X: 400, Y: 200},
  34. B: v.Point2d{X: 600, Y: 300},
  35. },
  36. },
  37. },
  38. },
  39. r: Robot{
  40. Position: v.Point2d{X: 100, Y: 100},
  41. // Heading: v.Vector2d{1, 1},
  42. },
  43. target: v.Vector2d{X: 900, Y: 350},
  44. location: &v.Point2d{X: 200, Y: 138.88889},
  45. collision: true,
  46. },
  47. // shouldn't intersect at all
  48. {
  49. g: Game{
  50. width: 800,
  51. height: 400,
  52. obstacles: []Obstacle{
  53. Obstacle{
  54. Bounds: v.AABB2d{
  55. A: v.Point2d{X: 200, Y: 100},
  56. B: v.Point2d{X: 300, Y: 200},
  57. },
  58. },
  59. },
  60. },
  61. r: Robot{
  62. Position: v.Point2d{X: 100, Y: 100},
  63. },
  64. target: v.Vector2d{X: 0, Y: 0},
  65. collision: false,
  66. },
  67. }
  68. for _, test := range tests {
  69. collision, location, _ := test.r.checkCollisions(&test.g, test.target)
  70. if collision != test.collision {
  71. t.Errorf("expected: %t, actual: %t", test.collision, collision)
  72. }
  73. if test.location == nil {
  74. if location != nil {
  75. t.Errorf("expected: %+v, actual: %+v", test.location, location)
  76. }
  77. }
  78. if test.location != nil {
  79. if location == nil {
  80. t.Errorf("expected: %+v, actual: %+v", test.location, location)
  81. } else {
  82. if math.Abs(float64(location.X-test.location.X)) > 1e-4 || math.Abs(float64(location.Y-test.location.Y)) > 1e-4 {
  83. t.Errorf("expected: %+v, actual: %+v", test.location, location)
  84. }
  85. }
  86. }
  87. }
  88. }
  89. // XXX
  90. func TestBotBotCollisions(t *testing.T) {
  91. t.Skip("NYI")
  92. }
  93. func TestProbeResultType(t *testing.T) {
  94. g := Game{
  95. width: 800,
  96. height: 400,
  97. obstacles: []Obstacle{
  98. Obstacle{
  99. Bounds: v.AABB2d{
  100. A: v.Point2d{X: 200, Y: 100},
  101. B: v.Point2d{X: 300, Y: 200},
  102. },
  103. },
  104. Obstacle{
  105. Bounds: v.AABB2d{
  106. A: v.Point2d{X: 400, Y: 200},
  107. B: v.Point2d{X: 600, Y: 300},
  108. },
  109. },
  110. },
  111. players: make(map[*Player]bool),
  112. }
  113. r1 := Robot{
  114. Position: v.Point2d{X: 100, Y: 100},
  115. Probe: &v.Point2d{X: 900, Y: 350},
  116. Id: "Bilbo's Bot",
  117. }
  118. p1 := Player{
  119. Robots: []*Robot{&r1},
  120. ProtoTalker: ProtoTalker{
  121. Id: "bilbo",
  122. },
  123. }
  124. r2 := Robot{
  125. Position: v.Point2d{X: 100, Y: 200},
  126. Probe: &v.Point2d{X: 100, Y: 90},
  127. Id: "Frodo's Bot",
  128. }
  129. p2 := Player{
  130. Robots: []*Robot{&r2},
  131. ProtoTalker: ProtoTalker{
  132. Id: "Frodo",
  133. },
  134. }
  135. g.players[&p1] = true
  136. g.players[&p2] = true
  137. r1.Tick(&g)
  138. r2.Tick(&g)
  139. if r1.ProbeResult.Type != "obstacle" {
  140. t.Errorf("incorrect probe type (expected obstacle, got %s)", r1.ProbeResult.Type)
  141. }
  142. if r2.ProbeResult.Type != "robot" {
  143. t.Errorf("incorrect probe type (expected robot, got %s)", r1.ProbeResult.Type)
  144. }
  145. }