-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.py
259 lines (213 loc) · 7.1 KB
/
Ship.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import sys
import config
import util
from Projectile import Projectile
from RoguePy.libtcod import libtcod
from RoguePy.Game import Entity
from shipTypes import shipTypes
from sounds import fail
from util import randint
class Ship(Entity):
def __init__(self, map, type, x, y, isPlayer=False, stats=None):
self.name = type
if stats is None:
self.stats = shipTypes[type]
else:
self.stats = stats
self.ch = '@'
self.isPlayer = isPlayer
self.isPirate = False
self.x = x
self.y = y
self.sunk = False
self.reloading = False
self.coolDown = 0
self.maxSails = config.maxSails
placed = False
attempts = 0
while not placed:
try:
super(Ship, self).__init__(
map, self.mapX, self.mapY, self.name, self.ch, self.stats['color'], True, config.ship['minView'], isPlayer)
placed = True
except:
dx, dy = 0, 0
while not (dx or dy):
dx = randint(1, -1)
dy = randint(1, -1)
# failed attempt (no water, don't count it)
if not map.getCell(self.mapX + dx, self.mapY + dy).terrain.passable:
continue
attempts += 1
if attempts >= 10:
raise ShipPlacementError
self.anchored = True
self.heading = 0.0
self.headingRad = 0.0
self.sails = 0
self.speed = 0.0
self.crew = 0
self.cannonballs = 0
self.chainshot = 0
self.goods = {
'food': 0,
'rum': 0,
'wood': 0,
'cloth': 0,
'coffee': 0,
'spice': 0
}
self.inHold = 0
# HACK calculate speed...
self.damageSails(0)
def killCrew(self, count):
self.crew -= count
self.crew = max(self.crew, 0)
def damageHull(self, dmg):
self.stats['hullDamage'] += dmg
if self.stats['hullDamage'] >= 100:
self.stats['hullDamage'] = 100
self.sunk = True
def damageSails(self, dmg):
self.stats['sailDamage'] += dmg
if self.stats['sailDamage'] >= 100:
self.stats['sailDamage'] = 100
self.maxSails = int(config.maxSails - (self.stats['sailDamage']/100.0 * config.maxSails))
@staticmethod
def getBuyPrice(stats):
return int(config.economy['buyMul'] * Ship._getValue(stats))
@staticmethod
def getSellPrice(stats):
return int(config.economy['sellMul'] * Ship._getValue(stats))
def addCannonballs(self, count):
if self.inHold + count > self.stats['size']:
return False
self.cannonballs += count
self.inHold += count
return True
def addChainshot(self, count):
if self.inHold + count > self.stats['size']:
return False
self.chainshot += count
self.inHold += count
return True
@staticmethod
def _getValue(stats):
value = stats['price']
if stats['hullDamage'] > 0:
value -= int(stats['hullDamage'] / 100.0 * stats['price'] * 2/3)
if stats['sailDamage'] > 0:
value -= int(stats['sailDamage'] / 100.0 * stats['price'] * 1/3)
return value
def addGoods(self, item):
if item not in self.goods:
return False
self.goods[item] += 1
self.inHold += 1
if self.inHold > self.stats['size']:
self.goods[item] -= 1
self.inHold -= 1
return False
return True
def takeGoods(self, item):
if item not in self.goods:
return False
if self.goods[item] < 1:
return False
self.goods[item] -= 1
self.inHold -= 1
return True
@property
def x(self):
return self.__x
@x.setter
def x(self, x):
self.__x = x
self.mapX = int(round(x))
@property
def y(self):
return self.__y
@y.setter
def y(self, y):
self.__y = y
self.mapY = int(round(y))
@property
def sails(self):
return self.__sails
@sails.setter
def sails(self, sails):
self.__sails = sails
self.speed = config.sailStep * self.__sails * self.stats['maxSpeed']
def toggleAnchor(self):
self.anchored = not self.anchored
def sailAdjust(self, step):
newSails = max(min(self.sails + step, self.maxSails), 0)
if newSails != self.sails:
self.sails = newSails
def headingAdjust(self, val):
self.heading += val
if self.heading < 0:
self.heading += 360
elif self.heading >= 360:
self.heading -= 360
def repairHull(self):
if not self.stats['hullDamage']:
return False
self.stats['hullDamage'] -= config.shipyard['repairReturn']
if self.stats['hullDamage'] < 0:
self.stats['hullDamage'] = 0
return True
def repairSails(self):
if not self.stats['sailDamage']:
return False
self.stats['sailDamage'] -= config.shipyard['repairReturn']
if self.stats['sailDamage'] < 0:
self.stats['sailDamage'] = 0
return True
def canFire(self, x, y, captain):
if self.reloading:
return False
maxDist = max(captain.skills['gun'], config.captains['minRange'])
if not captain.ship.isPlayer and util.dist(self.mapX, self.mapY, x, y) > maxDist:
return False
bearing = util.bearing(self.mapX, self.mapY, x, y)
bearing = self.heading - bearing
if bearing < 0:
bearing += 180
elif bearing >= 180:
bearing -= 180
if 50 <= abs(bearing) <= 130:
return True
else:
return False
def fire(self, targetX, targetY, _type, _range):
bearing = util.bearing(self.mapX, self.mapY, targetX, targetY)
shot = Projectile(self, _type, self.mapX, self.mapY, targetX, targetY, bearing, _range, self.stats['guns'] / 2)
self.map.addEntity(shot, shot.mapX, shot.mapY)
self.reloading = True
self.coolDown = 0
self.map.trigger('showReload', self, self)
return shot
def fireCannon(self, x, y, _range):
if not self.cannonballs:
return False
self.cannonballs -= self.stats['guns'] / 2
if self.cannonballs < 0:
self.cannonballs = 0
return self.fire(x, y, 'cannon', _range)
def fireChain(self, x, y, _range):
if not self.chainshot:
return False
self.chainshot -= self.stats['guns'] / 2
if self.chainshot < 0:
self.chainshot = 0
return self.fire(x, y, 'chain', _range)
def updateCoolDown(self):
if self.reloading:
if self.coolDown >= config.ship['coolDown']:
self.reloading = False
self.coolDown = 0
self.map.trigger('hideReload', self, self)
self.coolDown += 1
class ShipPlacementError(Exception):
pass