share/hedgewars/Data/Scripts/Multiplayer/Racer.lua
changeset 13839 2fceeea62b10
parent 13685 09ea1faf97ca
child 13841 b2cc4e4e380c
equal deleted inserted replaced
13765:3d2a7c563d8e 13839:2fceeea62b10
    93 
    93 
    94 ------------------
    94 ------------------
    95 -- Got Variables?
    95 -- Got Variables?
    96 ------------------
    96 ------------------
    97 
    97 
    98 local fMod = 1000000 -- 1
       
    99 local roundLimit = 3
    98 local roundLimit = 3
   100 local roundNumber = 0
    99 local roundNumber = 0
   101 local firstClan = 10
   100 local firstClan = 10
   102 
   101 
   103 local fastX = {}
   102 local fastX = {}
   141 
   140 
   142 local cGear = nil
   141 local cGear = nil
   143 local cameraGear = nil -- gear created to center the cameera on
   142 local cameraGear = nil -- gear created to center the cameera on
   144 
   143 
   145 local bestClan = 10
   144 local bestClan = 10
   146 local bestTime = 1000000
   145 local bestTime = MAX_TURN_TIME
   147 
   146 
   148 local gameBegun = false
   147 local gameBegun = false
   149 local gameOver = false
   148 local gameOver = false
   150 local racerActive = false
   149 local racerActive = false
   151 local trackTime = 0
   150 local trackTime = 0
   284         -- make a list of individual team names
   283         -- make a list of individual team names
   285         for i = 0, (TeamsCount-1) do
   284         for i = 0, (TeamsCount-1) do
   286                 teamNameArr[i] = " "
   285                 teamNameArr[i] = " "
   287                 teamSize[i] = 0
   286                 teamSize[i] = 0
   288                 teamIndex[i] = 0
   287                 teamIndex[i] = 0
   289                 teamScore[i] = 1000000
   288                 teamScore[i] = MAX_TURN_TIME
   290         end
   289         end
   291         numTeams = 0
   290         numTeams = 0
   292 
   291 
   293         for i = 0, (numhhs-1) do
   292         for i = 0, (numhhs-1) do
   294 
   293 
   437                         bestTime = teamScore[i]
   436                         bestTime = teamScore[i]
   438                         bestClan = teamClan[i]
   437                         bestClan = teamClan[i]
   439                 end
   438                 end
   440         end
   439         end
   441 
   440 
   442         if bestTime ~= 1000000 then
   441         if bestTime ~= MAX_TURN_TIME then
   443                 bestTimeComment = string.format(loc("%.1fs"), (bestTime/1000))
   442                 bestTimeComment = string.format(loc("%.1fs"), (bestTime/1000))
   444         end
   443         end
   445 
   444 
   446         if newScore == true then
   445         if newScore == true then
   447                 if trackTime == bestTime then -- best time of the race
   446                 if trackTime == bestTime then -- best time of the race
   463                 string.format(loc("Winning time: %s"), bestTimeComment), -amSkip, 4000)
   462                 string.format(loc("Winning time: %s"), bestTimeComment), -amSkip, 4000)
   464                 PlaySound(sndHellish)
   463                 PlaySound(sndHellish)
   465         end
   464         end
   466 
   465 
   467         for i = 0, (TeamsCount-1) do
   466         for i = 0, (TeamsCount-1) do
   468                 if teamNameArr[i] ~= " " and teamScore[i] ~= 1000000 then
   467                 if teamNameArr[i] ~= " " and teamScore[i] ~= MAX_TURN_TIME then
   469                         SetTeamLabel(teamNameArr[i], string.format(loc("%.1fs"), teamScore[i]/1000))
   468                         SetTeamLabel(teamNameArr[i], string.format(loc("%.1fs"), teamScore[i]/1000))
   470                 end
   469                 end
   471         end
   470         end
   472 
   471 
   473         if bestTime == trackTime then
   472         if bestTime == trackTime then
   494 
   493 
   495         roundNumber = roundNumber + 1
   494         roundNumber = roundNumber + 1
   496 
   495 
   497         totalComment = ""
   496         totalComment = ""
   498         for i = 0, (TeamsCount-1) do
   497         for i = 0, (TeamsCount-1) do
   499                         if teamNameArr[i] ~= " " and teamScore[i] ~= 1000000 then
   498                         if teamNameArr[i] ~= " " and teamScore[i] ~= MAX_TURN_TIME then
   500                                 teamComment[i] = string.format(loc("%s: %.1fs"), teamNameArr[i], (teamScore[i]/1000)) .. "|"
   499                                 teamComment[i] = string.format(loc("%s: %.1fs"), teamNameArr[i], (teamScore[i]/1000)) .. "|"
   501                         else
   500                         else
   502                                 teamComment[i] = string.format(loc("%s: Did not finish"), teamNameArr[i]) .. "|"
   501                                 teamComment[i] = string.format(loc("%s: Did not finish"), teamNameArr[i]) .. "|"
   503                         end
   502                         end
   504                         totalComment = totalComment .. teamComment[i]
   503                         totalComment = totalComment .. teamComment[i]
   513         if roundNumber >= roundLimit then
   512         if roundNumber >= roundLimit then
   514                 -- Sort the scores for the ranking list
   513                 -- Sort the scores for the ranking list
   515                 local unfinishedArray = {}
   514                 local unfinishedArray = {}
   516                 local sortedTeams = {}
   515                 local sortedTeams = {}
   517                 local k = 1
   516                 local k = 1
       
   517                 local c = 1
       
   518                 local clanScores = {}
       
   519                 local previousClan
   518                 for i = 0, TeamsCount-1 do
   520                 for i = 0, TeamsCount-1 do
   519                         if teamScore[i] ~= 1000000 and teamNameArr[i] ~= " " then
   521                         local clan = GetTeamClan(teamNameArr[i])
       
   522                         if not clanScores[clan+1] then
       
   523 	                       clanScores[clan+1] = {}
       
   524 	                       clanScores[clan+1].index = clan
       
   525 	                       clanScores[clan+1].score = teamScore[i]
       
   526                         end
       
   527                         if teamScore[i] ~= MAX_TURN_TIME and teamNameArr[i] ~= " " then
   520                                sortedTeams[k] = {}
   528                                sortedTeams[k] = {}
   521                                sortedTeams[k].name = teamNameArr[i]
   529                                sortedTeams[k].name = teamNameArr[i]
   522                                sortedTeams[k].score = teamScore[i]
   530                                sortedTeams[k].score = teamScore[i]
       
   531 	                       sortedTeams[k].clan = clan
   523                                k = k + 1
   532                                k = k + 1
   524                         else
   533                         else
   525                                table.insert(unfinishedArray, string.format(loc("%s did not finish the race."), teamNameArr[i]))
   534                                table.insert(unfinishedArray, string.format(loc("%s did not finish the race."), teamNameArr[i]))
   526                         end
   535                         end
   527                 end
   536                 end
   528                 table.sort(sortedTeams, function(team1, team2) return team1.score < team2.score end)
   537                 table.sort(sortedTeams, function(team1, team2)
       
   538                         if team1.score == team2.score then
       
   539                                 return team1.clan < team2.clan
       
   540                         else
       
   541                                 return team1.score < team2.score
       
   542                         end
       
   543                 end)
       
   544                 table.sort(clanScores, function(clan1, clan2) return clan1.score < clan2.score end)
       
   545                 local rank = 0
       
   546                 local rankPlus = 0
       
   547                 local prevScore
       
   548                 local clanRanks = {}
       
   549                 for c = 1, #clanScores do
       
   550                         rankPlus = rankPlus + 1
       
   551                         if clanScores[c].score ~= prevScore then
       
   552                                 rank = rank + rankPlus
       
   553                                 rankPlus = 0
       
   554                         end
       
   555                         prevScore = clanScores[c].score
       
   556                         clanRanks[clanScores[c].index] = rank
       
   557                 end
   529 
   558 
   530                 -- Write all the stats!
   559                 -- Write all the stats!
   531 
       
   532                 for i = 1, #sortedTeams do
   560                 for i = 1, #sortedTeams do
   533                         SendStat(siPointType, loc("milliseconds"))
   561                         SendStat(siPointType, loc("milliseconds"))
       
   562 			SendStat(siTeamRank, tostring(clanRanks[GetTeamClan(sortedTeams[i].name)]))
   534                         SendStat(siPlayerKills, sortedTeams[i].score, sortedTeams[i].name)
   563                         SendStat(siPlayerKills, sortedTeams[i].score, sortedTeams[i].name)
   535                 end
   564                 end
   536 
   565 
   537                 if #sortedTeams >= 1 then
   566 		local roundDraw = false
   538                         SendStat(siGameResult, string.format(loc("%s wins!"), sortedTeams[1].name))
   567 		if #clanScores >= 2 and clanScores[1].score == clanScores[2].score and clanScores[1].score ~= MAX_TURN_TIME then
       
   568 			roundDraw = true
       
   569                         SendStat(siGameResult, loc("Round draw"))
       
   570                         SendStat(siCustomAchievement, loc("The teams are tied for the fastest time."))
       
   571                 elseif #sortedTeams >= 1 then
   539                         SendStat(siGameResult, string.format(loc("%s wins!"), sortedTeams[1].name))
   572                         SendStat(siGameResult, string.format(loc("%s wins!"), sortedTeams[1].name))
   540                         SendStat(siCustomAchievement, string.format(loc("%s wins with a best time of %.1fs."), sortedTeams[1].name, (sortedTeams[1].score/1000)))
   573                         SendStat(siCustomAchievement, string.format(loc("%s wins with a best time of %.1fs."), sortedTeams[1].name, (sortedTeams[1].score/1000)))
   541                         for i=1,#unfinishedArray do
   574                         for i=1,#unfinishedArray do
   542                                  SendStat(siCustomAchievement, unfinishedArray[i])
   575                                  SendStat(siCustomAchievement, unfinishedArray[i])
   543                         end
   576                         end
   544                 else
   577                 else
       
   578 			roundDraw = true
   545                         SendStat(siGameResult, loc("Round draw"))
   579                         SendStat(siGameResult, loc("Round draw"))
   546                         SendStat(siCustomAchievement, loc("Nobody managed to finish the race. What a shame!"))
   580                         SendStat(siCustomAchievement, loc("Nobody managed to finish the race. What a shame!"))
   547                         if specialPointsCount > 0 then
   581                         if specialPointsCount > 0 then
   548                                 SendStat(siCustomAchievement, loc("Maybe you should try an easier map next time."))
   582                                 SendStat(siCustomAchievement, loc("Maybe you should try an easier map next time."))
   549                         else
   583                         else
   551                         end
   585                         end
   552                 end
   586                 end
   553 
   587 
   554 		-- Kill all the losers
   588 		-- Kill all the losers
   555 		for i = 0, (numhhs-1) do
   589 		for i = 0, (numhhs-1) do
   556 			if GetHogClan(hhs[i]) ~= bestClan then
   590 			if GetHogClan(hhs[i]) ~= bestClan or roundDraw then
   557 				SetEffect(hhs[i], heResurrectable, 0)
   591 				SetEffect(hhs[i], heResurrectable, 0)
   558 				SetHealth(hhs[i],0)
   592 				SetHealth(hhs[i],0)
   559 			end
   593 			end
   560 		end
   594 		end
   561 
   595 
  1059     end
  1093     end
  1060 
  1094 
  1061     map = detectMapWithDigest()
  1095     map = detectMapWithDigest()
  1062 
  1096 
  1063     for i = 0, (numTeams-1) do
  1097     for i = 0, (numTeams-1) do
  1064         if teamScore[i] < 1000000 then
  1098         if teamScore[i] < MAX_TURN_TIME then
  1065             DeclareAchievement(raceType, teamNameArr[i], map, teamScore[i])
  1099             DeclareAchievement(raceType, teamNameArr[i], map, teamScore[i])
  1066         end
  1100         end
  1067     end
  1101     end
  1068 
  1102 
  1069     if map ~= nil and fastCount > 0 then
  1103     if map ~= nil and fastCount > 0 then