File: unit_metal_maker.lua

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (130 lines) | stat: -rw-r--r-- 3,960 bytes parent folder | download | duplicates (7)
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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    unit_metal_maker.lua
--  brief:   controls metal makers to minimize energy stalls
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:GetInfo()
  return {
    name      = "MetalMakers",
    desc      = "Controls metal makers to minimize energy stalls",
    author    = "trepan",
    date      = "Jan 8, 2007",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

-- Automatically generated local definitions

local CMD_ONOFF            = CMD.ONOFF
local spGetMyTeamID        = Spring.GetMyTeamID
local spGetTeamResources   = Spring.GetTeamResources
local spGetTeamUnits       = Spring.GetTeamUnits
local spGetUnitDefID       = Spring.GetUnitDefID
local spGiveOrderToUnitMap = Spring.GiveOrderToUnitMap


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local  ON_METAL_LIMIT  = 0.8
local OFF_METAL_LIMIT  = 0.9
local  ON_ENERGY_LIMIT = 0.7
local OFF_ENERGY_LIMIT = 0.3
local metalMakers = {}

local currentState = true


--------------------------------------------------------------------------------

local function SetMetalMakers(state)
  currentState = state
  local numState = currentState and 1 or 0
  spGiveOrderToUnitMap(metalMakers, CMD_ONOFF, { numState }, {} )
end


--------------------------------------------------------------------------------

function widget:Initialize()
  local units = spGetTeamUnits(spGetMyTeamID())
  for _,uid in ipairs(units) do
    local udid = spGetUnitDefID(uid)
    local ud = UnitDefs[udid]
    if (ud.isBuilding) and (ud.onOffable) and (ud.makesMetal > 0) and (ud.energyUpkeep > 0) then
      metalMakers[uid] = true
      print("Added metal maker: "..uid)
    end
  end
  SetMetalMakers(true)
end


function widget:Update(deltaTime)
  local mNow, mMax = spGetTeamResources(spGetMyTeamID(), "metal")
  local eNow, eMax = spGetTeamResources(spGetMyTeamID(), "energy")
  local mFrac = (mNow / mMax)
  local eFrac = (eNow / eMax)
  
  if (currentState) then
    if ((eFrac < OFF_ENERGY_LIMIT) or (mFrac > OFF_METAL_LIMIT)) then
      SetMetalMakers(false)
    end
  else 
    if ((eFrac > ON_ENERGY_LIMIT) and (mFrac < ON_METAL_LIMIT)) then
      SetMetalMakers(true)
    end
  end
end


function widget:UnitFinished(unitID, unitDefID, unitTeam)
  if (unitTeam ~= spGetMyTeamID()) then
    return
  end
  local ud = UnitDefs[unitDefID]
  if (ud.isBuilding) and (ud.onOffable) and (ud.makesMetal > 0) and (ud.energyUpkeep > 0) then
    print("Added metal maker: "..unitID)
    metalMakers[unitID] = true
    -- set the new unit to our currentState
    local tmpMakers = metalMakers
    metalMakers = { [unitID] = true }
    SetMetalMakers(currentState)
    metalMakers = tmpMakers
  end
end


function widget:UnitDestroyed(unitID, unitDefID, unitTeam)
  if (metalMakers[unitID]) then
    print("Removed metal maker: "..unitID)
  end
  metalMakers[unitID] = nil
end


function widget:UnitTaken(unitID, unitDefID, unitTeam, newTeam)
  widget:UnitDestroyed(unitID, unitDefID)
end


function widget:UnitGiven(unitID, unitDefID, unitTeam, oldTeam)
  widget:UnitFinished(unitID, unitDefID, unitTeam)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------