File: unit_limit_dgun.lua

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (155 lines) | stat: -rw-r--r-- 4,820 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
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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    unit_dgun_limit.lua
--  brief:   Re-implements limit dgun in Lua
--  author:  Andrea Piras
--
--  Copyright (C) 2010.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function gadget:GetInfo()
  return {
    name      = "Limit Dgun",
    desc      = "Re-implements limit dgun in Lua",
    author    = "Andrea Piras",
    date      = "August, 2010",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end

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

local enabled = tonumber(Spring.GetModOptions().limitdgun) or 0
if (enabled == 0) then
  return false
end

local dgunMapFraction = 3 -- use the modoption to define it instead?
local dgunRadiusSquared = (Game.mapSizeX*Game.mapSizeZ) / (dgunMapFraction*dgunMapFraction)
local dgunRadius = math.sqrt(dgunRadiusSquared)

if (not gadgetHandler:IsSyncedCode()) then --begin unsynced section

local glColor = gl.Color
local glDrawGroundCircle = gl.DrawGroundCircle
local glPushMatrix = gl.PushMatrix
local glPopMatrix = gl.PopMatrix
local glDeleteList = gl.DeleteList
local glCreateList = gl.CreateList
local glCallList = gl.CallList

local GetSpectatingState = Spring.GetSpectatingState
local GetTeamStartPosition = Spring.GetTeamStartPosition
local GetTeamList = Spring.GetTeamList
local GetTeamStartPosition = Spring.GetTeamStartPosition
local AreTeamsAllied = Spring.AreTeamsAllied
local GetGaiaTeamID = Spring.GetGaiaTeamID
local GetMyTeamID = Spring.GetMyTeamID
local GetTeamInfo = Spring.GetTeamInfo

local allyTeamVec = Spring.GetAllyTeamList()
local myTeamID = GetMyTeamID()
local gaiaTeamID = GetGaiaTeamID()
local circlesList

function ReGenerateDisplayList()
	if circlesList then
		glDeleteList(circlesList)
	end
	circlesList = glCreateList( function()
		for allyIndex,allyTeamID in ipairs(allyTeamVec) do
			local teamList = GetTeamList(allyTeamID)
			for _,teamID in ipairs(teamList) do
				local _,_,isDead = GetTeamInfo(teamID)
				if not isDead and teamID ~= gaiaTeamID then
					if teamID == myTeamID then
						glColor(0.0, 0.0, 1.0, 0.6) -- use blue circles for your range
					elseif AreTeamsAllied( teamID, myTeamID ) == true then -- order matters! this tells if he's allied with us, not vice-versa
						glColor(1.0, 1.0, 1.0, 0.6) -- use white circles for ally ranges
					else
						glColor(1.0, 1.0, 0.0, 0.6) -- use yellow circles for enemy ranges
					end
					local teamX,teamY,teamZ = GetTeamStartPosition(teamID)
					if teamX and teamY and teamZ then
						glDrawGroundCircle(teamX,teamY,teamZ, dgunRadius, 40)
					end
				end
			end
		end
	end)
end

function gadget:PlayerChanged() -- regenerate the display list, update even if it's not for ourself, so we can rid of unnecessary team circles
	ReGenerateDisplayList()
end

function gadget:Initialize()
	ReGenerateDisplayList()
end

function gadget:Shutdown()
	glDeleteList(circlesList)
end

function gadget:DrawWorld() -- paint dgun limit preview range circle
	if circlesList then
		glPushMatrix()
			glCallList(circlesList)
		glPopMatrix()
	end
end

else -- begin synced section

local GetTeamStartPosition = Spring.GetTeamStartPosition
local GetUnitPosition = Spring.GetUnitPosition
local CMD_MANUALFIRE = CMD.MANUALFIRE

local teamStartPosVec = {} -- cache for team starting pos

-- squared distance between 2 points in 3D cartesian space
function SqDistance(one, two)
	dx = one[1] - two[1]
	dy = one[2] - two[2]
	dz = one[3] - two[3]
	return dx*dx + dy*dy + dz*dz
end

function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions, cmdTag, synced)
	if cmdID ~= CMD_MANUALFIRE then -- pass all non-dgun commands
		return true
	end

	if not teamStartPosVec[teamID] then -- build cache if not available
		local teamX,teamY,teamZ = GetTeamStartPosition(teamID)
		if teamX and teamY and teamZ then
			teamStartPosVec[teamID] = {teamX,teamY,teamZ}
		end
	end
	local teamStartPos = teamStartPosVec[teamID]
	if not teamStartPos then -- wtf
		return true
	end

	local unitX, unitY, unitZ = GetUnitPosition(unitID)
	if not unitX or not unitY or not unitZ then -- wtf
		return true
	end
	local unitPos = {unitX,unitY,unitZ}

	-- restrict the command to the allowed area
	if SqDistance(teamStartPos,unitPos) > dgunRadiusSquared then -- unit outside of allowed range, block the command
		return false
	end

	return true
end

end -- end synced section