share/hedgewars/Data/Maps/TrophyRace/map.lua
changeset 13012 d82fec121f31
parent 13011 c819675d4f4f
child 13643 690cc84e9fd6
equal deleted inserted replaced
13011:c819675d4f4f 13012:d82fec121f31
    10 -- store number of hedgehogs
    10 -- store number of hedgehogs
    11 local numhhs = 0
    11 local numhhs = 0
    12 
    12 
    13 -- store hedgehog gears
    13 -- store hedgehog gears
    14 local hhs = {}
    14 local hhs = {}
       
    15 
       
    16 -- count how many hogs each clan has
       
    17 local hogsByClan = {}
    15 
    18 
    16 -- store best time per team
    19 -- store best time per team
    17 local clantimes = {}
    20 local clantimes = {}
    18 
    21 
    19 -- store best times
    22 -- store best times
    77     end
    80     end
    78     
    81     
    79     for i=0, ClansCount-1 do
    82     for i=0, ClansCount-1 do
    80         clantimes[i] = 0
    83         clantimes[i] = 0
    81     end
    84     end
       
    85     SendAchievementsStatsOff()
    82 end
    86 end
    83 
    87 
    84 function onAmmoStoreInit()
    88 function onAmmoStoreInit()
    85     SetAmmo(amRope, 9, 1, 0)
    89     SetAmmo(amRope, 9, 1, 0)
    86     SetAmmo(amSkip, 9, 1, 0)
    90     SetAmmo(amSkip, 9, 1, 0)
   199             end
   203             end
   200         end
   204         end
   201     end
   205     end
   202 end
   206 end
   203 
   207 
       
   208 function WriteStats()
       
   209    if besthog then
       
   210        SendStat(siCustomAchievement, string.format(loc("The fastest hedgehog was %s from %s with a time of %.3fs."), besthogname, GetHogTeamName(besthog), besttime/1000))
       
   211    else
       
   212        SendStat(siCustomAchievement, loc("Nobody managed to finish the race. What a shame!"))
       
   213    end
       
   214 
       
   215    -- Write most skips
       
   216    local mostSkips = 2 -- a minimum skip threshold is required
       
   217    local mostSkipsTeam = nil
       
   218    for i=0, TeamsCount-1 do
       
   219       local teamName = GetTeamName(i)
       
   220       local stats = GetTeamStats(teamName)
       
   221       if stats.TurnSkips > mostSkips then
       
   222           mostSkips = stats.TurnSkips
       
   223           mostSkipsTeam = teamName
       
   224       end
       
   225    end
       
   226    if mostSkipsTeam then
       
   227        SendStat(siMaxTurnSkips, tostring(mostSkips) .. " " .. mostSkipsTeam)
       
   228    end
       
   229 end
       
   230 
   204 function onGearAdd(gear)
   231 function onGearAdd(gear)
   205     if GetGearType(gear) == gtHedgehog then
   232     if GetGearType(gear) == gtHedgehog then
   206         hhs[numhhs] = gear
   233         hhs[numhhs] = gear
   207         times[numhhs] = 0
   234         times[numhhs] = 0
   208         numhhs = numhhs + 1
   235         numhhs = numhhs + 1
       
   236         local clan = GetHogClan(gear)
       
   237         if not hogsByClan[clan] then
       
   238             hogsByClan[clan] = 0
       
   239         end
       
   240         hogsByClan[clan] = hogsByClan[clan] + 1
       
   241     end
       
   242 end
       
   243 
       
   244 function areTwoOrMoreClansLeft()
       
   245     local clans = 0
       
   246     for i=0, ClansCount-1 do
       
   247         if hogsByClan[i] >= 1 then
       
   248             clans = clans + 1
       
   249         end
       
   250         if clans >= 2 then
       
   251             return true
       
   252         end
       
   253     end
       
   254     return false
       
   255 end
       
   256 
       
   257 function onGearDelete(gear)
       
   258     if GetGearType(gear) == gtHedgehog then
       
   259         local clan = GetHogClan(gear)
       
   260 
       
   261         hogsByClan[clan] = hogsByClan[clan] - 1
       
   262         if not areTwoOrMoreClansLeft() then
       
   263             WriteStats()
       
   264         end
   209     end
   265     end
   210 end
   266 end
   211 
   267 
   212 function onAchievementsDeclaration()
   268 function onAchievementsDeclaration()
   213     for team,time in pairs(bestTimes) do
   269     for team,time in pairs(bestTimes) do
   214         DeclareAchievement("rope race", team, "TrophyRace", time)
   270         DeclareAchievement("rope race", team, "TrophyRace", time)
   215     end
   271     end
   216 end
   272 end
       
   273