share/hedgewars/Data/Scripts/Multiplayer/TechRacer.lua
changeset 13839 2fceeea62b10
parent 13687 a8b2a5e7e9db
child 13840 219c2e588874
equal deleted inserted replaced
13765:3d2a7c563d8e 13839:2fceeea62b10
   103 
   103 
   104 local activationStage = 0
   104 local activationStage = 0
   105 local jet = nil
   105 local jet = nil
   106 portalDistance = 5000
   106 portalDistance = 5000
   107 ufoFuel = 0
   107 ufoFuel = 0
   108 local fMod = 1000000
       
   109 local roundLimit = 3
   108 local roundLimit = 3
   110 local roundNumber = 0
   109 local roundNumber = 0
   111 local firstClan = 10
   110 local firstClan = 10
   112 
   111 
   113 local fastX = {}
   112 local fastX = {}
   149 
   148 
   150 local cGear = nil
   149 local cGear = nil
   151 local cameraGear = nil
   150 local cameraGear = nil
   152 
   151 
   153 local bestClan = 10
   152 local bestClan = 10
   154 local bestTime = 1000000
   153 local bestTime = MAX_TURN_TIME
   155 
   154 
   156 local gameBegun = false
   155 local gameBegun = false
   157 local gameOver = false
   156 local gameOver = false
   158 local racerActive = false
   157 local racerActive = false
   159 local trackTime = 0
   158 local trackTime = 0
   184 	-- make a list of individual team names
   183 	-- make a list of individual team names
   185 	for i = 0, (TeamsCount-1) do
   184 	for i = 0, (TeamsCount-1) do
   186 		teamNameArr[i] = " " -- = i
   185 		teamNameArr[i] = " " -- = i
   187 		teamSize[i] = 0
   186 		teamSize[i] = 0
   188 		teamIndex[i] = 0
   187 		teamIndex[i] = 0
   189 		teamScore[i] = 1000000
   188 		teamScore[i] = MAX_TURN_TIME
   190 	end
   189 	end
   191 	numTeams = 0
   190 	numTeams = 0
   192 
   191 
   193 	for i = 0, (numhhs-1) do
   192 	for i = 0, (numhhs-1) do
   194 
   193 
   330 			bestTime = teamScore[i]
   329 			bestTime = teamScore[i]
   331 			bestClan = teamClan[i]
   330 			bestClan = teamClan[i]
   332 		end
   331 		end
   333 	end
   332 	end
   334 
   333 
   335 	if bestTime ~= 1000000 then
   334 	if bestTime ~= MAX_TURN_TIME then
   336 		bestTimeComment = string.format(loc("%.1fs"), (bestTime/1000))
   335 		bestTimeComment = string.format(loc("%.1fs"), (bestTime/1000))
   337 	end
   336 	end
   338 
   337 
   339 	if newScore == true then
   338 	if newScore == true then
   340 		if trackTime == bestTime then -- best time of the race
   339 		if trackTime == bestTime then -- best time of the race
   356 			string.format(loc("Winning time: %s"), bestTimeComment), -amSkip, 4000)
   355 			string.format(loc("Winning time: %s"), bestTimeComment), -amSkip, 4000)
   357 		PlaySound(sndHellish)
   356 		PlaySound(sndHellish)
   358 	end
   357 	end
   359 
   358 
   360 	for i = 0, (TeamsCount-1) do
   359 	for i = 0, (TeamsCount-1) do
   361 		if teamNameArr[i] ~= " " and teamScore[i] ~= 1000000 then
   360 		if teamNameArr[i] ~= " " and teamScore[i] ~= MAX_TURN_TIME then
   362 			SetTeamLabel(teamNameArr[i], string.format(loc("%.1fs"), teamScore[i]/1000))
   361 			SetTeamLabel(teamNameArr[i], string.format(loc("%.1fs"), teamScore[i]/1000))
   363 		end
   362 		end
   364 	end
   363 	end
   365 
   364 
   366 	if bestTime == trackTime then
   365 	if bestTime == trackTime then
   387 	roundNumber = roundNumber + 1
   386 	roundNumber = roundNumber + 1
   388 
   387 
   389 	local totalComment = ""
   388 	local totalComment = ""
   390 	for i = 0, (TeamsCount-1) do
   389 	for i = 0, (TeamsCount-1) do
   391 		if teamNameArr[i] ~= " " then
   390 		if teamNameArr[i] ~= " " then
   392 			if teamScore[i] ~= 1000000 then
   391 			if teamScore[i] ~= MAX_TURN_TIME then
   393 				teamComment[i] = string.format(loc("%s: %.1fs"), teamNameArr[i], (teamScore[i]/1000)) .. "|"
   392 				teamComment[i] = string.format(loc("%s: %.1fs"), teamNameArr[i], (teamScore[i]/1000)) .. "|"
   394 			else
   393 			else
   395 				teamComment[i] = string.format(loc("%s: Did not finish"), teamNameArr[i]) .. "|"
   394 				teamComment[i] = string.format(loc("%s: Did not finish"), teamNameArr[i]) .. "|"
   396 			end
   395 			end
   397 			totalComment = totalComment .. teamComment[i]
   396 			totalComment = totalComment .. teamComment[i]
   411 	if roundNumber >= roundLimit then
   410 	if roundNumber >= roundLimit then
   412 		-- Sort the scores for the ranking list
   411 		-- Sort the scores for the ranking list
   413 		local unfinishedArray = {}
   412 		local unfinishedArray = {}
   414 		local sortedTeams = {}
   413 		local sortedTeams = {}
   415 		local k = 1
   414 		local k = 1
       
   415 		local c = 1
       
   416 		local clanScores = {}
       
   417 		local previousClan
   416 		for i = 0, TeamsCount-1 do
   418 		for i = 0, TeamsCount-1 do
   417 			if teamScore[i] ~= 1000000 and teamNameArr[i] ~= " " then
   419 			local clan = GetTeamClan(teamNameArr[i])
   418 			       sortedTeams[k] = {}
   420 			if not clanScores[clan+1] then
   419 			       sortedTeams[k].name = teamNameArr[i]
   421 				clanScores[clan+1] = {}
   420 			       sortedTeams[k].score = teamScore[i]
   422 				clanScores[clan+1].index = clan
   421 			       k = k + 1
   423 				clanScores[clan+1].score = teamScore[i]
       
   424 			end
       
   425 			if teamScore[i] ~= MAX_TURN_TIME and teamNameArr[i] ~= " " then
       
   426 				sortedTeams[k] = {}
       
   427 				sortedTeams[k].name = teamNameArr[i]
       
   428 				sortedTeams[k].score = teamScore[i]
       
   429 				sortedTeams[k].clan = clan
       
   430 				k = k + 1
   422 			else
   431 			else
   423 			       table.insert(unfinishedArray, string.format(loc("%s did not finish the race."), teamNameArr[i]))
   432 				table.insert(unfinishedArray, string.format(loc("%s did not finish the race."), teamNameArr[i]))
   424 			end
   433 			end
   425 		end
   434 		end
   426 		table.sort(sortedTeams, function(team1, team2) return team1.score < team2.score end)
   435 		table.sort(sortedTeams, function(team1, team2)
       
   436 			if team1.score == team2.score then
       
   437 				return team1.clan < team2.clan
       
   438 			else
       
   439 				return team1.score < team2.score
       
   440 			end
       
   441 		end)
       
   442 		table.sort(clanScores, function(clan1, clan2) return clan1.score < clan2.score end)
       
   443 		local rank = 0
       
   444 		local rankPlus = 0
       
   445 		local prevScore
       
   446 		local clanRanks = {}
       
   447 		for c = 1, #clanScores do
       
   448 			rankPlus = rankPlus + 1
       
   449 			if clanScores[c].score ~= prevScore then
       
   450 				rank = rank + rankPlus
       
   451 				rankPlus = 0
       
   452 			end
       
   453 			prevScore = clanScores[c].score
       
   454 			clanRanks[clanScores[c].index] = rank
       
   455 		end
   427 
   456 
   428 		-- Write all the stats!
   457 		-- Write all the stats!
   429 
       
   430 		for i = 1, #sortedTeams do
   458 		for i = 1, #sortedTeams do
   431 			SendStat(siPointType, loc("milliseconds"))
   459 			SendStat(siPointType, loc("milliseconds"))
       
   460 			SendStat(siTeamRank, tostring(clanRanks[GetTeamClan(sortedTeams[i].name)]))
   432 			SendStat(siPlayerKills, sortedTeams[i].score, sortedTeams[i].name)
   461 			SendStat(siPlayerKills, sortedTeams[i].score, sortedTeams[i].name)
   433 		end
   462 		end
   434 
   463 
   435 		if #sortedTeams >= 1 then
   464 		local roundDraw = false
       
   465 		if #clanScores >= 2 and clanScores[1].score == clanScores[2].score and clanScores[1].score ~= MAX_TURN_TIME then
       
   466 			roundDraw = true
       
   467 			SendStat(siGameResult, loc("Round draw"))
       
   468 			SendStat(siCustomAchievement, loc("The teams are tied for the fastest time."))
       
   469 		elseif #sortedTeams >= 1 then
   436 			SendStat(siGameResult, string.format(loc("%s wins!"), sortedTeams[1].name))
   470 			SendStat(siGameResult, string.format(loc("%s wins!"), sortedTeams[1].name))
   437 			SendStat(siCustomAchievement, string.format(loc("%s wins with a best time of %.1fs."), sortedTeams[1].name, (sortedTeams[1].score/1000)))
   471 			SendStat(siCustomAchievement, string.format(loc("%s wins with a best time of %.1fs."), sortedTeams[1].name, (sortedTeams[1].score/1000)))
   438 			for i=1,#unfinishedArray do
   472 			for i=1,#unfinishedArray do
   439 				 SendStat(siCustomAchievement, unfinishedArray[i])
   473 				 SendStat(siCustomAchievement, unfinishedArray[i])
   440 			end
   474 			end
   441 		else
   475 		else
       
   476 			roundDraw = true
   442 			SendStat(siGameResult, loc("Round draw"))
   477 			SendStat(siGameResult, loc("Round draw"))
   443 			SendStat(siCustomAchievement, loc("Nobody managed to finish the race. What a shame!"))
   478 			SendStat(siCustomAchievement, loc("Nobody managed to finish the race. What a shame!"))
   444 			SendStat(siCustomAchievement, loc("Maybe you should try an easier TechRacer map."))
   479 			SendStat(siCustomAchievement, loc("Maybe you should try an easier TechRacer map."))
   445 		end
   480 		end
   446 
   481 
  1216 	end
  1251 	end
  1217 
  1252 
  1218 	map = detectMapWithDigest()
  1253 	map = detectMapWithDigest()
  1219 
  1254 
  1220 	for i = 0, (numTeams-1) do
  1255 	for i = 0, (numTeams-1) do
  1221 		if teamScore[i] < 1000000 then
  1256 		if teamScore[i] < MAX_TURN_TIME then
  1222 			DeclareAchievement(raceType, teamNameArr[i], map, teamScore[i])
  1257 			DeclareAchievement(raceType, teamNameArr[i], map, teamScore[i])
  1223 		end
  1258 		end
  1224 	end
  1259 	end
  1225 
  1260 
  1226 	if map ~= nil and fastCount > 0 then
  1261 	if map ~= nil and fastCount > 0 then