Create utility library for movement.

git-svn-id: svn://svn.code.sf.net/p/crossfire/code/maps/trunk@6364 282e977c-c81d-0410-88c4-b93c2d0d6712
master
ryo_saeba 2007-05-29 21:18:04 +00:00
parent 369769316a
commit 3ccfe258be
2 changed files with 65 additions and 35 deletions

62
python/CFMove.py 100644
View File

@ -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))

View File

@ -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