project_files/Android-build/SDL-android-project/assets/Data/Scripts/Tracker.lua
changeset 7568 75ba91f14ed5
equal deleted inserted replaced
7566:57d343ee382f 7568:75ba91f14ed5
       
     1 -- Library for keeping track of gears in the game
       
     2 -- and running functions on them
       
     3 -- also keeps track of clans and teams
       
     4 
       
     5 local trackingTeams = false
       
     6 local resurrecting = false
       
     7 local gears = {}
       
     8 local teams = {}
       
     9 local clans = {}
       
    10 local resurrectedHogs = {}
       
    11 local gearValues = {}
       
    12 local teamValues = {}
       
    13 local clanValues = {}
       
    14 
       
    15 -- Registers when a gear is added
       
    16 function trackGear(gear)
       
    17     table.insert(gears, gear)
       
    18     if trackingTeams and GetGearType(gear) == gtResurrector then
       
    19         resurrecting = true
       
    20     elseif resurrecting and GetGearType(gear) == gtHedgehog then
       
    21         table.insert(resurrectedHogs, gear)
       
    22     end
       
    23 end
       
    24 
       
    25 -- Registers when a gear is deleted
       
    26 function trackDeletion(gear)
       
    27     gearValues[gear] = nil
       
    28     for k, g in ipairs(gears) do
       
    29         if g == gear then
       
    30             table.remove(gears, k)
       
    31             break
       
    32         end
       
    33     end
       
    34     if trackingTeams and GetGearType(gear) == gtHedgehog then
       
    35     	hogs = teams[GetHogTeamName(gear)]
       
    36         if hogs ~= nil then
       
    37             if table.maxn(hogs) == 1 then
       
    38                 hogs = nil
       
    39             else
       
    40 				for k, hog in ipairs(hogs) do
       
    41                     if hog == gear then
       
    42                         table.remove(hogs, k)
       
    43                         break
       
    44                     end
       
    45                 end
       
    46             end
       
    47         end
       
    48     elseif resurrecting and GetGearType(gear) == gtResurrector then
       
    49         for k, gear in ipairs(resurrectedHogs) do
       
    50             team = GetHogTeamName(gear)
       
    51             if teams[team] == nil then
       
    52                 teams[team] = {}
       
    53             end
       
    54             table.insert(teams[team], gear)
       
    55         end
       
    56         resurrecting = false
       
    57         resurrectedHogs = {}
       
    58     end
       
    59 end
       
    60 
       
    61 -- Start to keep track of teams
       
    62 function trackTeams()
       
    63     if not trackingTeams then
       
    64         trackingTeams = true
       
    65         for k, gear in ipairs(gears) do
       
    66             if GetGearType(gear) == gtHedgehog then
       
    67                 team = GetHogTeamName(gear)
       
    68                 if teams[team] == nil then
       
    69                     teams[team] = { gear }
       
    70                     clans[team] = GetHogClan(gear)
       
    71                 else
       
    72                     table.insert(teams[team], gear)
       
    73                 end
       
    74             end
       
    75         end
       
    76     end
       
    77 end
       
    78 
       
    79 -- Registers when a hog is hidden
       
    80 function trackHiding(gear)
       
    81     for k, g in ipairs(gears) do
       
    82         if g == gear then
       
    83             table.remove(gears, k)
       
    84             break
       
    85         end
       
    86     end
       
    87 	
       
    88     if trackingTeams then
       
    89     	hogs = teams[GetHogTeamName(gear)]
       
    90     	
       
    91         if hogs ~= nil then
       
    92             if table.maxn(hogs) == 1 then
       
    93                 hogs = nil
       
    94             else
       
    95                 for k, hog in ipairs(hogs) do
       
    96                     if hog == gear then
       
    97                         table.remove(hogs, k)
       
    98                         break
       
    99                     end
       
   100                 end
       
   101             end
       
   102         end
       
   103     end
       
   104 end
       
   105 
       
   106 -- Registers when a hog is restored
       
   107 function trackRestoring(gear)
       
   108 	table.insert(gears, gear)
       
   109 
       
   110     if trackingTeams then
       
   111         team = GetHogTeamName(gear)
       
   112         if teams[team] == nil then
       
   113             teams[team] = {}
       
   114         end
       
   115         table.insert(teams[team], gear)
       
   116     end
       
   117 end
       
   118 
       
   119 -- Get a value for a specific gear
       
   120 function getGearValue(gear, key)
       
   121     if gearValues[gear] ~= nil then
       
   122         return gearValues[gear][key]
       
   123     end
       
   124     return nil
       
   125 end
       
   126 
       
   127 -- Set a value for a specific gear
       
   128 function setGearValue(gear, key, value)
       
   129     found = false
       
   130     for id, values in pairs(gearValues) do
       
   131         if id == gear then
       
   132             values[key] = value
       
   133             found = true
       
   134         end
       
   135     end
       
   136     if not found then
       
   137         gearValues[gear] = { [key] = value }
       
   138     end
       
   139 end
       
   140 
       
   141 -- Increase a value for a specific gear
       
   142 function increaseGearValue(gear, key)
       
   143     for id, values in pairs(gearValues) do
       
   144         if id == gear then
       
   145             values[key] = values[key] + 1
       
   146         end
       
   147     end
       
   148 end
       
   149 
       
   150 -- Decrease a value for a specific gear
       
   151 function decreaseGearValue(gear, key)
       
   152     for id, values in pairs(gearValues) do
       
   153         if id == gear then
       
   154             values[key] = values[key] - 1
       
   155         end
       
   156     end
       
   157 end
       
   158 
       
   159 -- Get a value for a specific team
       
   160 function getTeamValue(team, key)
       
   161     if teamValues[team] ~= nil then
       
   162         return teamValues[team][key]
       
   163     end
       
   164     return nil
       
   165 end
       
   166 
       
   167 -- Set a value for a specific team
       
   168 function setTeamValue(team, key, value)
       
   169     found = false
       
   170     for name, values in pairs(teamValues) do
       
   171         if name == team then
       
   172             values[key] = value
       
   173             found = true
       
   174         end
       
   175     end
       
   176     if not found then
       
   177         teamValues[team] = { [key] = value }
       
   178     end
       
   179 end
       
   180 
       
   181 -- Increase a value for a specific team
       
   182 function increaseTeamValue(team, key)
       
   183     for name, values in pairs(teamValues) do
       
   184         if name == team then
       
   185             values[key] = values[key] + 1
       
   186         end
       
   187     end
       
   188 end
       
   189 
       
   190 -- Decrease a value for a specific team
       
   191 function decreaseTeamValue(team, key)
       
   192     for name, values in pairs(teamValues) do
       
   193         if name == team then
       
   194             values[key] = values[key] - 1
       
   195         end
       
   196     end
       
   197 end
       
   198 
       
   199 -- Get a value for a specific clan
       
   200 function getClanValue(clan, key)
       
   201     if clanValues[clan] ~= nil then
       
   202         return clanValues[clan][key]
       
   203     end
       
   204     return nil
       
   205 end
       
   206 
       
   207 -- Set a value for a specific clan
       
   208 function setClanValue(clan, key, value)
       
   209     found = false
       
   210     for num, values in ipairs(clanValues) do
       
   211         if num == clan then
       
   212             values[key] = value
       
   213             found = true
       
   214         end
       
   215     end
       
   216     if not found then
       
   217         clanValues[clan] = { [key] = value }
       
   218     end
       
   219 end
       
   220 
       
   221 -- Increase a value for a specific clan
       
   222 function increaseClanValue(clan, key)
       
   223     for num, values in ipairs(clanValues) do
       
   224         if num == clan then
       
   225             values[key] = values[key] + 1
       
   226         end
       
   227     end
       
   228 end
       
   229 
       
   230 -- Decrease a value for a specific clan
       
   231 function decreaseClanValue(clan, key)
       
   232     for num, values in ipairs(clanValues) do
       
   233         if num == clan then
       
   234             values[key] = values[key] - 1
       
   235         end
       
   236     end
       
   237 end
       
   238 
       
   239 -- Run a function on all tracked gears
       
   240 function runOnGears(func)
       
   241     for k, gear in ipairs(gears) do
       
   242         func(gear)
       
   243     end
       
   244 end
       
   245 
       
   246 -- Run a function on all tracked hogs
       
   247 function runOnHogs(func)
       
   248     for k, hogs in pairs(teams) do
       
   249         for m, hog in ipairs(hogs) do
       
   250             func(hog)
       
   251         end
       
   252     end
       
   253 end
       
   254 
       
   255 -- Run a function on hogs in a team
       
   256 function runOnHogsInTeam(func, team)
       
   257     if teams[team] ~= nil then
       
   258         for k, hog in ipairs(teams[team]) do
       
   259             func(hog)
       
   260         end
       
   261     end
       
   262 end
       
   263 
       
   264 -- Run a function on hogs in other teams
       
   265 function runOnHogsInOtherTeams(func, team)
       
   266     for k, hogs in pairs(teams) do
       
   267         if k ~= team then
       
   268             for m, hog in ipairs(hogs) do
       
   269                 func(hog)
       
   270             end
       
   271         end
       
   272     end
       
   273 end
       
   274 
       
   275 -- Run a function on hogs in a clan
       
   276 function runOnHogsInClan(func, clan)
       
   277     for i = 1, table.maxn(clans) do
       
   278         if clans[i] == clan then
       
   279             for k, hog in ipairs(teams[i]) do
       
   280                 func(hog)
       
   281             end
       
   282         end
       
   283     end
       
   284 end
       
   285 
       
   286 -- Run a function on hogs in other clans
       
   287 function runOnHogsInOtherClans(func, clan)
       
   288     for i = 1, table.maxn(clans) do
       
   289         if clans[i] ~= clan then
       
   290             for k, hog in ipairs(teams[i]) do
       
   291                 func(hog)
       
   292             end
       
   293         end
       
   294     end
       
   295 end