share/hedgewars/Data/Scripts/Multiplayer/Capture_the_Flag.lua
changeset 13109 59ebdb80d693
parent 13106 115d7438aa3d
child 13110 efcdf31684ca
equal deleted inserted replaced
13108:f3f8d017f994 13109:59ebdb80d693
    86 ----------because someone is too lazy
    86 ----------because someone is too lazy
    87 ----------to read about tables properly
    87 ----------to read about tables properly
    88 ------------------ "Oh well, they probably have the memory"
    88 ------------------ "Oh well, they probably have the memory"
    89 
    89 
    90 local gameStarted = false
    90 local gameStarted = false
    91 local gameTurns = 0
       
    92 local captureLimit = 3
    91 local captureLimit = 3
    93 
    92 
    94 --------------------------
    93 --------------------------
    95 -- hog and team tracking variales
    94 -- hog and team tracking variales
    96 --------------------------
    95 --------------------------
    97 
    96 
    98 local numhhs = 0 -- store number of hedgehogs
    97 local numhhs = 0 -- store number of hedgehogs
    99 local hhs = {} -- store hedgehog gears
    98 local hhs = {} -- store hedgehog gears
   100 
    99 
   101 local numTeams --  store the number of teams in the game
       
   102 local teamNameArr = {}	-- store the list of teams
       
   103 local teamSize = {}	-- store how many hogs per team
   100 local teamSize = {}	-- store how many hogs per team
   104 local teamIndex = {} -- at what point in the hhs{} does each team begin
   101 local teamIndex = {} -- at what point in the hhs{} does each team begin
   105 
   102 
   106 -------------------
   103 -------------------
   107 -- flag variables
   104 -- flag variables
   120 local fSpawnX = {}		-- spawn X for flags
   117 local fSpawnX = {}		-- spawn X for flags
   121 local fSpawnY = {}		-- spawn Y for flags
   118 local fSpawnY = {}		-- spawn Y for flags
   122 
   119 
   123 local fThiefX = {}
   120 local fThiefX = {}
   124 local fThiefY = {}
   121 local fThiefY = {}
   125 local FTTC = 0 -- flag thief tracker counter
       
   126 
   122 
   127 local fSpawnC = {} -- spawn circle marker
   123 local fSpawnC = {} -- spawn circle marker
   128 local fCirc = {} -- flag/carrier marker circles
   124 local fCirc = {} -- flag/carrier marker circles
   129 local fCol = {} -- colour of the clans
   125 local fCol = {} -- colour of the clans
   130 
   126 
   135 
   131 
   136 ------------------------
   132 ------------------------
   137 --flag methods
   133 --flag methods
   138 ------------------------
   134 ------------------------
   139 
   135 
   140 function CheckScore(teamID)
   136 function CheckScore(clanID)
   141 
   137 
   142 	if teamID == 0 then
   138 	if fCaptures[clanID] == captureLimit then
   143 		alt = 1
   139 		-- Capture limit reached! We have a winner!
   144 	elseif teamID == 1 then
       
   145 		alt = 0
       
   146 	end
       
   147 
       
   148 	if fCaptures[teamID] == captureLimit then
       
   149 		for i = 0, (numhhs-1) do
   140 		for i = 0, (numhhs-1) do
   150 			if hhs[i] ~= nil then
   141 			if hhs[i] ~= nil then
   151 				if GetHogClan(hhs[i]) == alt then
   142 				-- Kill all losers
       
   143 				if GetHogClan(hhs[i]) ~= clanID then
   152 					SetEffect(hhs[i], heResurrectable, 0)
   144 					SetEffect(hhs[i], heResurrectable, 0)
   153 					SetHealth(hhs[i],0)
   145 					SetHealth(hhs[i],0)
   154 				end
   146 				end
   155 			end
   147 			end
   156 		end
   148 		end
   157 		if CurrentHedgehog ~= nil then
   149 		if CurrentHedgehog ~= nil then
   158 			AddCaption(string.format(loc("Victory for %s!"), GetHogTeamName(CurrentHedgehog)))
   150 			AddCaption(string.format(loc("Victory for %s!"), GetHogTeamName(CurrentHedgehog)), 0xFFFFFFFF, capgrpGameState)
   159 			updateScores()
   151 			updateScores()
   160 		end
   152 		end
   161 	end
   153 	end
   162 
   154 
   163 end
   155 end
   164 
   156 
   165 function DoFlagStuff(gear)
   157 function DoFlagStuff(gear)
   166 
   158 
       
   159 	local wtf, bbq
   167 	if (gear == fGear[0]) then
   160 	if (gear == fGear[0]) then
   168 		wtf = 0
   161 		wtf = 0
   169 		bbq = 1
   162 		bbq = 1
   170 	elseif (gear == fGear[1]) then
   163 	elseif (gear == fGear[1]) then
   171 		wtf = 1
   164 		wtf = 1
   181 		fIsMissing[wtf] = false
   174 		fIsMissing[wtf] = false
   182 		fNeedsRespawn[wtf] = true
   175 		fNeedsRespawn[wtf] = true
   183 		fIsMissing[bbq] = false
   176 		fIsMissing[bbq] = false
   184 		fNeedsRespawn[bbq] = true
   177 		fNeedsRespawn[bbq] = true
   185 		fCaptures[wtf] = fCaptures[wtf] +1
   178 		fCaptures[wtf] = fCaptures[wtf] +1
   186 		AddCaption(string.format(loc("%s has scored!"), GetHogName(CurrentHedgehog)))
   179 		AddCaption(string.format(loc("%s has scored!"), GetHogName(CurrentHedgehog)), 0xFFFFFFFF, capgrpGameState)
   187 		updateScores()
   180 		updateScores()
   188 		PlaySound(sndHomerun)
   181 		PlaySound(sndHomerun)
   189 		fThief[bbq] = nil -- player no longer has the enemy flag
   182 		fThief[bbq] = nil -- player no longer has the enemy flag
   190 		CheckScore(wtf)
   183 		CheckScore(wtf)
   191 
   184 
   195 		DeleteVisualGear(fGear[wtf])
   188 		DeleteVisualGear(fGear[wtf])
   196 		fGear[wtf] = nil -- the flag has now disappeared
   189 		fGear[wtf] = nil -- the flag has now disappeared
   197 
   190 
   198 		fNeedsRespawn[wtf] = true
   191 		fNeedsRespawn[wtf] = true
   199 		HandleRespawns() -- this will set fIsMissing[wtf] to false :)
   192 		HandleRespawns() -- this will set fIsMissing[wtf] to false :)
   200 		AddCaption(loc("Flag returned!"))
   193 		AddCaption(loc("Flag returned!"), 0xFFFFFFFF, capgrpMessage2)
   201 
   194 
   202 	--if the player is taking the enemy flag
   195 	--if the player is taking the enemy flag
   203 	elseif GetHogClan(CurrentHedgehog) == bbq then
   196 	elseif GetHogClan(CurrentHedgehog) == bbq then
   204 
   197 
   205 		DeleteVisualGear(fGear[wtf])
   198 		DeleteVisualGear(fGear[wtf])
   211 				if CurrentHedgehog == hhs[i] then
   204 				if CurrentHedgehog == hhs[i] then
   212 					fThief[wtf] = hhs[i]
   205 					fThief[wtf] = hhs[i]
   213 				end
   206 				end
   214 			end
   207 			end
   215 		end
   208 		end
   216 		AddCaption(loc("Flag captured!"))
   209 		AddCaption(loc("Flag captured!"), 0xFFFFFFFF, capgrpMessage2)
   217 
   210 
   218 	end
   211 	end
   219 
   212 
   220 end
   213 end
   221 
   214 
   222 function CheckFlagProximity()
   215 function CheckFlagProximity()
   223 
   216 
   224 	for i = 0, 1 do
   217 	for i = 0, 1 do
   225 		if fGear[i] ~= nil then
   218 		if fGear[i] ~= nil then
   226 
   219 
   227 			g1X = fGearX[i]
   220 			local g1X = fGearX[i]
   228 			g1Y = fGearY[i]
   221 			local g1Y = fGearY[i]
   229 
   222 
   230 			g2X, g2Y = GetGearPosition(CurrentHedgehog)
   223 			local g2X, g2Y = GetGearPosition(CurrentHedgehog)
   231 
   224 
   232 			q = g1X - g2X
   225 			local q = g1X - g2X
   233 			w = g1Y - g2Y
   226 			local w = g1Y - g2Y
   234 			dist = (q*q) + (w*w)
   227 			local dist = (q*q) + (w*w)
   235 
   228 
   236 			if dist < 500 then --1600
   229 			if dist < 500 then --1600
   237 				DoFlagStuff(fGear[i])
   230 				DoFlagStuff(fGear[i])
   238 			end
   231 			end
   239 		end
   232 		end
   251 			fGearX[i] = fSpawnX[i]
   244 			fGearX[i] = fSpawnX[i]
   252 			fGearY[i] = fSpawnY[i]
   245 			fGearY[i] = fSpawnY[i]
   253 
   246 
   254 			fNeedsRespawn[i] = false
   247 			fNeedsRespawn[i] = false
   255 			fIsMissing[i] = false -- new, this should solve problems of a respawned flag being "returned" when a player tries to score
   248 			fIsMissing[i] = false -- new, this should solve problems of a respawned flag being "returned" when a player tries to score
   256 			AddCaption(loc("Flag respawned!"))
   249 			AddCaption(loc("Flag respawned!"), 0xFFFFFFFF, capgrpMessage2)
   257 		end
   250 		end
   258 
   251 
   259 	end
   252 	end
   260 
   253 
   261 end
   254 end
   262 
   255 
   263 
   256 
   264 function FlagThiefDead(gear)
   257 function FlagThiefDead(gear)
   265 
   258 
       
   259 	local wtf, bbq
   266 	if (gear == fThief[0]) then
   260 	if (gear == fThief[0]) then
   267 		wtf = 0
   261 		wtf = 0
   268 		bbq = 1
   262 		bbq = 1
   269 	elseif (gear == fThief[1]) then
   263 	elseif (gear == fThief[1]) then
   270 		wtf = 1
   264 		wtf = 1
   329 -- general methods
   323 -- general methods
   330 ------------------------
   324 ------------------------
   331 
   325 
   332 function CheckDistance(gear1, gear2)
   326 function CheckDistance(gear1, gear2)
   333 
   327 
   334 	g1X, g1Y = GetGearPosition(gear1)
   328 	local g1X, g1Y = GetGearPosition(gear1)
   335 	g2X, g2Y = GetGearPosition(gear2)
   329 	local g2X, g2Y = GetGearPosition(gear2)
   336 
   330 
   337 	g1X = g1X - g2X
   331 	g1X = g1X - g2X
   338 	g1Y = g1Y - g2Y
   332 	g1Y = g1Y - g2Y
   339 	z = (g1X*g1X) + (g1Y*g1Y)
   333 	local dist = (g1X*g1X) + (g1Y*g1Y)
   340 
       
   341 	dist = z
       
   342 
   334 
   343 	return dist
   335 	return dist
   344 
   336 
   345 end
   337 end
   346 
   338 
   347 function RebuildTeamInfo()
   339 function RebuildTeamInfo()
   348 
   340 
   349 
   341 	-- make a list of teams
   350 	-- make a list of individual team names
       
   351 	for i = 0, (TeamsCount-1) do
   342 	for i = 0, (TeamsCount-1) do
   352 		teamNameArr[i] = i
       
   353 		teamSize[i] = 0
   343 		teamSize[i] = 0
   354 		teamIndex[i] = 0
   344 		teamIndex[i] = 0
   355 	end
   345 	end
   356 	numTeams = 0
       
   357 
       
   358 	for i = 0, (numhhs-1) do
       
   359 
       
   360 		z = 0
       
   361 		unfinished = true
       
   362 		while(unfinished == true) do
       
   363 
       
   364 			newTeam = true
       
   365 			tempHogTeamName = GetHogTeamName(hhs[i]) -- this is the new name
       
   366 
       
   367 			if tempHogTeamName == teamNameArr[z] then
       
   368 				newTeam = false
       
   369 				unfinished = false
       
   370 			end
       
   371 
       
   372 			z = z + 1
       
   373 
       
   374 			if z == TeamsCount then
       
   375 				unfinished = false
       
   376 				if newTeam == true then
       
   377 					teamNameArr[numTeams] = tempHogTeamName
       
   378 					numTeams = numTeams + 1
       
   379 				end
       
   380 			end
       
   381 
       
   382 		end
       
   383 
       
   384 	end
       
   385 
   346 
   386 	-- find out how many hogs per team, and the index of the first hog in hhs
   347 	-- find out how many hogs per team, and the index of the first hog in hhs
   387 	for i = 0, numTeams-1 do
   348 	for i = 0, (TeamsCount-1) do
   388 
       
   389 		for z = 0, numhhs-1 do
   349 		for z = 0, numhhs-1 do
   390 			if GetHogTeamName(hhs[z]) == teamNameArr[i] then
   350 			if GetHogTeamName(hhs[z]) == GetTeamName(i) then
   391 				if teamSize[i] == 0 then
   351 				if teamSize[i] == 0 then
   392 					teamIndex[i] = z -- should give starting index
   352 					teamIndex[i] = z -- should give starting index
   393 				end
   353 				end
   394 				teamSize[i] = teamSize[i] + 1
   354 				teamSize[i] = teamSize[i] + 1
   395 				--add a pointer so this hog appears at i in hhs
   355 				--add a pointer so this hog appears at i in hhs
   396 			end
   356 			end
   397 		end
   357 		end
   398 
       
   399 	end
   358 	end
   400 
   359 
   401 end
   360 end
   402 
   361 
   403 function StartTheGame()
   362 function StartTheGame()
   404 
   363 
   405 	gameStarted = true
   364 	gameStarted = true
   406 	AddCaption(loc("Game Started!"))
   365 	AddCaption(loc("Game Started!"), 0xFFFFFFFF, capgrpGameState)
   407 
   366 
   408 	for i = 0, 1 do
   367 	for i = 0, 1 do
   409 
   368 
   410 		fGear[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   369 		fGear[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   411 		fCirc[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   370 		fCirc[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   445 	EnableGameFlags(gfDivideTeams)
   404 	EnableGameFlags(gfDivideTeams)
   446 
   405 
   447 	-- Disable Sudden Death
   406 	-- Disable Sudden Death
   448 	WaterRise = 0
   407 	WaterRise = 0
   449 	HealthDecrease = 0
   408 	HealthDecrease = 0
   450 
       
   451 	Delay = 10
       
   452 
   409 
   453 end
   410 end
   454 
   411 
   455 function showCTFMission()
   412 function showCTFMission()
   456 	local captures
   413 	local captures
   471 
   428 
   472 	ShowMission(loc("Capture The Flag"), loc("A Hedgewars minigame"), rules, 0, 0)
   429 	ShowMission(loc("Capture The Flag"), loc("A Hedgewars minigame"), rules, 0, 0)
   473 end
   430 end
   474 
   431 
   475 function updateScores()
   432 function updateScores()
   476 	for i=0, 1 do
   433 	for i=0, TeamsCount-1 do
   477 		SetTeamLabel(teamNameArr[i], tostring(fCaptures[i]))
   434 		local team = GetTeamName(i)
       
   435 		local clan = GetTeamClan(team)
       
   436 		if clan <= 1 then
       
   437 			SetTeamLabel(team, tostring(fCaptures[clan]))
       
   438 		end
   478 	end
   439 	end
   479 end
   440 end
   480 
   441 
   481 function onGameStart()
   442 function onGameStart()
   482 
   443 
   499 end
   460 end
   500 
   461 
   501 
   462 
   502 function onNewTurn()
   463 function onNewTurn()
   503 
   464 
   504 	gameTurns = gameTurns + 1
       
   505 
       
   506 	if lastTeam ~= GetHogTeamName(CurrentHedgehog) then
       
   507 		lastTeam = GetHogTeamName(CurrentHedgehog)
       
   508 	end
       
   509 
       
   510 	if gameStarted == true then
   465 	if gameStarted == true then
   511 		HandleRespawns()
   466 		HandleRespawns()
   512 	--new method of placing starting flags
   467 	end
   513 	elseif gameTurns == 1 then
   468 
   514 		showCTFMission()
   469 	for i=0, 1 do
   515 	elseif gameTurns == 2 then
   470 		if fSpawnX[i] and fSpawnY[i] then
   516 		fPlaced[0] = true
   471 			fPlaced[i] = true
   517 	elseif gameTurns == 3 then
   472 		end
   518 		fPlaced[1] = true
   473 	end
       
   474 	if not gameStarted and fPlaced[0] and fPlaced[1] then
   519 		StartTheGame()
   475 		StartTheGame()
   520 	end
   476 	end
   521 
   477 
       
   478 end
       
   479 
       
   480 function onEndTurn()
       
   481 	 -- if the game hasn't started yet, keep track of where we are gonna put the flags on turn end
       
   482 	if not gameStarted and CurrentHedgehog ~= nil then
       
   483 		local clan = GetHogClan(CurrentHedgehog)
       
   484 
       
   485 		if clan <= 1 and GetX(CurrentHedgehog) and not fSpawnX[clan] then
       
   486 			fSpawnX[clan] = GetX(CurrentHedgehog)
       
   487 			fSpawnY[clan] = GetY(CurrentHedgehog)
       
   488 		end
       
   489 	end
   522 end
   490 end
   523 
   491 
   524 function onGameTick()
   492 function onGameTick()
   525 
   493 
   526 	for i = 0,1 do
   494 	for i = 0,1 do
   533 	if gameStarted == true then
   501 	if gameStarted == true then
   534 		HandleCircles()
   502 		HandleCircles()
   535 		if CurrentHedgehog ~= nil then
   503 		if CurrentHedgehog ~= nil then
   536 			CheckFlagProximity()
   504 			CheckFlagProximity()
   537 		end
   505 		end
   538 	elseif CurrentHedgehog ~= nil then -- if the game hasn't started yet, keep track of where we are gonna put the flags on turn end
       
   539 
       
   540 		if GetHogClan(CurrentHedgehog) == 0 then
       
   541 			i = 0
       
   542 		elseif GetHogClan(CurrentHedgehog) == 1 then
       
   543 			i = 1
       
   544 		end
       
   545 
       
   546 		if TurnTimeLeft == 0 and GetX(CurrentHedgehog) then
       
   547 			fSpawnX[i] = GetX(CurrentHedgehog)
       
   548 			fSpawnY[i] = GetY(CurrentHedgehog)
       
   549 		end
       
   550 
       
   551 	end
   506 	end
   552 
   507 
   553 end
   508 end
   554 
   509 
   555 function onGearResurrect(gear)
   510 function onGearResurrect(gear)
   564 	AddVisualGear(GetX(gear), GetY(gear), vgtBigExplosion, 0, false)
   519 	AddVisualGear(GetX(gear), GetY(gear), vgtBigExplosion, 0, false)
   565 
   520 
   566 end
   521 end
   567 
   522 
   568 function InABetterPlaceNow(gear)
   523 function InABetterPlaceNow(gear)
   569 	for i = 0, (numhhs-1) do
   524 	for h = 0, (numhhs-1) do
   570 		if gear == hhs[i] then
   525 		if gear == hhs[h] then
   571 
       
   572 			for i = 0,1 do
   526 			for i = 0,1 do
   573 				if gear == fThief[i] then
   527 				if gear == fThief[i] then
   574 					FlagThiefDead(gear)
   528 					FlagThiefDead(gear)
   575 				end
   529 				end
   576 			end
   530 			end
   577 			hhs[i] = nil
   531 			hhs[h] = nil
   578 		end
   532 		end
   579 	end
   533 	end
   580 end
   534 end
   581 
   535 
   582 function onHogHide(gear)
   536 function onHogHide(gear)
   583 	 InABetterPlaceNow(gear)
   537 	 InABetterPlaceNow(gear)
   584 end
   538 end
   585 
   539 
   586 function onHogRestore(gear)
   540 function onHogRestore(gear)
   587 	match = false
       
   588 	for i = 0, (numhhs-1) do
   541 	for i = 0, (numhhs-1) do
   589 		if (hhs[i] == nil) and (match == false) then
   542 		if (hhs[i] == nil) then
   590 			hhs[i] = gear
   543 			hhs[i] = gear
   591 			match = true
   544 			break
   592 		end
   545 		end
   593 	end
   546 	end
   594 end
   547 end
   595 
   548 
   596 function onHogAttack(ammoType)
   549 function onHogAttack(ammoType)
   624 
   577 
   625 	if GetGearType(gear) == gtHedgehog then
   578 	if GetGearType(gear) == gtHedgehog then
   626 		InABetterPlaceNow(gear)
   579 		InABetterPlaceNow(gear)
   627 	elseif GetGearType(gear) == gtKamikaze and not gameStarted then
   580 	elseif GetGearType(gear) == gtKamikaze and not gameStarted then
   628 		local i = GetHogClan(CurrentHedgehog)
   581 		local i = GetHogClan(CurrentHedgehog)
   629 		fSpawnX[i] = GetX(CurrentHedgehog)
   582 		if i <= 1 then
   630 		fSpawnY[i] = GetY(CurrentHedgehog)
   583 			fSpawnX[i] = GetX(CurrentHedgehog)
   631 	end
   584 			fSpawnY[i] = GetY(CurrentHedgehog)
   632 
   585 		end
   633 end
   586 	end
       
   587 
       
   588 end