share/hedgewars/Data/Missions/Training/Basic_Training_-_Sniper_Rifle.lua
changeset 10655 1c85a442bd56
parent 10289 c3a77ff02a23
child 11015 7a905f0070ce
equal deleted inserted replaced
10654:cdce07f5a011 10655:1c85a442bd56
    15 
    15 
    16 HedgewarsScriptLoad("/Scripts/Locale.lua")
    16 HedgewarsScriptLoad("/Scripts/Locale.lua")
    17 
    17 
    18 -- This variable will hold the number of destroyed targets.
    18 -- This variable will hold the number of destroyed targets.
    19 local score = 0
    19 local score = 0
       
    20 -- This variable will hold the number of shots from the sniper rifle
       
    21 local shots = 0
    20 -- This variable represents the number of targets to destroy.
    22 -- This variable represents the number of targets to destroy.
    21 local score_goal = 31
    23 local score_goal = 31
    22 -- This variable controls how many milliseconds/ticks we'd
    24 -- This variable controls how many milliseconds/ticks we'd
    23 -- like to wait before we end the round once all targets
    25 -- like to wait before we end the round once all targets
    24 -- have been destroyed.
    26 -- have been destroyed.
    94 
    96 
    95 -- This function is called when the round starts
    97 -- This function is called when the round starts
    96 -- it spawns the first target that has to be destroyed.
    98 -- it spawns the first target that has to be destroyed.
    97 -- In addition it shows the scenario goal(s).
    99 -- In addition it shows the scenario goal(s).
    98 function onGameStart()
   100 function onGameStart()
       
   101 	-- Disable graph in stats screen
       
   102 	SendHealthStatsOff()
    99 	-- Spawn the first target.
   103 	-- Spawn the first target.
   100 	spawnTarget(860,1020)
   104 	spawnTarget(860,1020)
   101 	
   105 	
   102 	-- Show some nice mission goals.
   106 	-- Show some nice mission goals.
   103 	-- Parameters are: caption, sub caption, description,
   107 	-- Parameters are: caption, sub caption, description,
   125 		FollowGear(player)
   129 		FollowGear(player)
   126 	end
   130 	end
   127 	-- If time's up, set the game to be lost.
   131 	-- If time's up, set the game to be lost.
   128 	-- We actually check the time to be "1 ms" as it
   132 	-- We actually check the time to be "1 ms" as it
   129 	-- will be at "0 ms" right at the start of the game.
   133 	-- will be at "0 ms" right at the start of the game.
   130 	if TurnTimeLeft < 40 and TurnTimeLeft > 0 and score < score_goal then
   134 	if TurnTimeLeft < 40 and TurnTimeLeft > 0 and score < score_goal and game_lost == false then
   131 		game_lost = true
   135 		game_lost = true
   132 		-- ... and show a short message.
   136 		-- ... and show a short message.
       
   137 		AddCaption(loc("Time's up!"))
   133 		ShowMission(loc("Sniper Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0)
   138 		ShowMission(loc("Sniper Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0)
   134 		-- How about killing our poor hog due to his poor performance?
   139 		-- and generate the stats and go to the stats screen
   135 		SetHealth(player, 0)
   140 		generateStats()
       
   141 		EndGame()
   136 		-- Just to be sure set the goal time to 1 ms
   142 		-- Just to be sure set the goal time to 1 ms
   137 		time_goal = 1
   143 		time_goal = 1
   138 	end
   144 	end
   139 	-- If the goal is reached or we've lost ...
   145 	-- If the goal is reached or we've lost ...
   140 	if score == score_goal or game_lost then
   146 	if score == score_goal or game_lost then
   141 		-- ... check to see if the time we'd like to
   147 		-- ... check to see if the time we'd like to
   142 		-- wait has passed and then ...
   148 		-- wait has passed and then ...
   143 		if end_timer == 0 then
   149 		if end_timer == 0 then
   144 			-- ... end the game ...
   150 			-- ... end the game ...
       
   151 			generateStats()
   145 			EndGame()
   152 			EndGame()
   146 		else
   153 		else
   147 			-- ... or just lower the timer by 1.
   154 			-- ... or just lower the timer by 1.
   148 			-- Reset the time left to stop the timer
   155 			-- Reset the time left to stop the timer
   149 			TurnTimeLeft = time_goal
   156 			TurnTimeLeft = time_goal
   158 	-- add an unlimited supply of shotgun ammo
   165 	-- add an unlimited supply of shotgun ammo
   159 	SetAmmo(amSniperRifle, 9, 0, 0, 0)
   166 	SetAmmo(amSniperRifle, 9, 0, 0, 0)
   160 end
   167 end
   161 
   168 
   162 -- This function is called when a new gear is added.
   169 -- This function is called when a new gear is added.
   163 -- We don't need it for this training, so we can
   170 -- We use it to count the number of shots, which we
   164 -- keep it empty.
   171 -- in turn use to calculate the final score and stats
   165 -- function onGearAdd(gear)
   172 function onGearAdd(gear)
   166 -- end
   173 	if GetGearType(gear) == gtSniperRifleShot then
       
   174 		shots = shots + 1
       
   175 	end
       
   176 end
   167 
   177 
   168 -- This function is called before a gear is destroyed.
   178 -- This function is called before a gear is destroyed.
   169 -- We use it to count the number of targets destroyed.
   179 -- We use it to count the number of targets destroyed.
   170 function onGearDelete(gear)
   180 function onGearDelete(gear)
   171     
   181     
   308 			time_goal = TurnTimeLeft
   318 			time_goal = TurnTimeLeft
   309 			end
   319 			end
   310 		end
   320 		end
   311 	end
   321 	end
   312 end
   322 end
       
   323 
       
   324 -- This function calculates the final score of the player and provides some texts and
       
   325 -- data for the final stats screen
       
   326 function generateStats()
       
   327 	local accuracy = (score/shots)*100
       
   328 	local end_score_targets = (score * 200)
       
   329 	local end_score_overall
       
   330 	if not game_lost then
       
   331 		local end_score_time = math.ceil(time_goal/5)
       
   332 		local end_score_accuracy = math.ceil(accuracy * 100)
       
   333 		end_score_overall = end_score_time + end_score_targets + end_score_accuracy
       
   334 
       
   335 		SendStat(siGameResult, loc("You have successfully finished the sniper rifle training!"))
       
   336 		SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets (+%d points)."), score, score_goal, end_score_targets))
       
   337 		SendStat(siCustomAchievement, string.format(loc("You have made %d shots."), shots))
       
   338 		SendStat(siCustomAchievement, string.format(loc("Accuracy bonus: +%d points"), end_score_accuracy))
       
   339 		SendStat(siCustomAchievement, string.format(loc("You had %.2fs remaining on the clock (+%d points)."), (time_goal/1000), end_score_time))
       
   340 	else
       
   341 		SendStat(siGameResult, loc("You lose!"))
       
   342 	
       
   343 		SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets (+%d points)."), score, score_goal, end_score_targets))
       
   344 		SendStat(siCustomAchievement, string.format(loc("You have made %d shots."), shots))
       
   345 		end_score_overall = end_score_targets
       
   346 	end
       
   347 	SendStat(siPlayerKills, tostring(end_score_overall), loc("Sniperz"))
       
   348 	SendStat(siPointType, loc("points"))
       
   349 end