Advertisement
NickG

basic_powerplant logic

Nov 17th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.28 KB | None | 0 0
  1. function ENT:Think()
  2.     self.Entity:NextThink(CurTime()+1)
  3.     local EmptySlots = 0
  4.  
  5.     --Invalidate entities that don't exist and count up the empty slots
  6.     for i=1, self.PowerSlots, 1 do
  7.         if not IsValid(self.PoweredEntities[i]) then
  8.             self.PoweredEntities[i] = nil
  9.             EmptySlots = EmptySlots + 1
  10.         end
  11.     end
  12.     self.SlotsUsed = self.PowerSlots - EmptySlots
  13.  
  14.     --Move existing entities to the end of the PoweredEntities table
  15.     i=self.PowerSlots
  16.     TempTable = {}
  17.     for key, value in pairs(self.PoweredEntities) do
  18.         TempTable[i] = value
  19.         i = i - 1
  20.     end
  21.     self.PoweredEntities = {}
  22.     for key, value in pairs(TempTable) do
  23.         self.PoweredEntities[key] = value
  24.     end
  25.  
  26.     --Insert new entities into the PoweredEntities table
  27.     if self.SlotsUsed < self.PowerSlots then
  28.         i = 1
  29.         local NewEntityList = ents.FindInSphere(self:GetPos(), self.PowerDist)
  30.         for key, value in pairs(NewEntityList) do
  31.             if value.PowerUsage != nil and self.SlotsUsed + value.PowerUsage <= self.PowerSlots and not value:IsPowered() then
  32.                 for j=1, value.PowerUsage, 1 do
  33.                     self.PoweredEntities[i] = value
  34.                     self.SlotsUsed = self.SlotsUsed + 1
  35.                     i = i + 1
  36.                 end
  37.             end
  38.         end
  39.     end
  40.  
  41.     --Power the entities in the PoweredEntities table
  42.     for key, value in pairs(self.PoweredEntities) do
  43.         value:Power()
  44.     end
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement