import time
from weapons import harpoon, torpedo

class Craft(object):
 def __init__(self,
 name, #the actual name used to talk about this craft to the user
 type, #the type of craft
 board=None, #the playing board on which this craft is placed
 hits=0,
 maxHits=0,
 orientation=None,
 isAircraft=False,
 isCarrier=False,
 destroyed=False,
 weapons=None,
 coords=None):
  self.name=name #the craft's name, used to talk to the user about this craft
  self.type=type
  self.board=board
  self.hits=hits #how many times has this craft been hit
  self.maxHits=maxHits #how many times can it be hit before it is destroyed
  self.length=maxHits #alias to maxHits for use in some functions; it just makes more sense to say craft.length sometimes
  self.isAircraft=isAircraft #set True if craft is aircraft; this is important when the computer places craft on the grid since aircraft must go on carriers
  self.isCarrier=isCarrier #also important for random ship positioning
  self.destroyed=destroyed #bool to tell if this craft is still alive or not
  #weapons is a list of weapon objects (see weapons.py)
  if weapons is None:
   self.weapons={}
  else:
   self.weapons=weapons
  #end if
  #now do same to the coords dict, which holds the craft's position as tooples of (x,y)=hit/miss (1/0)
  if coords is None:
   self.coords={}
  else:
   self.coords=coords
  #end if
 #end def

 def checkDestroyed(self):
  #checks to see if all entries in Craft c's coord list are set to 1.
  #If they all are, then this craft is destroyed, else one or more squares on this ship remain unhit
  for (i,j) in self.coords:
   if(self.coords[(i,j)]==0): return False
  #end for
  return True #if no zeros were encountered, this craft must be destroyed
 #end def

 def toString(self):
  #puts all attribs of a Craft into a string, used for sending to/from server
  str=""

 def fromString(self, str):
  #creates a Craft object from the string
#end class Craft


#now a class for each type of craft in the game
#each will subclass the Craft class, but each also has its own settings.
#For example, recons will have isAircraft=True, and each class has a unique "type" set to a lowercase string for later lookup by type.
#Any craft capable of special functions, like launching a weapon or scanning, will have those specialized methods defined in their classes.

class Battleship(Craft):
 def __init__(self, name, **kwords):
  Craft.__init__(self, name, maxHits=5, type="battleship", **kwords) #call the superclass's __init__

class Carrier(Craft):
 def __init__(self, name, **kwords):
  Craft.__init__(self, name, maxHits=6, type="carrier", isCarrier=True, **kwords) #call the superclass's __init__

class Frigate(Craft):
 def __init__(self, name, **kwords):
  weapons={"harpoon":30}
  Craft.__init__(self, name, maxHits=3, type="frigate", weapons=weapons, **kwords) #call the superclass's __init__
 #end def

 def launchMissile(self, evt):
  if(self.weapons["harpoon"]==0): self.board.speak(self.name+" is out of missiles."); return False
  self.weapons["harpoon"]+=1 #used a missile
  points=harpoon.getImpactCoords(self.board)
  if not points: self.weapons["harpoon"]+=1; return False #user clicked Cancel in the dialog, so add the missile we just removed
  self.board.cmd="hm"
  for p in points:
   self.board.fire(x=p[0], y=p[1], missile=True)
   self.board.cmd+="%"+str(p[0])+","+str(p[1])
  #end for
  self.board.turnOver=True #this player is done, so let the other player go
 #end def
#end class Frigate

class Destroyer(Craft):
 def __init__(self, name, **kwords):
  Craft.__init__(self, name, maxHits=3, type="destroyer", **kwords) #call the superclass's __init__

class PatrolBoat(Craft):
 def __init__(self, name, **kwords):
  Craft.__init__(self, name, maxHits=2, type="patrolboat", **kwords) #call the superclass's __init__

class Recon(Craft):
 def __init__(self, name, **kwords):
  Craft.__init__(self, name, maxHits=1, type="recon", isAircraft=True, **kwords) #call the superclass's __init__

class Submarine(Craft):
 def __init__(self, name, **kwords):
  weapons={"torpedo":30}
  Craft.__init__(self, name, maxHits=3, type="submarine", weapons=weapons, **kwords) #call the superclass's __init__
 #end def

 def launchTorpedo(self, evt):
  if(self.weapons["torpedo"]==0): self.board.speak(self.name+" is out of torpedos."); return False
  self.weapons["torpedo"]-=1 #used a torpedo
  points=torpedo.getImpactCoords(self.board)
  if not points: self.weapons["torpedo"]+=1; return False #user clicked Cancel in the dialog, so add the torp we just removed
  self.board.cmd="t"
  for p in points:
   if(self.board.fire(x=p[0], y=p[1], torpedo=True)): self.board.cmd+="%"+str(p[0])+","+str(p[1]); return #stop at the first hit, if any
  #end for
  self.board.speak("Torpedo finds no target.") #if we get this far w/o returning, then nothing was hit
  self.board.turnOver=True #this player is done, so let the other player go
 #end def
#end class Submarine