Crossfire Server, Trunk
combat_chicken.py
Go to the documentation of this file.
1 # Script for the Combat Chicken for Sentrio's farmhouse (/lake_country/sentrio_farmhouse).
2 #
3 # Copyright 2007 Nicolas Weeger
4 # Released as GPL
5 #
6 # This script is supposed to be called for the time event.
7 
8 import Crossfire
9 import random
10 import CFMove
11 
12 key_target = 'chicken_target' # where the chicken is trying to go
13 key_food = 'chicken_food' # currently eaten food
14 key_attacked = 'chicken_attacked' # if set, chicken has normal monster behaviour - so it reacts when attacked
15 stay_on_floor = 'small_stones' # what ground it'll stay on
16 # what the chicken will eat, and food increase
17 eat = { 'orc\'s livers' : 5, 'orc\'s hearts' : 6, 'goblin\'s livers' : 1, 'goblin\'s hearts' : 2 }
18 
19 # returns floor with specific name
20 def has_floor(x, y, name):
21  obj = Crossfire.WhoAmI().Map.ObjectAt(x, y)
22  while obj != None:
23  if obj.Floor == 1 and obj.ArchName == name:
24  return True
25  obj = obj.Above
26  return False
27 
28 # returns some eligible food on specified spot
29 def find_food(chicken, x, y):
30  obj = chicken.Map.ObjectAt(x, y)
31  while obj != None:
32  #Crossfire.Log(Crossfire.LogMonster, obj.Name)
33  if obj.NamePl in eat:
34  return obj
35  obj = obj.Above
36  return None
37 
38 # main chicken handler
40  chicken = Crossfire.WhoAmI()
41  if chicken.Enemy != None:
42  # chicken won't let itself get killed easily!
43  chicken.WriteKey(key_attacked, '1', 1)
44 
45  if chicken.ReadKey(key_attacked) != '':
46  return
47 
48  Crossfire.SetReturnValue(1)
49  if chicken.Map.Darkness >= 3:
50  # too dark, night is for sleeping
51  return
52 
53  target = chicken.ReadKey(key_target)
54  if target != '':
55  x = int(target.split('|')[0])
56  y = int(target.split('|')[1])
57  if CFMove.get_object_to(chicken, x, y) != 0:
58  return
59  # target found, let's try to eat it
60  food = find_food(chicken, x, y)
61  chicken.WriteKey(key_target, '', 1)
62  if food != None:
63  chicken.Map.Print('The %s eats the %s!'%(chicken.Name, food.Name))
64  got = chicken.ReadKey(key_food)
65  if got == '':
66  got = 0
67  else:
68  got = int(got)
69  got = got + eat[food.NamePl]
70  # drop an egg?
71  if random.randint(1, 100) <= ( got * 2 ):
72  egg = chicken.Map.CreateObject('chicken_egg', chicken.X, chicken.Y)
73  egg.Name = 'Combat Chicken egg'
74  egg.NamePl = 'Combat Chicken eggs'
75  egg.Quantity = 1
76  chicken.Map.Print('The %s lays an egg!'%chicken.Name)
77  got = 0
78  chicken.WriteKey(key_food, str(got), 1)
79  food.Quantity = food.Quantity - 1
80  return
81  else:
82  # try to find some food
83  #chicken.Map.Print('find food...')
84  food = None
85  for x in range(-3, 4):
86  for y in range(-3, 4):
87  food = find_food(chicken, chicken.X + x, chicken.Y + y)
88  #chicken.Map.Print('find food %d %d...'%(chicken.X + x, chicken.Y + y))
89  if food != None:
90  target = '%d|%d'%(food.X, food.Y)
91  chicken.WriteKey(key_target, target, 1)
92  #chicken.Map.Print('got food %s'%target)
93  break
94  if food != None:
95  break
96 
97  # nothing found, random walk
98  for test in [1, 10]:
99  dir = random.randint(1, 8)
100  if (has_floor(chicken.X + CFMove.dir_x[dir], chicken.Y + CFMove.dir_y[dir], stay_on_floor)):
101  chicken.Move(dir)
102  Crossfire.SetReturnValue(1)
103  return
104 
105 
106 move_chicken()
combat_chicken.has_floor
def has_floor(x, y, name)
Definition: combat_chicken.py:20
make_face_from_files.str
str
Definition: make_face_from_files.py:30
combat_chicken.move_chicken
def move_chicken()
Definition: combat_chicken.py:39
CFMove.get_object_to
def get_object_to(obj, x, y)
Definition: CFMove.py:24
make_face_from_files.int
int
Definition: make_face_from_files.py:32
combat_chicken.find_food
def find_food(chicken, x, y)
Definition: combat_chicken.py:29