mirror of
https://github.com/fortressforever/fortressforever-scripts.git
synced 2025-05-30 16:30:40 +00:00
Merge branch 'master' into features/disabling-security-rewards
Conflicts: resource/FortressForever_english.txt
This commit is contained in:
commit
25009c6367
39 changed files with 540 additions and 388 deletions
|
@ -6,8 +6,10 @@
|
|||
-- This file is loaded automatically whenever a map is loaded.
|
||||
-- Do not change this file.
|
||||
-----------------------------------------------------------------------------
|
||||
local _G = getfenv(0)
|
||||
|
||||
require "util.utils"
|
||||
Class = require "util.class"
|
||||
Collection = require "util.collection"
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- defines
|
||||
|
@ -48,6 +50,61 @@ function baseclass:new (o)
|
|||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- set up pairs and ipairs for iterating the global entity list
|
||||
--
|
||||
-- Example usage:
|
||||
--
|
||||
-- for ent_id, ent in pairs(GlobalEntityList) do
|
||||
-- print(ent_id, ent)
|
||||
-- end
|
||||
--
|
||||
-- Note: The order of iteration is always arbitrary
|
||||
-----------------------------------------------------------------------------
|
||||
local GlobalEntityListIterator = function()
|
||||
local entity = GlobalEntityList:FirstEntity()
|
||||
return function()
|
||||
local cur_ent = entity
|
||||
entity = GlobalEntityList:NextEntity(cur_ent)
|
||||
if cur_ent then
|
||||
return cur_ent:GetId(), cur_ent
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local ipairs_base = ipairs
|
||||
ipairs = function(t)
|
||||
if t == GlobalEntityList then
|
||||
return GlobalEntityListIterator()
|
||||
end
|
||||
return ipairs_base(t)
|
||||
end
|
||||
|
||||
local pairs_base = pairs
|
||||
pairs = function(t)
|
||||
if t == GlobalEntityList then
|
||||
return GlobalEntityListIterator()
|
||||
end
|
||||
return pairs_base(t)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- make luabind's class_info function safer
|
||||
-- (don't crash if class_info() is called on non-luabind objects)
|
||||
-----------------------------------------------------------------------------
|
||||
local class_info_base = class_info
|
||||
class_info = function(obj)
|
||||
local obj_type = type(obj)
|
||||
if obj_type == "userdata" and getmetatable(obj).__luabind_class then
|
||||
return class_info_base(obj)
|
||||
end
|
||||
return {}
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- reset everything
|
||||
-----------------------------------------------------------------------------
|
||||
|
|
|
@ -568,7 +568,7 @@ function update_zone_text( player )
|
|||
if not zone_status then
|
||||
AddHudText( player, "defender_points_text", "#FF_Defenders", text_x, text_line1y, text_align )
|
||||
AddHudText( player, "defender_points_text2", "#ADZ_ScoreNotice", text_x, text_line2y, text_align )
|
||||
AddHudTimer( player, "defender_points_timer", current_timer +1, -1, text_x, text_line3y, text_align )
|
||||
AddHudTimer( player, "defender_points_timer", current_timer, -1, text_x, text_line3y, text_align )
|
||||
else
|
||||
AddHudText( player, "attackers_in_text", "#FF_Attackers", text_x, text_line1y, text_align )
|
||||
AddHudText( player, "attackers_in_text2", "#ADZ_AreIn", text_x, text_line2y, text_align )
|
||||
|
@ -596,7 +596,7 @@ function update_zone_text( player )
|
|||
if not zone_status then
|
||||
AddHudTextToAll( "defender_points_text", "#FF_Defenders", text_x, text_line1y, text_align )
|
||||
AddHudTextToAll( "defender_points_text2", "#ADZ_ScoreNotice", text_x, text_line2y, text_align )
|
||||
AddHudTimerToAll( "defender_points_timer", current_timer +1, -1, text_x, text_line3y, text_align )
|
||||
AddHudTimerToAll( "defender_points_timer", current_timer, -1, text_x, text_line3y, text_align )
|
||||
else
|
||||
AddHudTextToAll( "attackers_in_text", "#FF_Attackers", text_x, text_line1y, text_align )
|
||||
AddHudTextToAll( "attackers_in_text2", "#ADZ_AreIn", text_x, text_line2y, text_align )
|
||||
|
|
|
@ -375,7 +375,7 @@ function player_onchat( player, chatstring )
|
|||
func(unpack(params))
|
||||
else
|
||||
ChatToPlayer(player, "^"..CHAT_COMMAND_COLOR_ERROR.."Unexpected error while executing command")
|
||||
chat_error("Command function find error for "..command.."("..paramstring.."):".. finderror)
|
||||
chatbase_error("Command function find error for "..command.."("..paramstring.."):".. finderror)
|
||||
return true
|
||||
end
|
||||
|
||||
|
|
|
@ -785,7 +785,7 @@ function flaginfo( player_entity )
|
|||
else
|
||||
AddHudTextToAll("flag_return_text", "#AD_FlagReturnBase", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0)
|
||||
end
|
||||
AddHudTimer(player, "flag_return_timer", current_timer + 1, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign)
|
||||
AddHudTimer(player, "flag_return_timer", current_timer, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign)
|
||||
AddHudIcon(player, hudstatusicondropped, ( "cp_flag_d" ), flag_hudstatusiconx, flag_hudstatusicony, flag_hudstatusiconw, flag_hudstatusiconh, flag_hudstatusiconalign )
|
||||
elseif _G[flagname].status == 0 then
|
||||
AddHudText(player, "flag_athome", "#AD_FlagIsAt", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0)
|
||||
|
@ -802,7 +802,7 @@ function flaginfo( player_entity )
|
|||
else
|
||||
AddHudText(player, "flag_tobase_text", "#AD_FlagReturnBase", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0)
|
||||
end
|
||||
AddHudTimer(player, "flag_tobase_timer", current_timer + 1, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign)
|
||||
AddHudTimer(player, "flag_tobase_timer", current_timer, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign)
|
||||
AddHudIcon(player, hudstatusicontobase, ( "cp_flag_h" ), flag_hudstatusiconx, flag_hudstatusicony, flag_hudstatusiconw, flag_hudstatusiconh, flag_hudstatusiconalign )
|
||||
end
|
||||
|
||||
|
@ -891,7 +891,7 @@ function update_hud()
|
|||
else
|
||||
AddHudTextToAll("flag_return_text", "#AD_FlagReturnBase", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0)
|
||||
end
|
||||
AddHudTimerToAll("flag_return_timer", current_timer + 1, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign)
|
||||
AddHudTimerToAll("flag_return_timer", current_timer, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign)
|
||||
AddHudIconToAll( hudstatusicondropped, ( "cp_flag_d" ), flag_hudstatusiconx, flag_hudstatusicony, flag_hudstatusiconw, flag_hudstatusiconh, flag_hudstatusiconalign )
|
||||
elseif _G[flagname].status == 0 then
|
||||
AddHudTextToAll("flag_athome", "#AD_FlagIsAt", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0)
|
||||
|
@ -908,7 +908,7 @@ function update_hud()
|
|||
else
|
||||
AddHudTextToAll("flag_tobase_text", "#AD_FlagReturnBase", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0)
|
||||
end
|
||||
AddHudTimerToAll("flag_tobase_timer", current_timer + 1, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign)
|
||||
AddHudTimerToAll("flag_tobase_timer", current_timer, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign)
|
||||
AddHudIconToAll(hudstatusicontobase, ( "cp_flag_h" ), flag_hudstatusiconx, flag_hudstatusicony, flag_hudstatusiconw, flag_hudstatusiconh, flag_hudstatusiconalign )
|
||||
end
|
||||
|
||||
|
|
|
@ -1026,7 +1026,7 @@ function flaginfo( player_entity )
|
|||
else
|
||||
AddHudTextToAll("flag_return_text", "#AD_FlagReturnBase", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0, 2)
|
||||
end
|
||||
AddHudTimer(player, "flag_return_timer", current_timer + 1, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign, 0, 3)
|
||||
AddHudTimer(player, "flag_return_timer", current_timer, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign, 0, 3)
|
||||
AddHudIcon(player, hudstatusicondropped, ( "cp_flag_d" ), flag_hudstatusiconx, flag_hudstatusicony, flag_hudstatusiconw, flag_hudstatusiconh, flag_hudstatusiconalign )
|
||||
elseif _G[flagname].status == 0 then
|
||||
AddHudText(player, "flag_athome", "#AD_FlagIsAt", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0, 2)
|
||||
|
@ -1043,7 +1043,7 @@ function flaginfo( player_entity )
|
|||
else
|
||||
AddHudText(player, "flag_tobase_text", "#AD_FlagReturnBase", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0, 2)
|
||||
end
|
||||
AddHudTimer(player, "flag_tobase_timer", current_timer + 1, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign, 0, 3)
|
||||
AddHudTimer(player, "flag_tobase_timer", current_timer, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign, 0, 3)
|
||||
AddHudIcon(player, hudstatusicontobase, ( "cp_flag_h" ), flag_hudstatusiconx, flag_hudstatusicony, flag_hudstatusiconw, flag_hudstatusiconh, flag_hudstatusiconalign )
|
||||
end
|
||||
|
||||
|
@ -1140,7 +1140,7 @@ function update_hud()
|
|||
else
|
||||
AddHudTextToAll("flag_return_text", "#AD_FlagReturnBase", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0, 2)
|
||||
end
|
||||
AddHudTimerToAll("flag_return_timer", current_timer + 1, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign, 0, 3)
|
||||
AddHudTimerToAll("flag_return_timer", current_timer, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign, 0, 3)
|
||||
AddHudIconToAll( hudstatusicondropped, ( "cp_flag_d" ), flag_hudstatusiconx, flag_hudstatusicony, flag_hudstatusiconw, flag_hudstatusiconh, flag_hudstatusiconalign )
|
||||
elseif _G[flagname].status == 0 then
|
||||
AddHudTextToAll("flag_athome", "#AD_FlagIsAt", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0, 2)
|
||||
|
@ -1157,7 +1157,7 @@ function update_hud()
|
|||
else
|
||||
AddHudTextToAll("flag_tobase_text", "#AD_FlagReturnBase", text_hudstatusx, text_hudstatusy, text_hudstatusalign, 0, 2)
|
||||
end
|
||||
AddHudTimerToAll("flag_tobase_timer", current_timer + 1, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign, 0, 3)
|
||||
AddHudTimerToAll("flag_tobase_timer", current_timer, -1, text_hudstatusx, text_hudstatusy+8, text_hudstatusalign, 0, 3)
|
||||
AddHudIconToAll(hudstatusicontobase, ( "cp_flag_h" ), flag_hudstatusiconx, flag_hudstatusicony, flag_hudstatusiconw, flag_hudstatusiconh, flag_hudstatusiconalign )
|
||||
end
|
||||
|
||||
|
|
|
@ -54,6 +54,13 @@ function security_off( team )
|
|||
clip:SetClipFlags({ClipFlags.kClipTeamBlue})
|
||||
end
|
||||
end
|
||||
|
||||
-- add a timer for the security on HUD
|
||||
if team == "red" then
|
||||
AddHudTimerToAll( "red_sec_timer", SECURITY_LENGTH, -1, button_red.iconx, button_red.icony + 15, button_red.iconalign )
|
||||
else
|
||||
AddHudTimerToAll( "blue_sec_timer", SECURITY_LENGTH, -1, button_blue.iconx, button_blue.icony + 15, button_blue.iconalign )
|
||||
end
|
||||
end
|
||||
|
||||
-- called when security gets turned on (team is a string prefix, like "red")
|
||||
|
@ -76,6 +83,9 @@ function security_on( team )
|
|||
clip:SetClipFlags(_G[clipname].clipflags)
|
||||
end
|
||||
end
|
||||
|
||||
-- remove security timer
|
||||
RemoveHudItemFromAll( team.."_sec_timer" )
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
|
@ -241,6 +251,8 @@ blue_security_trigger = base_security_trigger:new( { team = Team.kBlue, button =
|
|||
-- Hurts
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
-- backwards compatibility
|
||||
hurt = team_only_trigger:new({})
|
||||
-- red lasers hurt blue and vice-versa
|
||||
red_security_hurt = not_red_trigger:new({})
|
||||
blue_security_hurt = not_blue_trigger:new({})
|
||||
|
|
|
@ -537,6 +537,7 @@ function baseflag:spawn()
|
|||
LogLuaEvent(0, 0, "flag_spawn","flag_name",flag:GetName())
|
||||
self.status = 0
|
||||
self:refreshStatusIcons(flag:GetName())
|
||||
flag:StartTrail(self.team)
|
||||
end
|
||||
|
||||
function baseflag:addnotouch(player_id, duration)
|
||||
|
@ -723,12 +724,12 @@ end
|
|||
function baseflag:hasanimation() return true end
|
||||
|
||||
function baseflag:gettouchsize( mins, maxs )
|
||||
mins.x = mins.x
|
||||
mins.y = mins.y
|
||||
maxs.x = maxs.x
|
||||
maxs.y = maxs.y
|
||||
mins.x = mins.x * 1.50
|
||||
mins.y = mins.y * 1.50
|
||||
maxs.x = maxs.x * 1.50
|
||||
maxs.y = maxs.y * 1.50
|
||||
mins.z = 0
|
||||
maxs.z = maxs.z * 0.65
|
||||
maxs.z = maxs.z * 0.80
|
||||
end
|
||||
|
||||
function baseflag:getphysicssize( mins, maxs )
|
||||
|
@ -756,7 +757,7 @@ function baseflag:refreshStatusIcons(flagname)
|
|||
AddHudIconToAll( self.hudstatusiconcarried, ( flagname .. "_status" ), self.hudstatusiconx, self.hudstatusicony, self.hudstatusiconw, self.hudstatusiconh, self.hudstatusiconalign )
|
||||
elseif self.status == 2 then
|
||||
AddHudTextToAll( flagname .. "location", self.droppedlocation, self.hudstatusiconx + 24, (self.hudstatusicony + self.hudstatusiconh), self.hudstatusiconalign )
|
||||
AddHudTimerToAll( flagname .. "timer", FLAG_RETURN_TIME+1, -1, self.hudstatusiconx, (self.hudstatusicony + self.hudstatusiconh), self.hudstatusiconalign )
|
||||
AddHudTimerToAll( flagname .. "timer", FLAG_RETURN_TIME, -1, self.hudstatusiconx, (self.hudstatusicony + self.hudstatusiconh), self.hudstatusiconalign )
|
||||
AddHudIconToAll( self.hudstatusicondropped, ( flagname .. "_status" ), self.hudstatusiconx, self.hudstatusicony, self.hudstatusiconw, self.hudstatusiconh, self.hudstatusiconalign )
|
||||
else
|
||||
AddHudIconToAll( self.hudstatusiconhome, ( flagname .. "_status" ), self.hudstatusiconx, self.hudstatusicony, self.hudstatusiconw, self.hudstatusiconh, self.hudstatusiconalign )
|
||||
|
|
46
maps/includes/util/class.lua
Normal file
46
maps/includes/util/class.lua
Normal file
|
@ -0,0 +1,46 @@
|
|||
-- from http://lua-users.org/wiki/SimpleLuaClasses
|
||||
|
||||
function Class(base, _ctor)
|
||||
local c = {} -- a new class instance
|
||||
if not _ctor and type(base) == 'function' then
|
||||
_ctor = base
|
||||
base = nil
|
||||
elseif type(base) == 'table' then
|
||||
-- our new class is a shallow copy of the base class!
|
||||
for i,v in pairs(base) do
|
||||
c[i] = v
|
||||
end
|
||||
c._base = base
|
||||
end
|
||||
|
||||
-- the class will be the metatable for all its objects,
|
||||
-- and they will look up their methods in it.
|
||||
c.__index = c
|
||||
|
||||
-- expose a constructor which can be called by <classname>(<args>)
|
||||
local mt = {}
|
||||
|
||||
mt.__call = function(class_tbl, ...)
|
||||
local obj = {}
|
||||
setmetatable(obj,c)
|
||||
|
||||
if c._ctor then
|
||||
c._ctor(obj,...)
|
||||
end
|
||||
return obj
|
||||
end
|
||||
|
||||
c._ctor = _ctor
|
||||
c.is_a = function(self, klass)
|
||||
local m = getmetatable(self)
|
||||
while m do
|
||||
if m == klass then return true end
|
||||
m = m._base
|
||||
end
|
||||
return false
|
||||
end
|
||||
setmetatable(c, mt)
|
||||
return c
|
||||
end
|
||||
|
||||
return Class
|
222
maps/includes/util/collection.lua
Normal file
222
maps/includes/util/collection.lua
Normal file
|
@ -0,0 +1,222 @@
|
|||
-- backwards compatibility for the Collection class
|
||||
|
||||
local Class = require "util.class"
|
||||
require "util.utils"
|
||||
|
||||
local Collection = Class(function(self, entity_or_entities)
|
||||
self.entities = totable(entity_or_entities)
|
||||
self:SetupItemsKey()
|
||||
end)
|
||||
|
||||
Collection.filters = {
|
||||
[CF.kNone] = function(entity) return true end,
|
||||
[CF.kPlayers] = function(entity) return IsPlayer(entity) end,
|
||||
[CF.kHumanPlayers] = function(entity) return IsPlayer(entity) and not CastToPlayer(entity):IsBot() end,
|
||||
[CF.kBotPlayers] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):IsBot() end,
|
||||
[CF.kPlayerScout] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kScout end,
|
||||
[CF.kPlayerSniper] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kSniper end,
|
||||
[CF.kPlayerSoldier] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kSoldier end,
|
||||
[CF.kPlayerDemoman] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kDemoman end,
|
||||
[CF.kPlayerMedic] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kMedic end,
|
||||
[CF.kPlayerHWGuy] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kHwguy end,
|
||||
[CF.kPlayerPyro] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kPyro end,
|
||||
[CF.kPlayerSpy] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kSpy end,
|
||||
[CF.kPlayerEngineer] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kEngineer end,
|
||||
[CF.kPlayerCivilian] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kCivilian end,
|
||||
[CF.kPlayerScout] = function(entity) return IsPlayer(entity) and CastToPlayer(entity):GetClass() == Player.kScout end,
|
||||
[CF.kTeams] = function(entity) return IsEntity(entity) and entity:GetTeamId() >= Team.kSpectator and entity:GetTeamId() <= Team.kGreen end,
|
||||
[CF.kTeamSpec] = function(entity) return IsEntity(entity) and entity:GetTeamId() == Team.kSpectator end,
|
||||
[CF.kTeamBlue] = function(entity) return IsEntity(entity) and entity:GetTeamId() == Team.kBlue end,
|
||||
[CF.kTeamRed] = function(entity) return IsEntity(entity) and entity:GetTeamId() == Team.kRed end,
|
||||
[CF.kTeamYellow] = function(entity) return IsEntity(entity) and entity:GetTeamId() == Team.kYellow end,
|
||||
[CF.kTeamGreen] = function(entity) return IsEntity(entity) and entity:GetTeamId() == Team.kGreen end,
|
||||
[CF.kProjectiles] = function(entity) return IsProjectile(entity) end,
|
||||
[CF.kGrenades] = function(entity) return IsGrenade(entity) end,
|
||||
[CF.kInfoScipts] = function(entity) return IsInfoScript(entity) end,
|
||||
[CF.kInfoScript_Carried] = function(entity) return IsInfoScript(entity) and CastToInfoScript(entity):IsCarried() end,
|
||||
[CF.kInfoScript_Dropped] = function(entity) return IsInfoScript(entity) and CastToInfoScript(entity):IsDropped() end,
|
||||
[CF.kInfoScript_Returned] = function(entity) return IsInfoScript(entity) and CastToInfoScript(entity):IsReturned() end,
|
||||
[CF.kInfoScript_Active] = function(entity) return IsInfoScript(entity) and CastToInfoScript(entity):IsActive() end,
|
||||
[CF.kInfoScript_Inactive] = function(entity) return IsInfoScript(entity) and CastToInfoScript(entity):IsInactive() end,
|
||||
[CF.kInfoScript_Removed] = function(entity) return IsInfoScript(entity) and CastToInfoScript(entity):IsRemoved() end,
|
||||
[CF.kTraceBlockWalls] = function(entity) return true end, -- not applicable for this type of filtering
|
||||
[CF.kBuildables] = function(entity) return IsBuildable(entity) end,
|
||||
[CF.kDispenser] = function(entity) return IsDispenser(entity) end,
|
||||
[CF.kSentrygun] = function(entity) return IsSentrygun(entity) end,
|
||||
[CF.kDetpack] = function(entity) return IsDetpack(entity) end,
|
||||
[CF.kJumpPad] = function(entity) return IsJumpPad(entity) end,
|
||||
}
|
||||
|
||||
function Collection.PassesFilter(entity, filter)
|
||||
if not filter then filter = CF.kNone end
|
||||
assert(Collection.filters[filter], "Unknown filter: "..tostring(filter))
|
||||
return Collection.filters[filter](entity)
|
||||
end
|
||||
|
||||
function Collection.PassesFilters(entity, filters)
|
||||
if not filters then return true end
|
||||
filters = totable(filters)
|
||||
for _,filter in ipairs(filters) do
|
||||
if not Collection.PassesFilter(entity, filter) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function Collection:SetupItemsKey()
|
||||
-- keep a usable reference to the base metatable
|
||||
local real_obj = setmetatable({}, getmetatable(self))
|
||||
-- add special handling of the items key
|
||||
setmetatable(self, {
|
||||
-- the items key returns an iterator
|
||||
__index = function(t, k)
|
||||
if k == "items" then
|
||||
local i = 0
|
||||
local n = self:Count()
|
||||
return function()
|
||||
i = i + 1
|
||||
if i <= n then return self.entities[i] end
|
||||
end
|
||||
end
|
||||
return real_obj[k]
|
||||
end,
|
||||
-- protect the items key from being assigned
|
||||
__newindex = function(t, k, v)
|
||||
if k == "items" then
|
||||
return
|
||||
end
|
||||
real_obj[k] = v
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
-- lua tables start at index 1
|
||||
function Collection.ToLuaIndex(collection_index)
|
||||
return collection_index+1
|
||||
end
|
||||
|
||||
-- Collection started at index 0
|
||||
function Collection.ToCollectionIndex(lua_index)
|
||||
return lua_index-1
|
||||
end
|
||||
|
||||
function Collection:AddItem(entity_or_entities)
|
||||
local entities_to_add = totable(entity_or_entities)
|
||||
|
||||
for i,entity_to_add in ipairs(entities_to_add) do
|
||||
table.insert(self.entities, entity_to_add)
|
||||
end
|
||||
end
|
||||
|
||||
function Collection:AddFiltered(entity_or_entities, filters)
|
||||
local entities_to_add = totable(entity_or_entities)
|
||||
|
||||
for i,entity_to_add in ipairs(entities_to_add) do
|
||||
if Collection.PassesFilters(entity_to_add, filters) then
|
||||
table.insert(self.entities, entity_to_add)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Collection:RemoveItem(entity_or_entities)
|
||||
local entities_to_find = totable(entity_or_entities)
|
||||
|
||||
for i,entity_to_find in ipairs(entities_to_find) do
|
||||
local i = self:FindItemIndex(entity_to_find)
|
||||
if i then
|
||||
table.remove(self.entities, Collection.ToLuaIndex(i))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Collection:RemoveAllItems()
|
||||
table.clear(self.entities)
|
||||
end
|
||||
|
||||
function Collection:Count()
|
||||
return #self.entities
|
||||
end
|
||||
|
||||
function Collection:NumItems()
|
||||
return self:Count()
|
||||
end
|
||||
|
||||
function Collection:IsEmpty()
|
||||
return self:Count() == 0
|
||||
end
|
||||
|
||||
function Collection:HasItem(entity_or_entities)
|
||||
local entities_to_find = totable(entity_or_entities)
|
||||
|
||||
for i,entity_to_find in ipairs(entities_to_find) do
|
||||
if self:FindItemIndex(entity_to_find) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- this is a strange function
|
||||
function Collection:GetItem(entity_or_entities)
|
||||
local entities_to_find = totable(entity_or_entities)
|
||||
|
||||
for i,entity_to_find in ipairs(entities_to_find) do
|
||||
local i = self:FindItemIndex(entity_to_find)
|
||||
if i then
|
||||
return self:Element(i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Collection:FindItemIndex(entity_to_find)
|
||||
for i,entity in ipairs(self.entities) do
|
||||
if entity:GetId() == entity_to_find:GetId() then
|
||||
return Collection.ToCollectionIndex(i)
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function Collection:Element(i)
|
||||
return self.entities[Collection.ToLuaIndex(i)]
|
||||
end
|
||||
|
||||
function Collection:GetByFilter(filters)
|
||||
filters = totable(filters)
|
||||
|
||||
-- optimization for players
|
||||
local players_only = false
|
||||
for _,filter in ipairs(filters) do
|
||||
if filter >= CF.kPlayers and filter <= CF.kPlayerScout then
|
||||
players_only = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if players_only then
|
||||
self:AddFiltered(GetPlayers(), filters)
|
||||
else
|
||||
for ent_id, entity in ipairs(GlobalEntityList) do
|
||||
self:AddFiltered(entity, filters)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Collection:GetByName(name_or_names, filters)
|
||||
local names_to_find = totable(name_or_names)
|
||||
filters = totable(filters)
|
||||
|
||||
for i,name_to_find in ipairs(names_to_find) do
|
||||
self:AddFiltered(GetEntitiesByName(name_to_find), filters)
|
||||
end
|
||||
end
|
||||
|
||||
function Collection:GetInSphere(entity_or_origin, radius, filters)
|
||||
filters = totable(filters)
|
||||
local origin = IsEntity(entity_or_origin) and entity_or_origin:GetOrigin() or entity_or_origin
|
||||
local ignore_walls = not table.contains(filters, CF.kTraceBlockWalls)
|
||||
self:AddFiltered(GetEntitiesInSphere(origin, radius, ignore_walls), filters)
|
||||
end
|
||||
|
||||
return Collection
|
34
maps/includes/util/utils.lua
Normal file
34
maps/includes/util/utils.lua
Normal file
|
@ -0,0 +1,34 @@
|
|||
|
||||
function table.clear(tbl)
|
||||
for k in pairs(tbl) do
|
||||
tbl[k] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function table.contains(tbl, element)
|
||||
if tbl == nil then return false end
|
||||
for _, value in pairs(tbl) do
|
||||
if value == element then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function table.contains_any(tbl, elements)
|
||||
if tbl == nil or elements == nil then return false end
|
||||
for _, value in pairs(tbl) do
|
||||
for _, element in pairs(elements) do
|
||||
if value == element then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function totable(obj)
|
||||
if obj == nil then return {} end
|
||||
if type(obj) == "table" then return obj end
|
||||
return {obj}
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue