Skip to content

Commit c1fa239

Browse files
committed
Implementing a wall-hugging AI.
1 parent 36d5919 commit c1fa239

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

ai/wall.py

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
# Title: WallTurtle
44
# Author: Adam Rumpf
55
# Version: 1.0.0
6-
# Date: 11/26/2020
6+
# Date: 1/5/2021
77

8+
import math
89
import game.tcturtle
910

1011
class CombatTurtle(game.tcturtle.TurtleParent):
1112
"""Wall-hugging combat turtle.
1213
13-
A turtle that attempts to navigate around obstacles by hugging walls using
14-
a left-hand rule.
14+
A turtle that attempts to navigate around obstacles by "feeling" the walls
15+
around it.
1516
1617
When it has direct line of sight to the opponent, it moves directly
1718
towards it. Otherwise it moves towards the opponent until hitting a wall,
18-
at which point it wraps around the wall by keeping its left side against
19-
it.
19+
at which point it attempts to turn so that the way ahead is free.
2020
"""
2121

2222
#-------------------------------------------------------------------------
@@ -71,7 +71,9 @@ def setup(self):
7171
Initialization code for Combat Turtle.
7272
"""
7373

74-
pass
74+
# Define the relative polar coordinates around the turtle to scan
75+
self.nose_rel = (8, 0.0) # just ahead of turtle's front
76+
self.hand_rel = (8, math.pi/2) # to left of turtle
7577

7678
#-------------------------------------------------------------------------
7779

@@ -80,12 +82,47 @@ def step(self):
8082
Step event code for Combat Turtle.
8183
"""
8284

83-
# Outline:
84-
# Turn and move towards the opponent as long as there's line of sight.
85-
# Otherwise, attempt to move towards opponent while scanning ahead of self.
86-
# If there's an obstacle, begin wall hugging behavior, which is based on scanning a point to left of self.
87-
# As long as that point is free, turn right while moving forward until it becomes blocked for the first time.
88-
# As long as that point is blocked, move forward.
89-
# After sticking to a wall, turn left whenever the point to the turtle's left is unblocked.
90-
91-
pass
85+
# Determine behavior based on whether there is line of sight
86+
if (self.line_of_sight() == True):
87+
# If there is line of sight, move directly towards opponent
88+
89+
# Turn towards opponent
90+
self.turn_towards()
91+
92+
# Move towards opponent (or away if too close)
93+
if self.distance() > 4*self.missile_radius:
94+
self.forward()
95+
else:
96+
self.backward()
97+
98+
# Shoot if facing opponent
99+
if (self.can_shoot == True and
100+
abs(self.relative_heading_towards()) <= 10):
101+
self.shoot()
102+
103+
else:
104+
# If no line of sight, attempt to navigate around obstacles
105+
106+
# Calculate Cartesian coordinates of nose and hand
107+
nose = ((self.x + self.nose_rel[0]*
108+
math.cos(math.radians(self.heading)+self.nose_rel[1])),
109+
(self.y - self.nose_rel[0]*
110+
math.sin(math.radians(self.heading)+self.nose_rel[1])))
111+
hand = ((self.x + self.hand_rel[0]*
112+
math.cos(math.radians(self.heading)+self.hand_rel[1])),
113+
(self.y - self.hand_rel[0]*
114+
math.sin(math.radians(self.heading)+self.hand_rel[1])))
115+
116+
# Determine behavior based on whether nose and hand are clear
117+
if self.free_space(nose) == True:
118+
# Move forward when clear ahead
119+
self.forward()
120+
else:
121+
if self.free_space(hand) == True:
122+
# If free to left, turn left
123+
self.left()
124+
self.forward()
125+
else:
126+
# If blocked ahead and to left, turn right
127+
self.right()
128+
self.forward()

0 commit comments

Comments
 (0)