share/hedgewars/Data/Scripts/Multiplayer/Capture_the_Flag.lua
changeset 13111 10c914738f22
parent 13110 efcdf31684ca
child 13112 714f6e5b89fd
equal deleted inserted replaced
13110:efcdf31684ca 13111:10c914738f22
    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 gameOver = false
    91 local captureLimit = 3
    92 local captureLimit = 3
    92 
    93 
    93 --------------------------
    94 --------------------------
    94 -- hog and team tracking variales
    95 -- hog and team tracking variales
    95 --------------------------
    96 --------------------------
   102 
   103 
   103 -------------------
   104 -------------------
   104 -- flag variables
   105 -- flag variables
   105 -------------------
   106 -------------------
   106 
   107 
   107 local fPlaced = {} -- has the flag been placed TRUE/FALSE
       
   108 
       
   109 local fGear = {}	-- pointer to the visual gears that represent the flag
   108 local fGear = {}	-- pointer to the visual gears that represent the flag
   110 local fGearX = {}
   109 local fGearX = {}
   111 local fGearY = {}
   110 local fGearY = {}
   112 
   111 
   113 local fThief = {}	-- pointer to the hogs who stole the flags
   112 local fThief = {}	-- pointer to the hogs who stole the flags
       
   113 local fThiefFlag = {}   -- contains the stolen flag type of fThief
   114 local fIsMissing = {}	-- have the flags been destroyed or captured
   114 local fIsMissing = {}	-- have the flags been destroyed or captured
   115 local fNeedsRespawn = {}	-- do the flags need to be respawned
   115 local fNeedsRespawn = {}	-- do the flags need to be respawned
   116 local fCaptures = {}	-- the team "scores" how many captures
   116 local fCaptures = {}	-- the team "scores" how many captures
   117 local fSpawnX = {}		-- spawn X for flags
   117 local fSpawnX = {}		-- spawn X for flags
   118 local fSpawnY = {}		-- spawn Y for flags
   118 local fSpawnY = {}		-- spawn Y for flags
   134 ------------------------
   134 ------------------------
   135 
   135 
   136 function CheckScore(clanID)
   136 function CheckScore(clanID)
   137 
   137 
   138 	if fCaptures[clanID] == captureLimit then
   138 	if fCaptures[clanID] == captureLimit then
       
   139 		gameOver = true
   139 		-- Capture limit reached! We have a winner!
   140 		-- Capture limit reached! We have a winner!
   140 		for i = 0, (numhhs-1) do
   141 		for i = 0, (numhhs-1) do
   141 			if hhs[i] ~= nil then
   142 			if hhs[i] ~= nil then
   142 				-- Kill all losers
   143 				-- Kill all losers
   143 				if GetHogClan(hhs[i]) ~= clanID then
   144 				if GetHogClan(hhs[i]) ~= clanID then
   152 		end
   153 		end
   153 	end
   154 	end
   154 
   155 
   155 end
   156 end
   156 
   157 
   157 function DoFlagStuff(gear)
   158 function DoFlagStuff(flag, flagClan)
   158 
   159 
   159 	local wtf, bbq
   160 	if not CurrentHedgehog then
   160 	if (gear == fGear[0]) then
   161 		return
   161 		wtf = 0
   162 	end
   162 		bbq = 1
   163 	local wtf = flagClan
   163 	elseif (gear == fGear[1]) then
   164 
   164 		wtf = 1
   165 	local thiefClan
   165 		bbq = 0
   166 	for i=0, ClansCount - 1 do
       
   167 		if CurrentHedgehog == fThief[i] then
       
   168 			thiefClan = i
       
   169 		end
   166 	end
   170 	end
   167 
   171 
   168 	-- player has successfully captured the enemy flag
   172 	-- player has successfully captured the enemy flag
   169 	if (GetHogClan(CurrentHedgehog) == wtf) and (CurrentHedgehog == fThief[bbq]) and (fIsMissing[wtf] == false) then
   173 	if (GetHogClan(CurrentHedgehog) == flagClan) and (thiefClan ~= nil) and (fIsMissing[flagClan] == false) then
   170 
   174 
   171 		DeleteVisualGear(fGear[wtf])
   175 		DeleteVisualGear(fGear[flagClan])
   172 		fGear[wtf] = nil -- the flag has now disappeared
   176 		fGear[flagClan] = nil -- the flag has now disappeared
   173 
   177 
   174 		fIsMissing[wtf] = false
   178 		fIsMissing[flagClan] = false
   175 		fNeedsRespawn[wtf] = true
   179 		fNeedsRespawn[flagClan] = true
   176 		fIsMissing[bbq] = false
   180 		fIsMissing[thiefClan] = false
   177 		fNeedsRespawn[bbq] = true
   181 		fNeedsRespawn[thiefClan] = true
   178 		fCaptures[wtf] = fCaptures[wtf] +1
   182 		fCaptures[flagClan] = fCaptures[flagClan] +1
   179 		AddCaption(string.format(loc("%s has scored!"), GetHogName(CurrentHedgehog)), 0xFFFFFFFF, capgrpGameState)
   183 		AddCaption(string.format(loc("%s has scored!"), GetHogName(CurrentHedgehog)), 0xFFFFFFFF, capgrpGameState)
   180 		updateScores()
   184 		updateScores()
   181 		PlaySound(sndHomerun)
   185 		PlaySound(sndHomerun)
   182 		fThief[bbq] = nil -- player no longer has the enemy flag
   186 		fThief[thiefClan] = nil -- player no longer has the enemy flag
   183 		CheckScore(wtf)
   187 		fThiefFlag[flagClan] = nil
       
   188 		CheckScore(flagClan)
   184 
   189 
   185 	--if the player is returning the flag
   190 	--if the player is returning the flag
   186 	elseif (GetHogClan(CurrentHedgehog) == wtf) and (fIsMissing[wtf] == true) then
   191 	elseif (GetHogClan(CurrentHedgehog) == flagClan) and (fIsMissing[flagClan] == true) then
   187 
   192 
   188 		DeleteVisualGear(fGear[wtf])
   193 		DeleteVisualGear(fGear[flagClan])
   189 		fGear[wtf] = nil -- the flag has now disappeared
   194 		fGear[flagClan] = nil -- the flag has now disappeared
   190 
   195 
   191 		fNeedsRespawn[wtf] = true
   196 		fNeedsRespawn[flagClan] = true
   192 		HandleRespawns() -- this will set fIsMissing[wtf] to false :)
   197 		HandleRespawns() -- this will set fIsMissing[flagClan] to false :)
   193 		AddCaption(loc("Flag returned!"), 0xFFFFFFFF, capgrpMessage2)
   198 		AddCaption(loc("Flag returned!"), 0xFFFFFFFF, capgrpMessage2)
   194 
   199 
   195 	--if the player is taking the enemy flag
   200 	--if the player is taking the enemy flag (not possible if already holding a flag)
   196 	elseif GetHogClan(CurrentHedgehog) == bbq then
   201 	elseif GetHogClan(CurrentHedgehog) ~= flagClan and (thiefClan == nil) then
   197 
   202 
   198 		DeleteVisualGear(fGear[wtf])
   203 		DeleteVisualGear(fGear[flagClan])
   199 		fGear[wtf] = nil -- the flag has now disappeared
   204 		fGear[flagClan] = nil -- the flag has now disappeared
   200 
   205 
   201 		fIsMissing[wtf] = true
   206 		fIsMissing[flagClan] = true
   202 		for i = 0,numhhs-1 do
   207 		for i = 0,numhhs-1 do
   203 			if CurrentHedgehog ~= nil then
   208 			if CurrentHedgehog ~= nil then
   204 				if CurrentHedgehog == hhs[i] then
   209 				if CurrentHedgehog == hhs[i] then
   205 					fThief[wtf] = hhs[i]
   210 					fThief[flagClan] = hhs[i]
       
   211 					fThiefFlag[flagClan] = flagClan
   206 				end
   212 				end
   207 			end
   213 			end
   208 		end
   214 		end
   209 		AddCaption(loc("Flag captured!"), 0xFFFFFFFF, capgrpMessage2)
   215 		AddCaption(loc("Flag captured!"), 0xFFFFFFFF, capgrpMessage2)
   210 
   216 
   212 
   218 
   213 end
   219 end
   214 
   220 
   215 function CheckFlagProximity()
   221 function CheckFlagProximity()
   216 
   222 
   217 	for i = 0, 1 do
   223 	for i = 0, ClansCount-1 do
   218 		if fGear[i] ~= nil then
   224 		if fGear[i] ~= nil then
   219 
   225 
   220 			local g1X = fGearX[i]
   226 			local g1X = fGearX[i]
   221 			local g1Y = fGearY[i]
   227 			local g1Y = fGearY[i]
   222 
   228 
   224 
   230 
   225 			local q = g1X - g2X
   231 			local q = g1X - g2X
   226 			local w = g1Y - g2Y
   232 			local w = g1Y - g2Y
   227 			local dist = (q*q) + (w*w)
   233 			local dist = (q*q) + (w*w)
   228 
   234 
   229 			if dist < 500 then --1600
   235 			if dist < 500 then
   230 				DoFlagStuff(fGear[i])
   236 				DoFlagStuff(fGear[i], i)
   231 			end
   237 			end
   232 		end
   238 		end
   233 	end
   239 	end
   234 
   240 
   235 end
   241 end
   236 
   242 
   237 
   243 
   238 function HandleRespawns()
   244 function HandleRespawns()
   239 
   245 
   240 	for i = 0, 1 do
   246 	for i = 0, ClansCount-1 do
   241 
   247 
   242 		if fNeedsRespawn[i] == true then
   248 		if fNeedsRespawn[i] == true then
   243 			fGear[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   249 			fGear[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   244 			fGearX[i] = fSpawnX[i]
   250 			fGearX[i] = fSpawnX[i]
   245 			fGearY[i] = fSpawnY[i]
   251 			fGearY[i] = fSpawnY[i]
   254 end
   260 end
   255 
   261 
   256 
   262 
   257 function FlagThiefDead(gear)
   263 function FlagThiefDead(gear)
   258 
   264 
   259 	local wtf, bbq
   265 	local thiefClan
   260 	if (gear == fThief[0]) then
   266 	local stolenFlagClan
   261 		wtf = 0
   267 	for i=0, ClansCount-1 do
   262 		bbq = 1
   268 		if (gear == fThief[i]) then
   263 	elseif (gear == fThief[1]) then
   269 			thiefClan = i
   264 		wtf = 1
   270 			stolenFlagClan = fThiefFlag[i]
   265 		bbq = 0
   271 			break
   266 	end
   272 		end
   267 
   273 	end
   268 	if fThief[wtf] ~= nil then
   274 
       
   275 	if stolenFlagClan ~= nil then
   269 		-- falls into water
   276 		-- falls into water
   270 		if (LAND_HEIGHT - fThiefY[wtf]) < 15 then
   277 		if (LAND_HEIGHT - fThiefY[thiefClan]) < 15 then
   271 			fIsMissing[wtf] = true
   278 			fIsMissing[stolenFlagClan] = true
   272 			fNeedsRespawn[wtf] = true
   279 			fNeedsRespawn[stolenFlagClan] = true
   273 			HandleRespawns()
   280 			HandleRespawns()
   274 		else	--normally
   281 		else	--normally
   275 			fGearX[wtf]  =  fThiefX[wtf]
   282 			fGearX[stolenFlagClan] = fThiefX[thiefClan]
   276 			fGearY[wtf]  =  fThiefY[wtf]
   283 			fGearY[stolenFlagClan] = fThiefY[thiefClan]
   277 			fGear[wtf] = AddVisualGear(fGearX[wtf],fGearY[wtf],vgtCircle,0,true)
   284 			fGear[stolenFlagClan] = AddVisualGear(fGearX[stolenFlagClan], fGearY[stolenFlagClan], vgtCircle, 0, true)
   278 		end
   285 		end
   279 
   286 
   280 		AddVisualGear(fThiefX[wtf], fThiefY[wtf], vgtBigExplosion, 0, false)
   287 		AddVisualGear(fThiefX[thiefClan], fThiefY[thiefClan], vgtBigExplosion, 0, false)
   281 		fThief[wtf] = nil
   288 		fThief[thiefClan] = nil
   282 	end
   289 	end
   283 
   290 
   284 end
   291 end
   285 
   292 
   286 function HandleCircles()
   293 function HandleCircles()
   292 		if fGearRad > fGearRadMax then
   299 		if fGearRad > fGearRadMax then
   293 			fGearRad = fGearRadMin
   300 			fGearRad = fGearRadMin
   294 		end
   301 		end
   295 	end
   302 	end
   296 
   303 
   297 	for i = 0, 1 do
   304 	for i = 0, ClansCount-1 do
   298 
   305 
   299 		if fIsMissing[i] == false then -- draw a flag marker at the flag's spawning place
   306 		if fIsMissing[i] == false then -- draw a flag marker at the flag's spawning place
   300 			SetVisualGearValues(fCirc[i], fSpawnX[i],fSpawnY[i], 20, 20, 0, 10, 0, 33, 3, fCol[i])
   307 			SetVisualGearValues(fCirc[i], fSpawnX[i],fSpawnY[i], 20, 20, 0, 10, 0, 33, 3, fCol[i])
   301 			if fGear[i] ~= nil then -- draw the flag gear itself
   308 			if fGear[i] ~= nil then -- draw the flag gear itself
   302 				SetVisualGearValues(fGear[i], fSpawnX[i],fSpawnY[i], 20, 200, 0, 0, 100, fGearRad, 2, fCol[i])
   309 				SetVisualGearValues(fGear[i], fSpawnX[i],fSpawnY[i], 20, 200, 0, 0, 100, fGearRad, 2, fCol[i])
   362 function StartTheGame()
   369 function StartTheGame()
   363 
   370 
   364 	gameStarted = true
   371 	gameStarted = true
   365 	AddCaption(loc("Game Started!"), 0xFFFFFFFF, capgrpGameState)
   372 	AddCaption(loc("Game Started!"), 0xFFFFFFFF, capgrpGameState)
   366 
   373 
   367 	for i = 0, 1 do
   374 	for i = 0, ClansCount-1 do
   368 
   375 
   369 		fGear[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   376 		fGear[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   370 		fCirc[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   377 		fCirc[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   371 		fSpawnC[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   378 		fSpawnC[i] = AddVisualGear(fSpawnX[i],fSpawnY[i],vgtCircle,0,true)
   372 
   379 
   431 
   438 
   432 function updateScores()
   439 function updateScores()
   433 	for i=0, TeamsCount-1 do
   440 	for i=0, TeamsCount-1 do
   434 		local team = GetTeamName(i)
   441 		local team = GetTeamName(i)
   435 		local clan = GetTeamClan(team)
   442 		local clan = GetTeamClan(team)
   436 		if clan <= 1 then
   443 		SetTeamLabel(team, tostring(fCaptures[clan]))
   437 			SetTeamLabel(team, tostring(fCaptures[clan]))
       
   438 		end
       
   439 	end
   444 	end
   440 end
   445 end
   441 
   446 
   442 function onGameStart()
   447 function onGameStart()
   443 
   448 
   444 	showCTFMission()
   449 	showCTFMission()
   445 
   450 
   446 	RebuildTeamInfo()
   451 	RebuildTeamInfo()
   447 
   452 
   448 	for i=0, 1 do
   453 	for i=0, ClansCount-1 do
   449 		fPlaced[i] = false
       
   450 		fCaptures[i] = 0
   454 		fCaptures[i] = 0
   451 	end
   455 	end
   452 
   456 
   453 	for h=1, numhhs do
   457 	for h=1, numhhs do
   454 		-- Hogs are resurrected for free, so this is pointless
   458 		-- Hogs are resurrected for free, so this is pointless
   464 
   468 
   465 	if gameStarted == true then
   469 	if gameStarted == true then
   466 		HandleRespawns()
   470 		HandleRespawns()
   467 	end
   471 	end
   468 
   472 
   469 	for i=0, 1 do
   473 	local flagsPlaced = 0
       
   474 	for i=0, ClansCount-1 do
   470 		if fSpawnX[i] and fSpawnY[i] then
   475 		if fSpawnX[i] and fSpawnY[i] then
   471 			fPlaced[i] = true
   476 			flagsPlaced = flagsPlaced + 1
   472 		end
   477 		end
   473 	end
   478 	end
   474 	if not gameStarted and fPlaced[0] and fPlaced[1] then
   479 	if not gameStarted and flagsPlaced == ClansCount then
   475 		StartTheGame()
   480 		StartTheGame()
   476 	end
   481 	end
   477 
   482 
   478 end
   483 end
   479 
   484 
   480 function onEndTurn()
   485 function onEndTurn()
   481 	 -- if the game hasn't started yet, keep track of where we are gonna put the flags on turn end
   486 	 -- 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
   487 	if not gameStarted and CurrentHedgehog ~= nil then
   483 		local clan = GetHogClan(CurrentHedgehog)
   488 		local clan = GetHogClan(CurrentHedgehog)
   484 
   489 
   485 		if clan <= 1 and GetX(CurrentHedgehog) and not fSpawnX[clan] then
   490 		if GetX(CurrentHedgehog) and not fSpawnX[clan] then
   486 			fSpawnX[clan] = GetX(CurrentHedgehog)
   491 			fSpawnX[clan] = GetX(CurrentHedgehog)
   487 			fSpawnY[clan] = GetY(CurrentHedgehog)
   492 			fSpawnY[clan] = GetY(CurrentHedgehog)
   488 		end
   493 		end
   489 	end
   494 	end
   490 end
   495 end
   491 
   496 
   492 function onGameTick()
   497 function onGameTick()
   493 
   498 
   494 	for i = 0,1 do
   499 	for i = 0, ClansCount-1 do
   495 		if fThief[i] ~= nil then
   500 		if fThief[i] ~= nil then
   496 			fThiefX[i] = GetX(fThief[i])
   501 			fThiefX[i] = GetX(fThief[i])
   497 			fThiefY[i] = GetY(fThief[i])
   502 			fThiefY[i] = GetY(fThief[i])
   498 		end
   503 		end
   499 	end
   504 	end
   500 
   505 
   501 	if gameStarted == true then
   506 	if gameStarted == true and not gameOver then
   502 		HandleCircles()
   507 		HandleCircles()
   503 		if CurrentHedgehog ~= nil then
   508 		if CurrentHedgehog ~= nil then
   504 			CheckFlagProximity()
   509 			CheckFlagProximity()
   505 		end
   510 		end
   506 	end
   511 	end
   509 
   514 
   510 function onGearResurrect(gear)
   515 function onGearResurrect(gear)
   511 
   516 
   512 	if GetGearType(gear) == gtHedgehog then
   517 	if GetGearType(gear) == gtHedgehog then
   513 		-- mark the flag thief as dead if he needed a respawn
   518 		-- mark the flag thief as dead if he needed a respawn
   514 		for i = 0,1 do
   519 		for i = 0, ClansCount-1 do
   515 			if gear == fThief[i] then
   520 			if gear == fThief[i] then
   516 				FlagThiefDead(gear)
   521 				FlagThiefDead(gear)
   517 			end
   522 			end
   518 		end
   523 		end
   519 		AddVisualGear(GetX(gear), GetY(gear), vgtBigExplosion, 0, false)
   524 		AddVisualGear(GetX(gear), GetY(gear), vgtBigExplosion, 0, false)
   522 end
   527 end
   523 
   528 
   524 function InABetterPlaceNow(gear)
   529 function InABetterPlaceNow(gear)
   525 	for h = 0, (numhhs-1) do
   530 	for h = 0, (numhhs-1) do
   526 		if gear == hhs[h] then
   531 		if gear == hhs[h] then
   527 			for i = 0,1 do
   532 			for i = 0, ClansCount-1 do
   528 				if gear == fThief[i] then
   533 				if gear == fThief[i] then
   529 					FlagThiefDead(gear)
   534 					FlagThiefDead(gear)
   530 				end
   535 				end
   531 			end
   536 			end
   532 			hhs[h] = nil
   537 			hhs[h] = nil
   561 		hhs[numhhs] = gear
   566 		hhs[numhhs] = gear
   562 		numhhs = numhhs + 1
   567 		numhhs = numhhs + 1
   563 		SetEffect(gear, heResurrectable, 1)
   568 		SetEffect(gear, heResurrectable, 1)
   564 
   569 
   565 	elseif GetGearType(gear) == gtPiano then
   570 	elseif GetGearType(gear) == gtPiano then
   566 		for i = 0, 1 do
   571 		for i = 0, ClansCount-1 do
   567 			if CurrentHedgehog == fThief[i] then
   572 			if CurrentHedgehog == fThief[i] then
   568 				FlagThiefDead(CurrentHedgehog)
   573 				FlagThiefDead(CurrentHedgehog)
   569 			end
   574 			end
   570 		end
   575 		end
   571 
   576