| 1 |
# Script for the roll-o-matic. |
| 2 |
# Idea courtesy Alex Schultz. |
| 3 |
# |
| 4 |
# Copyright 2007 Nicolas Weeger |
| 5 |
# Released as GPL |
| 6 |
# |
| 7 |
# This script makes its item move following tiles, in direction specified by a player |
| 8 |
|
| 9 |
import Crossfire |
| 10 |
|
| 11 |
key_direction = 'rom_dir' |
| 12 |
key_follow = 'rom_follow' |
| 13 |
|
| 14 |
dir_x = [ 0, 0, 1, 1, 1, 0, -1, -1, -1 ] |
| 15 |
dir_y = [ 0, -1, -1, 0, 1, 1, 1, 0, -1 ] |
| 16 |
|
| 17 |
def find_floor(x, y): |
| 18 |
obj = me.Map.ObjectAt(x, y) |
| 19 |
while obj != None: |
| 20 |
if obj.Floor == 1: |
| 21 |
return obj.ArchName |
| 22 |
obj = obj.Above |
| 23 |
return '' |
| 24 |
|
| 25 |
def find_player(): |
| 26 |
obj = me.Map.ObjectAt(me.X, me.Y) |
| 27 |
while obj != None: |
| 28 |
if obj.Type == Crossfire.Type.PLAYER: |
| 29 |
return obj |
| 30 |
obj = obj.Above |
| 31 |
return None |
| 32 |
|
| 33 |
def has_floor(x, y, name): |
| 34 |
obj = me.Map.ObjectAt(x, y) |
| 35 |
while obj != None: |
| 36 |
if obj.Floor == 1 and obj.ArchName == name: |
| 37 |
return True |
| 38 |
obj = obj.Above |
| 39 |
return False |
| 40 |
|
| 41 |
|
| 42 |
def abs_dir(d): |
| 43 |
while d < 1: |
| 44 |
d = d + 8 |
| 45 |
while d > 8: |
| 46 |
d = d - 8 |
| 47 |
return d |
| 48 |
|
| 49 |
def stop(): |
| 50 |
me.WriteKey(key_direction, '', 1) |
| 51 |
me.WriteKey(key_follow, '', 1) |
| 52 |
me.Map.Print('The %s stops moving.'%me.Name) |
| 53 |
|
| 54 |
def handle_move(): |
| 55 |
want_dir = me.ReadKey(key_direction) |
| 56 |
floor = me.ReadKey(key_follow) |
| 57 |
if want_dir == '' or floor == '': |
| 58 |
return |
| 59 |
# me.Map.Print('roll') |
| 60 |
pl = find_player() |
| 61 |
want_dir = int(want_dir) |
| 62 |
done = False |
| 63 |
x = me.X |
| 64 |
y = me.Y |
| 65 |
for check in [0, 1, -1]: |
| 66 |
d = abs_dir(want_dir + check) |
| 67 |
if has_floor(x + dir_x[d], y + dir_y[d], floor): |
| 68 |
# Next time, move in the direction we last moved in. |
| 69 |
# This heuristic helps follow the roads better. |
| 70 |
me.WriteKey(key_direction, str(d)) |
| 71 |
if me.Move(d) == 0: |
| 72 |
continue |
| 73 |
|
| 74 |
if pl != None: |
| 75 |
pl.Move(d) |
| 76 |
done = True |
| 77 |
break |
| 78 |
|
| 79 |
if not done: |
| 80 |
stop() |
| 81 |
return |
| 82 |
|
| 83 |
def handle_say(): |
| 84 |
msg = Crossfire.WhatIsMessage() |
| 85 |
if msg == 'stop': |
| 86 |
if me.ReadKey(key_direction) != '': |
| 87 |
stop() |
| 88 |
return |
| 89 |
|
| 90 |
want_dir = -1 |
| 91 |
|
| 92 |
for d in Crossfire.DirectionName.keys(): |
| 93 |
if msg == Crossfire.DirectionName[d].lower(): |
| 94 |
want_dir = d |
| 95 |
break |
| 96 |
|
| 97 |
if want_dir == -1: |
| 98 |
return |
| 99 |
|
| 100 |
floor = find_floor(me.X, me.Y) |
| 101 |
if floor == '': |
| 102 |
return |
| 103 |
|
| 104 |
if me.ReadKey(key_direction) == '': |
| 105 |
me.Map.Print('The %s starts moving!'%me.Name) |
| 106 |
|
| 107 |
me.WriteKey(key_direction, str(want_dir), 1) |
| 108 |
me.WriteKey(key_follow, floor, 1) |
| 109 |
|
| 110 |
def do_handle(): |
| 111 |
if me.Map == None: |
| 112 |
return |
| 113 |
|
| 114 |
if evt.Subtype == Crossfire.EventType.SAY: |
| 115 |
handle_say() |
| 116 |
elif evt.Subtype == Crossfire.EventType.TIME: |
| 117 |
handle_move() |
| 118 |
|
| 119 |
|
| 120 |
evt = Crossfire.WhatIsEvent() |
| 121 |
me = Crossfire.WhoAmI() |
| 122 |
pl = Crossfire.WhoIsActivator() |
| 123 |
|
| 124 |
Crossfire.SetReturnValue(1) |
| 125 |
|
| 126 |
do_handle() |