share/hedgewars/Data/Scripts/Multiplayer/Racer.lua
changeset 5845 b20a1e0a0e7e
child 5894 86c59c34cdd5
equal deleted inserted replaced
5844:fdf22a4843f7 5845:b20a1e0a0e7e
       
     1 
       
     2 ------------------------------------------
       
     3 -- RACER 0.1
       
     4 -- map-independant racing script
       
     5 -- by mikade
       
     6 -----------------------------------------
       
     7 
       
     8 -----------------------------------
       
     9 --0.1: took all the code from crazy racer and scrapped most of it
       
    10 -----------------------------------
       
    11 
       
    12 -- Removed tumbler system
       
    13 -- Removed extra adds like boosters etc
       
    14 -- Added experimental waypoint placement system
       
    15 -- More user feedback
       
    16 -- Reduced race complexity limit to 5 waypoints
       
    17 -- stop placement at complexity limit reached and end turn
       
    18 -- guys dont keep racing after dying
       
    19 -- invulnerable feasibility
       
    20 -- reverted time keeping method
       
    21 -- reduced feedback display time
       
    22 -- colour-coded addcaptions
       
    23 -- cleaned up code
       
    24 -- support for more players properly added
       
    25 -- tardis fix
       
    26 -- remove airstrikes
       
    27 
       
    28 -- i think the remainder 0 .456 sec of the tracktime isnt getting reset on newturn
       
    29 
       
    30 -- update feedback
       
    31 
       
    32 -----------------------------
       
    33 -- SCRIPT BEGINS
       
    34 -----------------------------
       
    35 
       
    36 loadfile(GetDataPath() .. "Scripts/Locale.lua")()
       
    37 
       
    38 ------------------
       
    39 -- Got Variables?
       
    40 ------------------
       
    41 
       
    42 local fMod = 1000000 -- 1
       
    43 local roundLimit = 3
       
    44 local roundNumber = 0
       
    45 local firstClan = 10
       
    46 
       
    47 local fastX = {}
       
    48 local fastY = {}
       
    49 local fastCount = 0
       
    50 local fastIndex = 0
       
    51 local fastColour
       
    52 
       
    53 local currX = {}
       
    54 local currY = {}
       
    55 local currCount = 0
       
    56 
       
    57 --------------------------
       
    58 -- hog and team tracking variales
       
    59 --------------------------
       
    60 
       
    61 local numhhs = 0 -- store number of hedgehogs
       
    62 local hhs = {} -- store hedgehog gears
       
    63 
       
    64 local numTeams --  store the number of teams in the game
       
    65 local teamNameArr = {}	-- store the list of teams
       
    66 local teamClan = {}
       
    67 local teamSize = {}	-- store how many hogs per team
       
    68 local teamIndex = {} -- at what point in the hhs{} does each team begin
       
    69 
       
    70 local teamComment = {}
       
    71 local teamScore = {}
       
    72 
       
    73 -------
       
    74 -- racer vars
       
    75 --------
       
    76 
       
    77 local cGear = nil
       
    78 local gTimer = 0
       
    79 
       
    80 local bestClan = nil
       
    81 local bestTime = nil
       
    82 
       
    83 local gameBegun = false
       
    84 local gameOver = false
       
    85 local racerActive = false
       
    86 local trackTime = 0
       
    87 local wpCheckCounter = 0
       
    88 
       
    89 local wpCirc = {}
       
    90 local wpX = {}
       
    91 local wpY = {}
       
    92 local wpCol = {}
       
    93 local wpActive = {}
       
    94 local wpRad = 450 --75 
       
    95 local wpCount = 0
       
    96 local wpLimit = 5
       
    97 
       
    98 -------------------
       
    99 -- general methods
       
   100 -------------------
       
   101 
       
   102 function RebuildTeamInfo()
       
   103 
       
   104 
       
   105 	-- make a list of individual team names
       
   106 	for i = 0, (TeamsCount-1) do
       
   107 		teamNameArr[i] = " " -- = i
       
   108 		teamSize[i] = 0
       
   109 		teamIndex[i] = 0
       
   110 		teamScore[i] = 100000
       
   111 	end
       
   112 	numTeams = 0
       
   113 
       
   114 	for i = 0, (numhhs-1) do
       
   115 
       
   116 		z = 0
       
   117 		unfinished = true
       
   118 		while(unfinished == true) do
       
   119 
       
   120 			newTeam = true
       
   121 			tempHogTeamName = GetHogTeamName(hhs[i]) -- this is the new name
       
   122 
       
   123 			if tempHogTeamName == teamNameArr[z] then
       
   124 				newTeam = false
       
   125 				unfinished = false
       
   126 			end
       
   127 
       
   128 			z = z + 1
       
   129 
       
   130 			if z == TeamsCount then
       
   131 				unfinished = false
       
   132 				if newTeam == true then
       
   133 					teamNameArr[numTeams] = tempHogTeamName
       
   134 					numTeams = numTeams + 1
       
   135 				end
       
   136 			end
       
   137 
       
   138 		end
       
   139 
       
   140 	end
       
   141 
       
   142 	-- find out how many hogs per team, and the index of the first hog in hhs
       
   143 	for i = 0, (numTeams-1) do
       
   144 		for z = 0, (numhhs-1) do
       
   145 			if GetHogTeamName(hhs[z]) == teamNameArr[i] then
       
   146 				teamClan[i] = GetHogClan(hhs[z])
       
   147 				if teamSize[i] == 0 then
       
   148 					teamIndex[i] = z -- should give starting index
       
   149 				end
       
   150 				teamSize[i] = teamSize[i] + 1
       
   151 				--add a pointer so this hog appears at i in hhs
       
   152 			end
       
   153 		end
       
   154 
       
   155 	end
       
   156 
       
   157 end
       
   158 
       
   159 
       
   160 -----------------
       
   161 -- RACER METHODS
       
   162 -----------------
       
   163 
       
   164 function CheckWaypoints()
       
   165 
       
   166 	trackFinished = true
       
   167 	
       
   168 	for i = 0, (wpCount-1) do
       
   169 
       
   170 		g1X, g1Y = GetGearPosition(CurrentHedgehog)
       
   171 		g2X, g2Y = wpX[i], wpY[i]
       
   172 
       
   173 		g1X = g1X - g2X
       
   174 		g1Y = g1Y - g2Y
       
   175 		dist = (g1X*g1X) + (g1Y*g1Y)
       
   176 
       
   177 		--if i == 0 then
       
   178 		--	AddCaption(dist .. "/" .. (wpRad*wpRad) )
       
   179 		--end
       
   180 
       
   181 		NR = (48/100*wpRad)/2		
       
   182 
       
   183 		if dist < (NR*NR) then		
       
   184 		--if dist < (wpRad*wpRad) then
       
   185 			--AddCaption("howdy")
       
   186 			wpActive[i] = true
       
   187 			wpCol[i] = GetClanColor(GetHogClan(CurrentHedgehog)) -- new				--GetClanColor(1)
       
   188 			SetVisualGearValues(wpCirc[i], wpX[i], wpY[i], 20, 100, 1, 10, 0, wpRad, 5, wpCol[i])
       
   189 
       
   190 			wpRem = 0
       
   191 			for k = 0, (wpCount-1) do
       
   192 				if wpActive[k] == false then
       
   193 					wpRem = wpRem + 1				
       
   194 				end
       
   195 			end
       
   196 
       
   197 			AddCaption(loc("Way-Points Remaining") .. ": " .. wpRem,0xffba00ff,capgrpAmmoinfo)
       
   198 
       
   199 		end
       
   200 
       
   201 		if wpActive[i] == false then
       
   202 			trackFinished = false
       
   203 		end
       
   204 
       
   205 	end
       
   206 
       
   207 	return(trackFinished)
       
   208 
       
   209 end
       
   210 
       
   211 function AdjustScores()
       
   212 
       
   213 	if bestTime == nil then
       
   214 		bestTime = 100000
       
   215 		bestClan = 10
       
   216 		bestTimeComment = "N/A"
       
   217 	end
       
   218 
       
   219 	newScore = false
       
   220 
       
   221 	-- update this clan's time if the new track is better
       
   222 	for i = 0, (numTeams-1) do
       
   223 		if teamClan[i] == GetHogClan(CurrentHedgehog) then
       
   224 			if trackTime < teamScore[i] then
       
   225 				teamScore[i] = trackTime
       
   226 				newScore = true
       
   227 			else
       
   228 				newScore = false
       
   229 			end
       
   230 		end
       
   231 	end
       
   232 
       
   233 	--bestTime = 100000
       
   234 	--bestClan = 10
       
   235 
       
   236 	-- find the best time out of those so far
       
   237 	for i = 0, (numTeams-1) do
       
   238 		if teamScore[i] < bestTime then
       
   239 			bestTime = teamScore[i]
       
   240 			bestClan = teamClan[i]
       
   241 		end
       
   242 	end
       
   243 
       
   244 	if bestTime ~= 100000 then
       
   245 		bestTimeComment = (bestTime/1000) ..loc("s")
       
   246 	end
       
   247 
       
   248 	if newScore == true then
       
   249 		if trackTime == bestTime then -- best time of the race
       
   250 			ShowMission(loc("RACER"), 
       
   251 			loc("TRACK COMPLETED"), 
       
   252 			loc("NEW RACE RECORD: ") .. (trackTime/1000) ..loc("s") .. "|" .. 
       
   253 			loc("WINNING TIME: ") .. bestTimeComment, 0, 4000)
       
   254 		else	-- best time for the clan
       
   255 			ShowMission(loc("RACER"), 
       
   256 			loc("TRACK COMPLETED"), 
       
   257 			loc("NEW CLAN RECORD: ") .. (trackTime/1000) ..loc("s") .. "|" .. 
       
   258 			loc("WINNING TIME: ") .. bestTimeComment, 4, 4000)
       
   259 		end
       
   260 	else -- not any kind of new score
       
   261 		ShowMission(loc("RACER"), 
       
   262 		loc("TRACK COMPLETED"), 
       
   263 		loc("TIME: ") .. (trackTime/1000) ..loc("s") .. "|" .. 
       
   264 		loc("WINNING TIME: ") .. bestTimeComment, -amSkip, 4000)
       
   265 	end
       
   266 
       
   267 
       
   268 	--------
       
   269 	--new
       
   270 	--------
       
   271 
       
   272 	if bestTime == trackTime then
       
   273 		--AddCaption("wooooooooooooooooooooooooooooo")
       
   274 
       
   275 		fastColour = GetClanColor(GetHogClan(CurrentHedgehog))
       
   276 
       
   277 		for i = 0, (currCount-1) do
       
   278 			fastX[i] = currX[i]
       
   279 			fastY[i] = currY[i]
       
   280 		end
       
   281 
       
   282 		fastCount = currCount
       
   283 		fastIndex = 0
       
   284 
       
   285 		--currCount = 0 -- is this needed?
       
   286 
       
   287 	else
       
   288 		currCount = 0
       
   289 		fastIndex = 0
       
   290 	end
       
   291 
       
   292 
       
   293 end
       
   294 
       
   295 function CheckForNewRound()
       
   296 
       
   297 	if GetHogClan(CurrentHedgehog) == firstClan then
       
   298 
       
   299 		roundNumber = roundNumber + 1
       
   300 
       
   301 		totalComment = ""		
       
   302 		for i = 0, (TeamsCount-1) do
       
   303 				if teamNameArr[i] ~= " " then				-- teamScore[teamClan[i]]
       
   304 					teamComment[i] = teamNameArr[i] .. ": " .. (teamScore[i]/1000) .. loc("s|")
       
   305 					totalComment = totalComment .. teamComment[i]
       
   306 				elseif teamNameArr[i] == " " then
       
   307 					teamComment[i] = "|"
       
   308 				end
       
   309 		end
       
   310 
       
   311 		ShowMission(	loc("RACER"), 
       
   312 				loc("STATUS UPDATE"), 
       
   313 				loc("Rounds Complete: ") .. roundNumber .. "/" .. roundLimit .. "|" .. " " .. "|" .. 
       
   314 				loc("Best Team Times: ") .. "|" .. totalComment, 0, 4000)
       
   315 
       
   316 		-- end game if its at round limit
       
   317 		if roundNumber == roundLimit then
       
   318 			for i = 0, (numhhs-1) do
       
   319 				if GetHogClan(hhs[i]) ~= bestClan then
       
   320 					SetEffect(hhs[i], heResurrectable, false)
       
   321 					SetHealth(hhs[i],0)
       
   322 				end
       
   323 			end
       
   324 			gameOver = true
       
   325 			TurnTimeLeft = 1
       
   326 		end
       
   327 
       
   328 	end
       
   329 
       
   330 end
       
   331 
       
   332 function DisableTumbler()
       
   333 	currCount = 0
       
   334 	fastIndex = 0
       
   335 	TurnTimeLeft = 0
       
   336 	racerActive = false -- newadd
       
   337 end
       
   338 
       
   339 function HandleGhost()
       
   340 
       
   341 	-- get the current xy of the racer at this point
       
   342 	currX[currCount] = GetX(CurrentHedgehog)
       
   343 	currY[currCount] = GetY(CurrentHedgehog)
       
   344 	currCount = currCount + 1
       
   345 
       
   346 	-- draw a ping of smoke where the fastest player was at this point
       
   347 	if (fastCount ~= 0) and (fastIndex < fastCount) then
       
   348 
       
   349 		fastIndex = fastIndex + 1
       
   350 
       
   351 		tempE = AddVisualGear(fastX[fastIndex], fastY[fastIndex], vgtSmoke, 0, false)
       
   352 		g1, g2, g3, g4, g5, g6, g7, g8, g9, g10 = GetVisualGearValues(tempE)
       
   353 		SetVisualGearValues(tempE, g1, g2, g3, g4, g5, g6, g7, g8, g9, fastColour )
       
   354 
       
   355 		--AddCaption("fC: " .. fastIndex .. " / " .. fastCount)
       
   356 
       
   357 	else
       
   358 
       
   359 		--AddCaption("excep fC: " .. fastIndex .. " / " .. fastCount)
       
   360 
       
   361 	end
       
   362 
       
   363 
       
   364 
       
   365 end
       
   366 
       
   367 ----------------------------------
       
   368 -- GAME METHODS / EVENT HANDLERS
       
   369 ----------------------------------
       
   370 
       
   371 function onGameInit()
       
   372 	GameFlags = gfInfAttack + gfSolidLand + gfInvulnerable
       
   373 	CaseFreq = 0
       
   374 	Delay = 1000 -- doh
       
   375 end
       
   376 
       
   377 
       
   378 function onGameStart()
       
   379 	RebuildTeamInfo()
       
   380 
       
   381 	ShowMission	(
       
   382 				loc("RACER"),
       
   383 				loc("a Hedgewars mini-game"),
       
   384 				
       
   385 				loc("Build a track and race.") .. "|" ..
       
   386 				loc("Round Limit:") .. " " .. roundLimit .. "|" ..
       
   387 								
       
   388 				"", 4, 4000
       
   389 				)
       
   390 
       
   391 
       
   392 end
       
   393 
       
   394 function PlaceWayPoint(x,y)
       
   395 
       
   396 	if (wpCount < wpLimit) then -- seems to not work with a hedgehog nil chek
       
   397 
       
   398 		wpX[wpCount] = x
       
   399 		wpY[wpCount] = y
       
   400 		wpCol[wpCount] = 0xffffffff
       
   401 		wpCirc[wpCount] = AddVisualGear(wpX[wpCount],wpY[wpCount],vgtCircle,0,true)
       
   402 																		--100	  
       
   403 		SetVisualGearValues(wpCirc[wpCount], wpX[wpCount], wpY[wpCount], 20, 100, 1, 10, 0, wpRad, 5, wpCol[wpCount])
       
   404 
       
   405 		wpCount = wpCount + 1
       
   406 
       
   407 		AddCaption(loc("Waypoint placed.") .. " " .. loc("Available points remaining: ") .. (wpLimit-wpCount))
       
   408 
       
   409 		-- for some reason the code doesnt function if placed here		
       
   410 		--[[if wpCount == wpLimit then
       
   411 			AddCaption(loc("Race complexity limit reached."))
       
   412 			--DisableTumbler()
       
   413 		end]]
       
   414 
       
   415 	end
       
   416 
       
   417 end
       
   418 
       
   419 function onNewTurn()
       
   420 
       
   421 	CheckForNewRound()
       
   422 
       
   423 	racerActive = false
       
   424 	
       
   425 	trackTime = 0
       
   426 
       
   427 	currCount = 0 -- hopefully this solves problem
       
   428 	AddAmmo(CurrentHedgehog, amAirAttack, 0)
       
   429 	gTimer = 0
       
   430 
       
   431 	-- Set the waypoints to unactive on new round
       
   432 	for i = 0,(wpCount-1) do
       
   433 		wpActive[i] = false
       
   434 		wpCol[i] = 0xffffffff
       
   435 		SetVisualGearValues(wpCirc[i], wpX[i], wpY[i], 20, 100, 1, 10, 0, wpRad, 5, wpCol[i])
       
   436 	end
       
   437 
       
   438 	-- Handle Starting Stage of Game
       
   439 	if (gameOver == false) and (gameBegun == false) then
       
   440 		if wpCount >= 3 then
       
   441 			gameBegun = true
       
   442 			roundNumber = 0 
       
   443 			firstClan = GetHogClan(CurrentHedgehog)
       
   444 			ShowMission(loc("RACER"), 
       
   445 			loc("GAME BEGUN!!!"), 
       
   446 			loc("Complete the track as fast as you can!"), 2, 4000)
       
   447 		else
       
   448 			ShowMission(loc("RACER"), 
       
   449 			loc("NOT ENOUGH WAYPOINTS"), 
       
   450 			loc("Place more waypoints using the 'Air Attack' weapon."), 2, 4000)
       
   451 			AddAmmo(CurrentHedgehog, amAirAttack, 4000)
       
   452 		end
       
   453 	end
       
   454 
       
   455 	if gameOver == true then
       
   456 		gameBegun = false
       
   457 		racerActive = false -- newadd
       
   458 	end
       
   459 
       
   460 	AddAmmo(CurrentHedgehog, amTardis, 0)
       
   461 
       
   462 end
       
   463 
       
   464 function onGameTick()
       
   465 
       
   466 	-- convert airstrikes into waypoints
       
   467 	if cGear ~= nil then
       
   468 		x,y = GetGearTarget(cGear)
       
   469 		PlaceWayPoint(x, y)
       
   470 		DeleteGear(cGear)
       
   471 		if wpCount == wpLimit then
       
   472 			AddCaption(loc("Race complexity limit reached."))
       
   473 			DisableTumbler()
       
   474 		end
       
   475 	end
       
   476 
       
   477 
       
   478 
       
   479 	-- start the player tumbling with a boom once their turn has actually begun
       
   480 	if racerActive == false then
       
   481 
       
   482 		if (TurnTimeLeft > 0) and (TurnTimeLeft ~= TurnTime) then
       
   483 			
       
   484 			-- if the gamehas started put the player in the middle of the first
       
   485 			--waypoint that was placed
       
   486 			if gameBegun == true then
       
   487 
       
   488 				AddCaption(loc("Good to go!"))
       
   489 				racerActive = true
       
   490 				trackTime = 0
       
   491 				
       
   492 				SetGearPosition(CurrentHedgehog, wpX[0], wpY[0])
       
   493 				AddGear(GetX(CurrentHedgehog), GetY(CurrentHedgehog), gtGrenade, 0, 0, 0, 1)
       
   494 				FollowGear(CurrentHedgehog)
       
   495 				ShowMission("...", "...", "...", 2, 1)
       
   496 				
       
   497 			else 
       
   498 				-- still in placement mode
       
   499 			end
       
   500 
       
   501 		end
       
   502 	end
       
   503 
       
   504 
       
   505 
       
   506 	-- has the player started his tumbling spree?
       
   507 	if (CurrentHedgehog ~= nil) then
       
   508 
       
   509 		--airstrike conversion used to be here
       
   510 
       
   511 		-- if the RACE has started, show tracktimes and keep tabs on waypoints
       
   512 		if (racerActive == true) and (gameBegun == true) then 
       
   513 
       
   514 			--ghost			
       
   515 			gTimer = gTimer + 1
       
   516 			if gTimer == 15 then
       
   517 				gTimer = 0
       
   518 				HandleGhost()
       
   519 			end
       
   520 
       
   521 			trackTime = trackTime + 1
       
   522 			
       
   523 			wpCheckCounter = wpCheckCounter + 1
       
   524 			if (wpCheckCounter == 100) then
       
   525 			
       
   526 				wpCheckCounter = 0
       
   527 				AddCaption(trackTime/1000,GetClanColor(GetHogClan(CurrentHedgehog)),capgrpMessage2)
       
   528 				
       
   529 				if (CheckWaypoints() == true) then
       
   530 					AdjustScores()
       
   531 					racerActive = false
       
   532 					DisableTumbler()
       
   533 				end
       
   534 				
       
   535 			end
       
   536 
       
   537 		end
       
   538 
       
   539 		
       
   540 
       
   541 		-- if the player has expended his tunbling time, stop him tumbling
       
   542 		if TurnTimeLeft <= 1 then
       
   543 			DisableTumbler()
       
   544 		end
       
   545 
       
   546 	end
       
   547 
       
   548 end
       
   549 
       
   550 function onGearResurrect(gear)
       
   551 
       
   552 	AddVisualGear(GetX(gear), GetY(gear), vgtBigExplosion, 0, false)
       
   553 
       
   554 	-- if the player stops and "dies" or flies into water, stop him racing
       
   555 	if gear == CurrentHedgehog then
       
   556 		DisableTumbler()
       
   557 		ShowMission(loc("RACER"), 
       
   558 		loc("TRACK FAILED!"), 
       
   559 		loc("WINNING TIME: ") .. bestTimeComment, -amSkip, 4000)
       
   560 	end
       
   561 
       
   562 end
       
   563 
       
   564 function onGearAdd(gear)
       
   565 
       
   566 	if GetGearType(gear) == gtHedgehog then
       
   567 		hhs[numhhs] = gear
       
   568 		numhhs = numhhs + 1
       
   569 		SetEffect(gear, heResurrectable, true)
       
   570 	end
       
   571 
       
   572 	if GetGearType(gear) == gtAirAttack then
       
   573 		cGear = gear		
       
   574 	end
       
   575 
       
   576 end
       
   577 
       
   578 function onGearDelete(gear)
       
   579 
       
   580 	if CurrentHedgehog ~= nil then
       
   581 		FollowGear(CurrentHedgehog)
       
   582 	end
       
   583 
       
   584 	if GetGearType(gear) == gtAirAttack then
       
   585 		cGear = nil		
       
   586 	end
       
   587 
       
   588 end
       
   589 
       
   590 function onAmmoStoreInit()
       
   591 	SetAmmo(amRope, 9, 0, 0, 0)	
       
   592 	SetAmmo(amJetpack, 9, 0, 0, 0)	
       
   593 	SetAmmo(amSkip, 9, 0, 0, 0)
       
   594 end
       
   595 
       
   596