share/hedgewars/Data/Scripts/Multiplayer/Mutant.lua
changeset 13220 1c390781f72f
parent 13219 b229b3259a76
child 13643 690cc84e9fd6
equal deleted inserted replaced
13219:b229b3259a76 13220:1c390781f72f
    58 local timer=0
    58 local timer=0
    59 
    59 
    60 local winScore = 15
    60 local winScore = 15
    61 local hogsLimit = 1
    61 local hogsLimit = 1
    62 
    62 
    63 local teams = {}
    63 local teamsDead = {}
    64 
    64 
    65 local circles = {}
    65 local circles = {}
    66 local circleFrame = -1
    66 local circleFrame = -1
    67 
    67 
    68 -- Variables for custom achievements
    68 -- Variables for custom achievements
    80 -- Most skips
    80 -- Most skips
    81 local recordSkips = 0
    81 local recordSkips = 0
    82 local recordSkipsHogName = nil
    82 local recordSkipsHogName = nil
    83 local recordSkipsTeamName = nil
    83 local recordSkipsTeamName = nil
    84 
    84 
    85 -- Most crates collected 
    85 -- Most crates collected
    86 local recordCrates = 0
    86 local recordCrates = 0
    87 local recordCratesHogName = nil
    87 local recordCratesHogName = nil
    88 local recordCratesTeamName = nil
    88 local recordCratesTeamName = nil
    89 
    89 
    90 -- Most deaths
    90 -- Most deaths
   161     runOnHogs(saveStuff)
   161     runOnHogs(saveStuff)
   162 
   162 
   163     hogLimitHit = false
   163     hogLimitHit = false
   164     for i=0 , TeamsCount - 1 do
   164     for i=0 , TeamsCount - 1 do
   165         cnthhs = 0
   165         cnthhs = 0
   166         runOnHogsInTeam(limitHogs, teams[i])
   166         runOnHogsInTeam(limitHogs, GetTeamName(i))
   167     end
   167     end
   168     if hogLimitHit then
   168     if hogLimitHit then
   169         WriteLnToChat(loc("Only one hog per team allowed! Excess hogs will be removed."))
   169         WriteLnToChat(loc("Only one hog per team allowed! Excess hogs will be removed."))
   170     end
   170     end
   171     showStartingInfo()
   171     showStartingInfo()
   239     end
   239     end
   240 
   240 
   241     checkScore()
   241     checkScore()
   242 
   242 
   243     for i=0, TeamsCount-1 do
   243     for i=0, TeamsCount-1 do
   244         SendStat(siClanHealth, getTeamValue(teams[i], "Score"), teams[i])
   244         SendStat(siClanHealth, getTeamValue(GetTeamName(i), "Score"), GetTeamName(i))
   245     end
   245     end
   246 
   246 
   247     giveWeapons(CurrentHedgehog)
   247     giveWeapons(CurrentHedgehog)
   248     drawCircles()
   248     drawCircles()
   249     setAIHints()
   249     setAIHints()
   368     SetHealth(gear, 0)
   368     SetHealth(gear, 0)
   369 end
   369 end
   370 
   370 
   371 function renderScores()
   371 function renderScores()
   372     for i=0, TeamsCount-1 do
   372     for i=0, TeamsCount-1 do
   373         if teams[i]~= nil then
   373         local name = GetTeamName(i)
   374             SetTeamLabel(teams[i], string.format(loc("%d | %d"), getTeamValue(teams[i], "Score"), getTeamValue(teams[i], "DeadHogs")))
   374         SetTeamLabel(name, string.format(loc("%d | %d"), getTeamValue(name, "Score"), getTeamValue(name, "DeadHogs")))
   375         end
   375     end
       
   376 end
       
   377 
       
   378 function createEndGameStats()
       
   379     SendStat(siGraphTitle, loc("Score graph"))
       
   380 
       
   381     local teamsSorted = {}
       
   382     for i=0, TeamsCount-1, 1 do
       
   383         teamsSorted[i+1] = GetTeamName(i)
       
   384     end
       
   385 
       
   386     -- Achievements stuff
       
   387     local achievements = 0
       
   388     --- Most kills per turn
       
   389     if recordKills >= 3 then
       
   390         SendStat(siMaxStepKills, string.format("%d %s (%s)", recordKills, recordKillsHogName, recordKillsTeamName))
       
   391         achievements = achievements + 1
       
   392     end
       
   393     --- Most crates collected
       
   394     if recordCrates >= 5 then
       
   395         SendStat(siCustomAchievement, string.format(loc("%s (%s) was the greediest hedgehog and collected %d crates."), recordCratesHogName, recordCratesTeamName, recordCrates))
       
   396         achievements = achievements + 1
       
   397     end
       
   398     --- Most suicides
       
   399     if recordSuicides >= 5 then
       
   400         SendStat(siCustomAchievement, string.format(loc("%s (%s) hate life and suicided %d times."), recordSuicidesHogName, recordSuicidesTeamName, recordSuicides))
       
   401         achievements = achievements + 1
       
   402     end
       
   403     --- Most deaths
       
   404     if recordDeaths >= 5 then
       
   405         SendStat(siCustomAchievement, string.format(loc("Poor %s (%s) died %d times."), recordDeathsHogName, recordDeathsTeamName, recordDeaths))
       
   406         achievements = achievements + 1
       
   407     end
       
   408     --- Most skips
       
   409     if recordSkips >= 3 then
       
   410         SendStat(siMaxTurnSkips, string.format("%d %s (%s)", recordSkips, recordSkipsHogName, recordSkipsTeamName))
       
   411         achievements = achievements + 1
       
   412     end
       
   413     --- Total damage
       
   414     if totalDamage >= 900 then
       
   415         SendStat(siCustomAchievement, string.format(loc("%d damage was dealt in this game."), totalDamage))
       
   416         achievements = achievements + 1
       
   417     end
       
   418     --- Total kills
       
   419     if totalKills >= 20 or (achievements <= 0 and totalKills >= 1) then
       
   420         SendStat(siKilledHHs, tostring(totalKills))
       
   421         achievements = achievements + 1
       
   422     end
       
   423 
       
   424     -- Score and stats stuff
       
   425     local showScore = ""
       
   426     table.sort(teamsSorted, function(team1, team2) return getTeamValue(team1, "Score") > getTeamValue(team2, "Score") end)
       
   427     for i=1, TeamsCount do
       
   428         SendStat(siPointType, loc("point(s)"))
       
   429         local score = getTeamValue(teamsSorted[i], "Score")
       
   430         local deaths = getTeamValue(teamsSorted[i], "DeadHogs")
       
   431         SendStat(siPlayerKills, score, teamsSorted[i])
       
   432 
       
   433         showScore = showScore .. string.format(loc("%s: %d (deaths: %d)"), teamsSorted[i], score, deaths) .. "|"
       
   434     end
       
   435 
       
   436     if getTeamValue(teamsSorted[1], "Score") == getTeamValue(teamsSorted[2], "Score") then
       
   437         -- The first two teams have the same score! Round is drawn.
       
   438         return nil
       
   439     else
       
   440 
       
   441     ShowMission(loc("Mutant"),
       
   442         loc("Final result"),
       
   443         string.format(loc("Winner: %s"), teamsSorted[1]) .. "| |" .. loc("Scores:") .. " |" ..
       
   444         showScore, 0, 15000)
       
   445 
       
   446         -- return winning team
       
   447         return teamsSorted[1]
   376     end
   448     end
   377 end
   449 end
   378 
   450 
   379 function checkScore()
   451 function checkScore()
   380 local showScore = ""
       
   381 local lowest_score_team = nil
   452 local lowest_score_team = nil
   382 local min_score=nil
   453 local min_score=nil
   383 local winTeam = nil
   454 local winTeam = nil
   384 
   455 
   385 local only_low_score = true
   456 local only_low_score = true
   386 
   457 
   387     for i=0, TeamsCount-1 do
   458     for i=0, TeamsCount-1 do
   388         if teams[i]~=nil then
   459         local teamName = GetTeamName(i)
   389             local curr_score = getTeamValue(teams[i], "Score")
   460         if not teamsDead[teamName] then
   390 
   461             local curr_score = getTeamValue(teamName, "Score")
   391             runOnHogsInTeam(removeFeeder, teams[i])
   462 
   392 
   463             runOnHogsInTeam(removeFeeder, teamName)
   393             showScore = showScore .. string.format(loc("%s: %d (deaths: %d)"), teams[i], curr_score, getTeamValue(teams[i], "DeadHogs")) .. "|"
       
   394 
   464 
   395             if curr_score >= winScore then
   465             if curr_score >= winScore then
   396                 gameOver = true
   466                 gameOver = true
   397                 winTeam = teams[i]
   467                 winTeam = teamName
   398             end
   468             end
   399 
   469 
   400             if min_score==nil then
   470             if min_score==nil then
   401                 min_score= curr_score
   471                 min_score= curr_score
   402                 lowest_score_team = teams[i]
   472                 lowest_score_team = teamName
   403             else
   473             else
   404                 if curr_score <= min_score then
   474                 if curr_score <= min_score then
   405                     if curr_score == min_score then
   475                     if curr_score == min_score then
   406                         if getTeamValue(teams[i], "DeadHogs") == getTeamValue(lowest_score_team, "DeadHogs") then
   476                         if getTeamValue(teamName, "DeadHogs") == getTeamValue(lowest_score_team, "DeadHogs") then
   407                             only_low_score = false
   477                             only_low_score = false
   408                         else
   478                         else
   409                             if getTeamValue(teams[i], "DeadHogs") > getTeamValue(lowest_score_team, "DeadHogs") then
   479                             if getTeamValue(teamName, "DeadHogs") > getTeamValue(lowest_score_team, "DeadHogs") then
   410                                 lowest_score_team = teams[i]
   480                                 lowest_score_team = teamName
   411                             end
   481                             end
   412                             only_low_score = true
   482                             only_low_score = true
   413                         end
   483                         end
   414 
   484 
   415                     else
   485                     else
   416                         min_score= curr_score
   486                         min_score= curr_score
   417                         lowest_score_team = teams[i]
   487                         lowest_score_team = teamName
   418                         only_low_score = true
   488                         only_low_score = true
   419                     end
   489                     end
   420                 end
   490                 end
   421             end
   491             end
   422         end
   492         end
   423     end
   493     end
   424 
   494 
   425     if gameOver then
   495     if gameOver then
   426         EndTurn(true)
   496         EndTurn(true)
   427 
   497 
   428         teamsSorted = {}
       
   429  
       
   430         for i=0, TeamsCount-1 do
   498         for i=0, TeamsCount-1 do
   431             if teams[i]~=winTeam then
   499             local teamName = GetTeamName(i)
   432                 runOnHogsInTeam(armageddon, teams[i])
   500             if teamName~=winTeam then
   433             end
   501                 runOnHogsInTeam(armageddon, teamName)
   434             teamsSorted[i+1] = teams[i]
   502             end
   435         end
   503         end
   436 
   504 
   437         -- Achievements stuff
   505         createEndGameStats()
   438         local achievements = 0
       
   439         --- Most kills per turn
       
   440         if recordKills >= 3 then
       
   441             SendStat(siMaxStepKills, string.format("%d %s (%s)", recordKills, recordKillsHogName, recordKillsTeamName))
       
   442             achievements = achievements + 1
       
   443         end
       
   444         --- Most crates collected
       
   445         if recordCrates >= 5 then
       
   446             SendStat(siCustomAchievement, string.format(loc("%s (%s) was the greediest hedgehog and collected %d crates."), recordCratesHogName, recordCratesTeamName, recordCrates))
       
   447             achievements = achievements + 1
       
   448         end
       
   449         --- Most suicides
       
   450         if recordSuicides >= 5 then
       
   451             SendStat(siCustomAchievement, string.format(loc("%s (%s) hate life and suicided %d times."), recordSuicidesHogName, recordSuicidesTeamName, recordSuicides))
       
   452             achievements = achievements + 1
       
   453         end
       
   454         --- Most deaths
       
   455         if recordDeaths >= 5 then
       
   456             SendStat(siCustomAchievement, string.format(loc("Poor %s (%s) died %d times."), recordDeathsHogName, recordDeathsTeamName, recordDeaths))
       
   457             achievements = achievements + 1
       
   458         end
       
   459         --- Most skips
       
   460         if recordSkips >= 3 then
       
   461             SendStat(siMaxTurnSkips, string.format("%d %s (%s)", recordSkips, recordSkipsHogName, recordSkipsTeamName))
       
   462             achievements = achievements + 1
       
   463         end
       
   464         --- Total damage 
       
   465         if totalDamage >= 900 then
       
   466             SendStat(siCustomAchievement, string.format(loc("%d damage was dealt in this game."), totalDamage))
       
   467             achievements = achievements + 1
       
   468         end
       
   469         --- Total kills
       
   470         if totalKills >= 20 or achievements <= 0 then
       
   471             SendStat(siKilledHHs, tostring(totalKills))
       
   472             achievements = achievements + 1
       
   473         end
       
   474 
       
   475         -- Score and stats stuff
       
   476         table.sort(teamsSorted, function(team1, team2) return getTeamValue(team1, "Score") < getTeamValue(team2, "Score") end)
       
   477         for i=TeamsCount, 1, -1 do
       
   478             SendStat(siPointType, loc("point(s)"))
       
   479             SendStat(siPlayerKills, getTeamValue(teamsSorted[i], "Score"), teamsSorted[i])
       
   480         end
       
   481 
       
   482         ShowMission(    loc("Mutant"),
       
   483                         loc("Final result"),
       
   484                         string.format(loc("Winner: %s"), winTeam) .. "| |" .. loc("Scores:") .. " |" ..
       
   485                         showScore, 0, 15000)
       
   486     else
   506     else
   487 
   507 
   488     if only_low_score then
   508     if only_low_score then
   489         runOnHogsInTeam(setFeeder, lowest_score_team)
   509         runOnHogsInTeam(setFeeder, lowest_score_team)
   490     end
   510     end
   558     PlaySound(sndSuddenDeath)
   578     PlaySound(sndSuddenDeath)
   559 end
   579 end
   560 
   580 
   561 function teamScan()
   581 function teamScan()
   562 
   582 
   563         for i=0, TeamsCount-1 do --nil filling
   583         for j=0, TeamsCount-1 do
   564         teams[i]=nil
   584             teamName = GetTeamName(j)
   565         end
   585             teamsDead[teamName] = false
   566 
   586             setTeamValue(teamName, "Score",0)
   567         for i=0, #hhs do
   587             setTeamValue(teamName, "Suicides",0)
   568             for j=0, TeamsCount-1 do
   588             setTeamValue(teamName, "Skips",0)
   569                 if teams[j] ==nil and hhs[i]~=nil then
   589             setTeamValue(teamName, "Crates",0)
   570                 teams[j] = GetHogTeamName(hhs[i])
   590             setTeamValue(teamName, "DeadHogs",0)
   571                 setTeamValue(teams[j], "Score",0)
       
   572                 setTeamValue(teams[j], "Suicides",0)
       
   573                 setTeamValue(teams[j], "Skips",0)
       
   574                 setTeamValue(teams[j], "Crates",0)
       
   575                 setTeamValue(teams[j], "DeadHogs",0)
       
   576                 break
       
   577                 end
       
   578 
       
   579                 if teams[j] == GetHogTeamName(hhs[i]) then
       
   580                     break
       
   581                 end
       
   582             end
       
   583         end
   591         end
   584 
   592 
   585         renderScores()
   593         renderScores()
   586 
   594 
   587         ---***---
   595         ---***---
   710         trackGear(gear)
   718         trackGear(gear)
   711         hhs[numhhs] = gear
   719         hhs[numhhs] = gear
   712         numhhs = numhhs + 1
   720         numhhs = numhhs + 1
   713         SetEffect(gear, heResurrectable, 1)
   721         SetEffect(gear, heResurrectable, 1)
   714     elseif GetGearType(gear) == gtATFinishGame then
   722     elseif GetGearType(gear) == gtATFinishGame then
   715         SendStat(siGraphTitle, loc("Score graph"))
   723         if not gameOver then
       
   724             local winner = createEndGameStats()
       
   725             if winner then
       
   726                 SendStat(siGameResult, string.format(loc("%s wins!"), winner))
       
   727                 AddCaption(string.format(loc("%s wins!"), winner), 0xFFFFFFFF, capgrpGameState)
       
   728             end
       
   729             gameOver = true
       
   730         end
   716     end
   731     end
   717 end
   732 end
   718 
   733 
   719 function checkEmptyTeam (teamName)
   734 function checkEmptyTeam (teamName)
   720     for i=0 , #hhs do
   735     for i=0 , #hhs do
   745         hhs[#hhs] = nil
   760         hhs[#hhs] = nil
   746 
   761 
   747         local t_name = GetHogTeamName(gear)
   762         local t_name = GetHogTeamName(gear)
   748         if checkEmptyTeam(t_name) then
   763         if checkEmptyTeam(t_name) then
   749             for i = 0, TeamsCount - 1 do
   764             for i = 0, TeamsCount - 1 do
   750                 if teams[i] == t_name then
   765                 if GetTeamName(i) == t_name then
   751                     found = i
   766                     found = i
       
   767                     teamsDead[t_name] = true
   752                     break
   768                     break
   753                 end
   769                 end
   754             end
   770             end
   755             for i = found, TeamsCount - 2 do
   771         end
   756                 teams[i] = teams[i + 1]
   772         if getGearValue(gear, "excess") ~= true and band(GetState(gear), gstDrowning) == 0 then
   757             end
       
   758             teams[TeamsCount - 1] = nil
       
   759             TeamsCount = TeamsCount - 1
       
   760         end
       
   761         if getGearValue(gear, "excess") ~= true then
       
   762             AddVisualGear(GetX(gear), GetY(gear), vgtBigExplosion, 0, false)
   773             AddVisualGear(GetX(gear), GetY(gear), vgtBigExplosion, 0, false)
   763         end
   774         end
   764         trackDeletion(gear)
   775         trackDeletion(gear)
   765     elseif GetGearType(gear) == gtCase then
   776     elseif GetGearType(gear) == gtCase then
   766         -- Check if a crate has been collected
   777         -- Check if a crate has been collected