Commits

Andrew Peterson committed 6e84038

Add rate limiting to the ship factory.

Eventually this value will be a combination of the factory speed and the complexity of the ship template being built.

  • Participants
  • Parent commits 9fb1826
  • Branches dev

Comments (0)

Files changed (3)

File equip_db.dat

 +tool.id,name,rank,size,power,range
 1007,"Mining Laser",1,100,10,200
 
-+factory.id,name,rank,size
-1008,"Ship Factory",1,1000
++factory.id,name,rank,size,rate
+1008,"Ship Factory",1,1000,10.0

File example_scripts/mship_ai.py

 # Just construct a new ship
 def createShip():
 	factory = ship.getEquipment("factory")
-	new_id = factory.constructShip("ship_ai")
-	data.children.append(new_id)
+	if factory.canBuild():
+		new_id = factory.constructShip("ship_ai")
+		data.children.append(new_id)
 
-	# Send the ship undocking information.
-	comm.send(new_id, ("mship", "id"))
-	ship.log("Contructed new ship %d" % new_id)
+		# Send the ship undocking information.
+		comm.send(new_id, ("mship", "id"))
+		ship.log("Contructed new ship %d" % new_id)
 #end createShip
 
 # Find a task for a ship(c_id) to perform.
 
 
 # Maintain a certain number of children for an army
-if len(data.children) < data.MAX_ARMY:
+if len(data.children) < data.MAX_ARMY and ship.getEquipment("factory").canBuild():
 	ship.log("Autocreating ship %d/%d" % (len(data.children), data.MAX_ARMY))
 	createShip()
 

File pyshipcommand/equipment.py

     from target import TargetPosition
 
     TYPE = Type.FACTORY
+    REQ_DATA = Equipment.REQ_DATA | {'rate'}
 
     def __init__(self, equip_data):
         Equipment.__init__(self, equip_data)
         self._cvector = primitives.Vector(-100, 100)
         self._attrs = None
+        self._reload = 0.0
+
+    def getRate(self):
+        return self._data.rate
 
     def setShipTemplate(self, attrs):
         self._attrs = attrs
 
+    def canBuild(self):
+        return self._reload <= 0.0
+
     def constructShip(self, script_name):
-        if not self._attrs:
+        if not self._attrs or not self.canBuild():
             return None
 
         sm = self._ship._getMgr()
         ship.setTarget(self.TargetPosition(ship.pos + self._cvector))
         ship.setActiveScript(script_name)
 
+        self._reload = self.getRate()
+
         return ship_id
+
+    def _update(self, dt):
+        self._reload = (self._reload - dt if self._reload > 0.0 else 0.0)
+
 #end ShipFactory
 
 
 
         return equip_dict
 
-
 #end EquipmentManager