Skip to content

Commit

Permalink
work on weighting spawn toward center and away from player and nommer
Browse files Browse the repository at this point in the history
  • Loading branch information
david-fong committed Feb 22, 2019
1 parent c37adfd commit 57be984
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,25 +413,26 @@ def spawn_new_targets(self):
Targets try to spawn spread out and not too
close to the player or the nommer.
"""
def bell(p1: Pair, t2: Tile):
dist = (p1 - t2.pos).norm()
bounds = self.width / 2
return 1 - 2 ** -((2*dist/bounds)**2)
def bell(p1: Pair, p2: Pair, radius, lip=1.0, peak=0.0):
dist = (p1 - p2).norm()
return (peak-lip) * 2 ** -((2*dist/radius)**2) + lip

# Get an appropriate number
# of random keys for targets:
new_targets = []
while len(self.targets) < self.num_targets:
# Favor tiles with few targets nearby:
available = list(filter(
lambda t: t not in self.targets and
not self.is_character(t), self.grid))
weights = dict.fromkeys(available, 0)
for tile in weights:
loneliness = sum(map(lambda t: bell(tile.pos, t), self.targets))
loneliness += self.num_targets * 0.4 * bell(self.player, tile)
loneliness += self.num_targets * 0.6 * bell(self.runner, tile)
weights[tile] = loneliness
lambda tile: tile not in self.targets and
not self.is_character(tile), self.grid))
weights = dict.fromkeys(available, 0.0)

center = Pair(self.width//2, self.width//2)
for t in weights:
# Slight bias towards the center:
weights[t] = bell(center, t.pos, 0.8*self.width, lip=0, peak=1)
weights[t] += bell(self.player, t.pos, self.width/3, lip=0, peak=0.6)
weights[t] += bell(self.nommer, t.pos, self.width/3, lip=0, peak=0.6)

# Use the generated weights to get a new target:
target = weighted_choice(weights)
Expand Down Expand Up @@ -535,14 +536,17 @@ def __enemy_diff(self, origin: Pair, target: Pair,

def enemy_base_speed(self):
"""
Returns a speed in tiles per second
Returns a speed in tiles per second.
Uses an upside-down bell-curve shape
as a function of the sum of the absolutes
of the the player's score and losses.
"""
obtained = self.score.get() + self.losses.get()

high = 2.2 # Tiles per second
low = 0.33 # Tiles per second
slowness = 80 * self.num_targets
return (high-low) * (1-(2**-(obtained/slowness))) + low
high = 1.4 # Tiles per second
low = 0.34 # Tiles per second
slowness = 15 * self.num_targets
return (high-low) * (1-(2**-(obtained/slowness)**2)) + low

def player_avg_period(self):
if len(self.time_delta) > 5:
Expand Down Expand Up @@ -919,12 +923,13 @@ def __print_controls(self):
'with- I don\'t judge)', ],
['nommer',
'This guy will compete with you for',
'food. While he\'s quite harmless, he',
'refuses to pay his bill, and the chaser',
'is after you for it!', ],
'food. While he\'s quite harmless,',
'he hasn\'t paid his bill, and the',
'chaser is after you for it!', ],
['runner',
'What a cheeky little fellow. Maybe he',
'just wants to play a game of tag?', ],
'just wants to play a game of tag? Wait-',
'is that a stolen wallet in his hand?', ],
)
max_line_length = max([max(map(
lambda line: len(line), character))
Expand Down

0 comments on commit 57be984

Please sign in to comment.