Skip to content

Commit

Permalink
basic AI with some glitches = endday commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sirvaulterscoff committed Mar 25, 2011
1 parent daad950 commit 129f10f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
46 changes: 31 additions & 15 deletions critters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
import util
from thirdparty.libtcod import libtcodpy as libtcod

WALKING = 1
FLYING = 2
Expand All @@ -10,6 +11,7 @@
INTELLIGENT = 1 << 7

class Critter(object):
name = 'crit'
ALL = []
char = '@'
color = [255, 255, 255]
Expand All @@ -31,7 +33,8 @@ class Critter(object):
common = 10
__metaclass__ = util.AutoAdd
skip_register = True
fov_range = 10
fov_range = 5
ai = None

def __init__(self):
self.map = None
Expand All @@ -45,35 +48,49 @@ def place(self, x, y, map):
self.x, self.y = x, y
self.map = map

def move(self, x, y):
self.x, self.y = x, y
def move(self, dx, dy):
newx, newy = self.x + dx, self.y + dy
if self.map.has_critter_at( (newx, newy)):
return
player = self.map.player
if player.x == newx and player.y == newy:
self.attack(newx, newy)
return

def walk(self, dx, dy):
self.move(self.x + dx, self.y + dy)
next_tile = self.map[newy][newx]
if next_tile.passable():
self.x, self.y = newx, newy

def attack(self, newx, newy):
print (self.name + ' tries to hit you')

def move_towards(self, target_x, target_y):
dx = target_x - self.x
dy = target_y - self.y
distance = math.sqrt(dx ** 2 + dy ** 2)

#normalize it to length 1 (preserving direction), then round it and
#convert to integer so the movement is restricted to the map grid
dx = int(round(dx / distance))
dy = int(round(dy / distance))
self.move(dx, dy)

def see_player(self):
player = self.map.player
see_range = self.fov_range / 2
see_range = self.fov_range
#if it's intelligent one - let it follow source of light
if self.flags & INTELLIGENT:
see_range += player.light_range / 2
if T.map_is_in_fov(self.map.fov_map, self.x, self.y):
d = distance(self.x, self.y, player.x, player.y)
if d <= fov_range:
if libtcod.map_is_in_fov(self.map.fov_map, self.x, self.y):
d = util.distance(self.x, self.y, player.x, player.y)
if d <= see_range:
return d
return None

def take_turn(self):
if self.ai is None:
if self.see_player():
player = self.map.player
self.move_towards(player.x, player.y)

class Player(Critter):
x, y = 0, 0
char = '@'
Expand Down Expand Up @@ -103,6 +120,7 @@ def attack(self, mobx, moby):

class Rat(Critter):
char = 'r'
name = 'Rat'
color = [90, 30, 40]
description_past = 'Obesity makes this plague-bearing rats realy huge. Interesting, can you even kill one that big...'
description_present = 'Huge, fat rat somehow managed to leave sewers or households and now posess enourmous threat to unwary adventurer.'
Expand All @@ -113,6 +131,7 @@ class Rat(Critter):

class Bat(Critter):
char = 'w'
name = 'bat'
flags = FLYING
color = [0, 255, 60]
description_past = 'Strange green glow comes from afar... Maybe it\'s a lost sool seeking exit from endless caverns... Wait! It\'s a bat?! Ouch, stop biting me!'
Expand All @@ -126,6 +145,7 @@ class Bat(Critter):

class Orc(Critter):
char = 'o'
name = 'orc'
flags = WALKING | INTELLIGENT
color = [255, 0, 0]
description_past = 'Beware! This mighty ugly-looking humanoid will eat you for dinner. Nightmare comes to live. By the way, there should be it\' friends somewhere nearby'
Expand All @@ -135,7 +155,3 @@ class Orc(Critter):
base_hp = 10
base_ac = 1
dlvl = 3


class BasicAI(object):
pass
1 change: 1 addition & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def main_loop(self, map, player):
for critter in map.map_critters:
libtcod.console_set_foreground_color(self.con, self.create_color(critter.color))
self.print_critter(critter.x, critter.y, critter.char)
critter.take_turn()

libtcod.console_set_foreground_color(self.con, self.create_color(player.color))
self.print_critter(player.x, player.y, player.char)
Expand Down
4 changes: 3 additions & 1 deletion map.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def recompute_fov(self):
libtcod.map_compute_fov(self.fov_map, self.player.x, self.player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGORITHM)

def place_critter(self, crit_level, crit_hd, x, y):
crit = util.random_by_level(crit_level, critters.Critter.ALL)()
crit = util.random_by_level(crit_level, critters.Critter.ALL)
if crit is None: return
crit = crit()
crit.adjust_hd(crit_hd)
self.map_critters.append(crit)
self.critter_xy_cache[(x, y)] = crit
Expand Down
11 changes: 9 additions & 2 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def cap_lower(what, min, to):

def random_by_level(level, items):
items = filter(lambda a: a.dlvl == level, items)
n = randrange(sum(item.common for item in items))
start = sum(item.common for item in items)
if start == 0 : return None
n = randrange(start)
for item in items:
if n < item.common:
return item
Expand All @@ -29,4 +31,9 @@ def __new__(mcs, name, bases, dict):
cls = type.__new__(mcs, name, bases, dict)
if not dict.get('skip_register'):
cls.ALL.append(cls)
return cls
return cls

def distance(x1, y1, x2, y2):
dx = abs(x2-x1)
dy = abs(y2-y1)
return (dx+dy+max(dx,dy))/2

0 comments on commit 129f10f

Please sign in to comment.