File: share_control.lua

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (239 lines) | stat: -rw-r--r-- 6,814 bytes parent folder | download | duplicates (8)
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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    share_control.lua
--  brief:   only allow sharing with allied teams
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function gadget:GetInfo()
  return {
    name      = "ShareControl",
    desc      = "Controls sharing of units and resources",
    author    = "trepan",
    date      = "Apr 22, 2007",
    license   = "GNU GPL, v2 or later",
    layer     = -5,
    enabled   = true  --  loaded by default?
  }
end

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

local resShare  = true  -- allow sharing resources

local unitShare = true  -- allow sharing units

local resShareEnemy  = false  -- allow sharing resources with enemies

local unitShareEnemy = false  -- allow sharing units with enemies


--
--  A team indexed list of the last frames that a unit transfer was blocked.
--  This helps to avoid filling up the console with warnings when a user tries
--  to share multiple units.
--
--
local lastRefusals = {}


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

local function AllowAction(playerID)
  if (playerID ~= 0) then
    Spring.SendMessageToPlayer(playerID, "Must be the host player")
    return false
  end
  if (not Spring.IsCheatingEnabled()) then
    Spring.SendMessageToPlayer(playerID, "Cheating must be enabled")
    return false
  end
  return true
end


local function PrintState()
  Spring.Echo('sharing units is '
              .. (unitShare and 'enabled' or 'disabled'))
  Spring.Echo('sharing resources is '
              .. (resShare and 'enabled' or 'disabled'))
  Spring.Echo('sharing units with enemies is '
              .. (unitShare and unitShareEnemy and 'enabled' or 'disabled'))
  Spring.Echo('sharing resources with enemies is '
              .. (resShare and resShareEnemy and 'enabled' or 'disabled'))
  return true
end


local function ChatControl(cmd, line, words, playerID)
  if (not AllowAction(playerID)) then
    PrintState()
    return true
  end
  local count = #words

  if (count == 0) then
    unitShare = not unitShare
    resShare  = unitShare
  elseif (count == 1) then
    if ((words[1] == '0') or (words[1] == 'none')) then
      resShare  = false
      unitShare = false
      resShareEnemy  = false
      unitShareEnemy = false
    elseif ((words[1] == '1') or (words[1] == 'ally')) then
      resShare  = true
      unitShare = true
      resShareEnemy  = false
      unitShareEnemy = false
    elseif ((words[1] == '2') or (words[1] == 'full')) then
      resShare  = true
      unitShare = true
      resShareEnemy  = true
      unitShareEnemy = true
    elseif (words[1] == 'r') then
      resShare  = not resShare
    elseif (words[1] == 'u') then
      unitShare  = not unitShare
    elseif (words[1] == 'e') then
      resShareEnemy  = not resShareEnemy
      unitShareEnemy = not unitShareEnemy
    end
  elseif (count == 2) then
    if (words[1] == 'e') then
      if (words[2] == '0') then
        resShareEnemy  = false
        unitShareEnemy = false
      elseif (words[2] == '1') then
        resShareEnemy  = true
        unitShareEnemy = true
      end
    elseif (words[1] == 'r') then
      if (words[2] == '0') then
        resShare = false
      elseif (words[2] == '1') then
        resShare = true
      elseif (words[2] == 'e') then
        resShareEnemy = not resShareEnemy
      end
    elseif (words[1] == 'u') then
      if (words[2] == '0') then
        unitShare = false
      elseif (words[2] == '1') then
        unitShare = true
      elseif (words[2] == 'e') then
        unitShareEnemy = not unitShareEnemy
      end
    end
  elseif (count == 3) then
    if (words[1] == 'r') then
      if (words[2] == 'e') then
        if (words[3] == '0') then
          resShareEnemy = false
        elseif (words[3] == '1') then
          resShareEnemy = true
        end
      end
    elseif (words[1] == 'u') then
      if (words[2] == 'e') then
        if (words[3] == '0') then
          unitShareEnemy = false
        elseif (words[3] == '1') then
          unitShareEnemy = true
        end
      end
    end
  end

  PrintState()
  return true
end


function gadget:Initialize()
  if (not gadgetHandler:IsSyncedCode()) then
    gadgetHandler:RemoveGadget()
    return
  end
  local cmd, help
  
  cmd  = "sharectrl"
  local h = ''
  h = h..     ' [ "none" | "ally" | "full"]:  basic sharing modes\n'
  h = h..cmd..' <"u"|"r"> ["e"] [0|1]:  finer sharing control\n'
  h = h..'  u: unit sharing\n'
  h = h..'  r: resource sharing\n'
  h = h..'  e: enemy mode\n'
  
--  h = h..cmd..' [0|1]:  control unit and resource sharing\n'
--  h = h..cmd..' res [0|1]:  control resource sharing\n'
--  h = h..cmd..' unit [0|1]:  control unit sharing\n'
--  h = h..cmd..' enemy [0|1]:  control enemy unit and resource sharing\n'
--  h = h..cmd..' enemy res [0|1]:  control enemy resource sharing\n'
  help = h
  gadgetHandler:AddChatAction(cmd, ChatControl, help)
  Script.AddActionFallback(cmd .. ' ', help)
end


function gadget:Shutdown()
  gadgetHandler:RemoveChatAction("sharectrl", ChatControl)
end


function gadget:AllowResourceTransfer(oldTeam, newTeam, type, amount)
  if (resShare) then
    if (resShareEnemy or Spring.AreTeamsAllied(oldTeam, newTeam)) then
      return true
    else
      Spring.SendMessageToTeam(oldTeam, "Can not give resources to enemies")
    end
  else
    Spring.SendMessageToTeam(oldTeam, "Resource sharing has been disabled")
  end
  return false
end


local function AddRefusal(team, msg)
  local frameNum = Spring.GetGameFrame()
  local lastRefusal = lastRefusals[team]
  if ((not lastRefusal) or (lastRefusal ~= frameNum)) then
    lastRefusals[team] = frameNum
    Spring.SendMessageToTeam(team, msg)
  end
end


function gadget:AllowUnitTransfer(unitID, unitDefID, oldTeam, newTeam, capture)
  if (capture) then
    return true
  end

  if (Spring.IsCheatingEnabled()) then
    return true
  end

  if (not unitShare) then
    AddRefusal(oldTeam, "Unit sharing has been disabled")
    return false
  end
    
  if (unitShareEnemy or Spring.AreTeamsAllied(oldTeam, newTeam)) then
    return true
  end

  AddRefusal(oldTeam, "Can not give units to enemies")
  return false
end


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