From 3ccfe258be45a64946dce1bc65b3328f4c73c226 Mon Sep 17 00:00:00 2001 From: ryo_saeba Date: Tue, 29 May 2007 21:18:04 +0000 Subject: [PATCH] Create utility library for movement. git-svn-id: svn://svn.code.sf.net/p/crossfire/code/maps/trunk@6364 282e977c-c81d-0410-88c4-b93c2d0d6712 --- python/CFMove.py | 62 +++++++++++++++++++++++++++++++ python/monsters/combat_chicken.py | 38 ++----------------- 2 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 python/CFMove.py diff --git a/python/CFMove.py b/python/CFMove.py new file mode 100644 index 000000000..0e3506be2 --- /dev/null +++ b/python/CFMove.py @@ -0,0 +1,62 @@ +# CFMove.py - various move-related functions. +# +# Copyright (C) 2007 Nicolas Weeger +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + +'''Those represents the x/y offsets to move to a specific direction.''' +dir_x = [ 0, 0, 1, 1, 1, 0, -1, -1, -1 ] +dir_y = [ 0, -1, -1, 0, 1, 1, 1, 0, -1 ] + +def coordinates_to_dir(x, y): + '''This returns, for the specified offsets, the direction to move to get there.''' + q = 0 + if(y == 0): + q = -300 * x; + else: + q = int(x * 100 / y); + if(y>0): + if(q < -242): + return 3 + if (q < -41): + return 2 + if (q < 41): + return 1 + if (q < 242): + return 8 ; + return 7 + + if (q < -242): + return 7 + if (q < -41): + return 6 + if (q < 41): + return 5 + if (q < 242): + return 4 + return 3 + +def get_object_to(obj, x, y): + '''This tries to move obj to the (x, y) location. + Return value is: + * 0: object is on the spot + * 1: object moved towards the goal + * 2: object's movement was blocked. + ''' + if obj.X == x and obj.Y == y: + return 0 + # Move returns 0 for couldn't move, 1 for moved. + return 2 - obj.Move(coordinates_to_dir(obj.X - x, obj.Y - y)) diff --git a/python/monsters/combat_chicken.py b/python/monsters/combat_chicken.py index dd09e2eb3..a2d94a156 100644 --- a/python/monsters/combat_chicken.py +++ b/python/monsters/combat_chicken.py @@ -7,6 +7,7 @@ import Crossfire import random +import CFMove key_target = 'chicken_target' # where the chicken is trying to go key_food = 'chicken_food' # currently eaten food @@ -15,38 +16,6 @@ stay_on_floor = 'small_stones' # what ground it'll stay on # what the chicken will eat, and food increase eat = { 'orc\'s livers' : 5, 'orc\'s hearts' : 6, 'goblin\'s livers' : 1, 'goblin\'s hearts' : 2 } -# those should be in some common library -dir_x = [ 0, 0, 1, 1, 1, 0, -1, -1, -1 ] -dir_y = [ 0, -1, -1, 0, 1, 1, 1, 0, -1 ] - -# this should be in some common library -def coordinates_to_dir(x, y): - q = 0 - if(y == 0): - q = -300 * x; - else: - q = int(x * 100 / y); - if(y>0): - if(q < -242): - return 3 - if (q < -41): - return 2 - if (q < 41): - return 1 - if (q < 242): - return 8 ; - return 7 - - if (q < -242): - return 7 - if (q < -41): - return 6 - if (q < 41): - return 5 - if (q < 242): - return 4 - return 3 - # returns floor with specific name def has_floor(x, y, name): obj = Crossfire.WhoAmI().Map.ObjectAt(x, y) @@ -85,8 +54,7 @@ def move_chicken(): if target != '': x = int(target.split('|')[0]) y = int(target.split('|')[1]) - if chicken.X != x or chicken.Y != y: - chicken.Move(coordinates_to_dir(chicken.X - x, chicken.Y - y)) + if CFMove.get_object_to(chicken, x, y) != 0: return # target found, let's try to eat it food = find_food(chicken, x, y) @@ -129,7 +97,7 @@ def move_chicken(): # nothing found, random walk for test in [1, 10]: dir = random.randint(1, 8) - if (has_floor(chicken.X + dir_x[dir], chicken.Y + dir_y[dir], stay_on_floor)): + if (has_floor(chicken.X + CFMove.dir_x[dir], chicken.Y + CFMove.dir_y[dir], stay_on_floor)): chicken.Move(dir) Crossfire.SetReturnValue(1) return