Crossfire Server, Trunk
CFTowerDefense.py
Go to the documentation of this file.
1 import Crossfire
2 
3 bulletwall = [
4  'lbulletwall_1',
5  'lbulletwall_2',
6  'lbulletwall_3',
7  'lbulletwall_4',
8  'lbulletwall_5',
9  'lbulletwall_6',
10  'lbulletwall_7',
11  'lbulletwall_8',
12 ]
13 
14 lightningwall = [
15  'lightningwall_1',
16  'lightningwall_2',
17  'lightningwall_3',
18  'lightningwall_4',
19  'lightningwall_5',
20  'lightningwall_6',
21  'lightningwall_7',
22  'lightningwall_8',
23 ]
24 
25 firewall = [
26  'firewall_1',
27  'firewall_2',
28  'firewall_3',
29  'firewall_4',
30  'firewall_5',
31  'firewall_6',
32  'firewall_7',
33  'firewall_8',
34 ]
35 
36 
37 def create_tower(player, current_map, tile, builder):
38  """
39  If a builder is dropped on a tower placement tile, a tower is built
40 
41  player: the player object
42  current_map: the map object the player is in
43  tile: all objects on a tile (a list of Crossfire objects)
44  builder: the tower builder (a Crossfire object)
45 
46  Returns: True if a tower was built, False otherwise.
47  """
48  for ob in tile:
49  if ob.Name == 'tower placement':
50  # key/value: builder name/(arch name, spell level)
51  builders = {
52  'build large bullet tower': ('lbulletwall_1', 1),
53  'build lightning tower': ('lightningwall_1', 1),
54  'build fire tower': ('firewall_1', 40)
55  }
56 
57  for name in builders:
58  if name in builder.Name:
59  wall = Crossfire.CreateObjectByName(builders[name][0])
60  wall.Level = builders[name][1]
61 
62  wall.Teleport(current_map, player.X, player.Y)
63  player.Teleport(current_map, player.X, player.Y+1)
64 
65  builder.Quantity -= 1
66  return True
67  return False
68 
69 
70 def destroy_tower(tile, towerlist):
71  """
72  If an object in tile exists in towerlist, the object is removed
73 
74  tile: all objects on a tile (a list of Crossfire objects)
75  towerlist: bulletwall, firewall, or lightningwall
76 
77  Returns: True if a tower was destroyed, False otherwise.
78  """
79  for ob in tile:
80  if ob.ArchName in towerlist:
81  ob.Remove()
82  return True
83  return False
84 
85 
86 def replace_tower(current_map, tile, towerlist, level=0):
87  """
88  If an object in tile exists in towerlist, the object is replaced with the
89  next direction in towerlist
90 
91  current_map: the map object the player is in
92  tile: all objects on a tile (a list of Crossfire objects)
93  towerlist: bulletwall, firewall, or lightningwall
94  level: the spell level of the tower
95 
96  Returns: True if a tower was replaced, False otherwise.
97  """
98  for ob in tile:
99  if ob.ArchName in towerlist:
100  position = towerlist.index(ob.ArchName)
101  x, y = ob.X, ob.Y
102 
103  if position == len(towerlist)-1:
104  new_position = 0
105  else:
106  new_position = position+1
107 
108  ob.Remove()
109  wall = Crossfire.CreateObjectByName(towerlist[new_position])
110  wall.Teleport(current_map, x, y)
111 
112  # Add original rebalanced spell level for tower
113  if level:
114  wall.Level = level
115  return True
116  return False
117 
118 
119 def player_objects(player, current_map) -> list:
120  """
121  Returns all objects in the direction the player faces
122 
123  player: the player object
124  current_map: the map object the player is in
125  """
126  # key/value: crossfire direction number/corresponding (x, y) coordinate
127  directions = {
128  Crossfire.Direction.NORTH: (player.X, player.Y-1),
129  Crossfire.Direction.SOUTH: (player.X, player.Y+1),
130  Crossfire.Direction.EAST: (player.X+1, player.Y),
131  Crossfire.Direction.WEST: (player.X-1, player.Y),
132  Crossfire.Direction.NORTHWEST: (player.X-1, player.Y-1),
133  Crossfire.Direction.NORTHEAST: (player.X+1, player.Y-1),
134  Crossfire.Direction.SOUTHWEST: (player.X-1, player.Y+1),
135  Crossfire.Direction.SOUTHEAST: (player.X+1, player.Y+1)
136  }
137  x, y = directions[player.Facing]
138 
139  tile_ob = []
140  ob = current_map.ObjectAt(x, y)
141  while ob:
142  tile_ob.append(ob)
143  ob = ob.Above
144 
145  return tile_ob
146 
147 
148 def player_tile_objects(player, current_map) -> list:
149  """
150  Returns all objects in the tile the player is standing on
151 
152  player: the player object
153  current_map: the map object the player is in
154  """
155  tile_ob = []
156  ob = current_map.ObjectAt(player.X, player.Y)
157  while ob:
158  tile_ob.append(ob)
159  ob = ob.Above
160 
161  return tile_ob
CFTowerDefense.destroy_tower
def destroy_tower(tile, towerlist)
Definition: CFTowerDefense.py:70
CFTowerDefense.replace_tower
def replace_tower(current_map, tile, towerlist, level=0)
Definition: CFTowerDefense.py:86
CFTowerDefense.player_objects
list player_objects(player, current_map)
Definition: CFTowerDefense.py:119
CFTowerDefense.create_tower
def create_tower(player, current_map, tile, builder)
Definition: CFTowerDefense.py:37
CFTowerDefense.player_tile_objects
list player_tile_objects(player, current_map)
Definition: CFTowerDefense.py:148