share/hedgewars/Data/Missions/Training/Sniper_Rifle.lua
changeset 4662 63aafc9c2a81
parent 4661 f5d858e4b634
child 4663 49d89e25f196
equal deleted inserted replaced
4661:f5d858e4b634 4662:63aafc9c2a81
     1 -- Hedgewars SniperRifle Training
       
     2 -- Scripting Example
       
     3 
       
     4 -- Lines such as this one are comments - they are ignored
       
     5 -- by the game, no matter what kind of text is in there.
       
     6 -- It's also possible to place a comment after some real
       
     7 -- instruction as you see below. In short, everything
       
     8 -- following "--" is ignored.
       
     9 
       
    10 ---------------------------------------------------------------
       
    11 -- At first we implement the localization library using loadfile.
       
    12 -- This allows us to localize strings without needing to think
       
    13 -- about translations.
       
    14 -- We can use the function loc(text) to localize a string.
       
    15 
       
    16 loadfile(GetDataPath() .. "Scripts/Locale.lua")()
       
    17 
       
    18 -- This variable will hold the number of destroyed targets.
       
    19 local score = 0
       
    20 -- This variable represents the number of targets to destroy.
       
    21 local score_goal = 31
       
    22 -- This variable controls how many milliseconds/ticks we'd
       
    23 -- like to wait before we end the round once all targets
       
    24 -- have been destroyed.
       
    25 local end_timer = 5000 -- 5000 ms = 5 s
       
    26 -- This variable is set to true if the game is lost (i.e.
       
    27 -- time runs out).
       
    28 local game_lost = false
       
    29 -- This variable will point to the hog's gear
       
    30 local player = nil
       
    31 -- This variable will grab the time left at the end of the round
       
    32 local time_goal = 0
       
    33 
       
    34 local target = nil
       
    35 
       
    36 local last_hit_time = 0
       
    37 -- This is a custom function to make it easier to
       
    38 -- spawn more targets with just one line of code
       
    39 -- You may define as many custom functions as you
       
    40 -- like.
       
    41 function spawnTarget(x, y)
       
    42 	-- add a new target gear
       
    43 	target = AddGear(x, y, gtTarget, 0, 0, 0, 0)
       
    44 	-- have the camera move to the target so the player knows where it is
       
    45 	FollowGear(target)
       
    46 end
       
    47 
       
    48 function blowUp(x, y)
       
    49 	-- adds some TNT
       
    50 	gear = AddGear(x, y, gtDynamite, 0, 0, 0, 0)
       
    51 end
       
    52 
       
    53 -- This function is called before the game loads its
       
    54 -- resources.
       
    55 -- It's one of the predefined function names that will
       
    56 -- be called by the game. They give you entry points
       
    57 -- where you're able to call your own code using either
       
    58 -- provided instructions or custom functions.
       
    59 function onGameInit()
       
    60 	-- At first we have to overwrite/set some global variables
       
    61 	-- that define the map, the game has to load, as well as
       
    62 	-- other things such as the game rules to use, etc.
       
    63 	-- Things we don't modify here will use their default values.
       
    64 
       
    65 	-- The base number for the random number generator
       
    66 	Seed = 0
       
    67 	-- Game settings and rules
       
    68 	GameFlags = gfMultiWeapon + gfOneClanMode + gfArtillery
       
    69 	-- The time the player has to move each round (in ms)
       
    70 	TurnTime = 150000
       
    71 	-- The frequency of crate drops
       
    72 	CaseFreq = 0
       
    73 	-- The number of mines being placed
       
    74 	MinesNum = 0
       
    75 	-- The number of explosives being placed
       
    76 	Explosives = 0
       
    77 	-- The delay between each round
       
    78 	Delay = 0
       
    79 	-- The map to be played
       
    80 	Map = "Ropes"
       
    81 	-- The theme to be used
       
    82 	Theme = "City"
       
    83 
       
    84 	-- Create the player team
       
    85 	AddTeam(loc("Sniperz"), 14483456, "Simple", "Island", "Default")
       
    86 	-- And add a hog to it
       
    87 	player = AddHog(loc("Hunter"), 0, 1, "Sniper")
       
    88 	SetGearPosition(player, 602, 1465)
       
    89 end
       
    90 
       
    91 -- This function is called when the round starts
       
    92 -- it spawns the first target that has to be destroyed.
       
    93 -- In addition it shows the scenario goal(s).
       
    94 function onGameStart()
       
    95 	-- Spawn the first target.
       
    96 	spawnTarget(860,1020)
       
    97 	
       
    98 	-- Show some nice mission goals.
       
    99 	-- Parameters are: caption, sub caption, description,
       
   100 	-- extra text, icon and time to show.
       
   101 	-- A negative icon parameter (-n) represents the n-th weapon icon
       
   102 	-- A positive icon paramter (n) represents the (n+1)-th mission icon
       
   103 	-- A timeframe of 0 is replaced with the default time to show.
       
   104 	ShowMission(loc("Sniper Training"), loc("Aiming Practice"), loc("Eliminate all targets before your time runs out.|You have unlimited ammo for this mission."), -amSniperRifle, 0)
       
   105 end
       
   106 
       
   107 -- This function is called every game tick.
       
   108 -- Note that there are 1000 ticks within one second.
       
   109 -- You shouldn't try to calculate too complicated
       
   110 -- code here as this might slow down your game.
       
   111 function onGameTick()
       
   112 	if game_lost then
       
   113 		return
       
   114 	end
       
   115 	-- after a target is destroyed, show hog, then target
       
   116 	if (target ~= nil) and (TurnTimeLeft + 1300 < last_hit_time) then
       
   117 		-- move camera to the target
       
   118 		FollowGear(target)
       
   119 	elseif TurnTimeLeft + 300 < last_hit_time then
       
   120 		-- move camera to the hog
       
   121 		FollowGear(player)
       
   122 	end
       
   123 	-- If time's up, set the game to be lost.
       
   124 	-- We actually check the time to be "1 ms" as it
       
   125 	-- will be at "0 ms" right at the start of the game.
       
   126 	if TurnTimeLeft == 1 and score < score_goal then
       
   127 		game_lost = true
       
   128 		-- ... and show a short message.
       
   129 		ShowMission(loc("Sniper Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0)
       
   130 		-- How about killing our poor hog due to his poor performance?
       
   131 		SetHealth(player, 0)
       
   132 		-- Just to be sure set the goal time to 1 ms
       
   133 		time_goal = 1
       
   134 	end
       
   135 	-- If the goal is reached or we've lost ...
       
   136 	if score == score_goal or game_lost then
       
   137 		-- ... check to see if the time we'd like to
       
   138 		-- wait has passed and then ...
       
   139 		if end_timer == 0 then
       
   140 			-- ... end the game ...
       
   141 			EndGame()
       
   142 		else
       
   143 			-- ... or just lower the timer by 1.
       
   144 			end_timer = end_timer - 1
       
   145 			-- Reset the time left to stop the timer
       
   146 			TurnTimeLeft = time_goal
       
   147 		end
       
   148 	end
       
   149 end
       
   150 
       
   151 -- This function is called when the game is initialized
       
   152 -- to request the available ammo and probabilities
       
   153 function onAmmoStoreInit()
       
   154 	-- add an unlimited supply of shotgun ammo
       
   155 	SetAmmo(amSniperRifle, 9, 0, 0, 0)
       
   156 end
       
   157 
       
   158 -- This function is called when a new gear is added.
       
   159 -- We don't need it for this training, so we can
       
   160 -- keep it empty.
       
   161 function onGearAdd(gear)
       
   162 end
       
   163 
       
   164 -- This function is called before a gear is destroyed.
       
   165 -- We use it to count the number of targets destroyed.
       
   166 function onGearDelete(gear)
       
   167     
       
   168 	if GetGearType(gear) == gtCase then
       
   169 		game_lost = true
       
   170 		return
       
   171 	end
       
   172 	
       
   173 	if (GetGearType(gear) == gtTarget) then
       
   174 		-- remember when the target was hit for adjusting the camera
       
   175 		last_hit_time = TurnTimeLeft
       
   176 		-- Add one point to our score/counter
       
   177 		score = score + 1
       
   178 		-- If we haven't reached the goal ...
       
   179 		if score < score_goal then
       
   180 			-- ... spawn another target.
       
   181 			if score == 1 then
       
   182 				spawnTarget(1520,1350)
       
   183 			elseif score == 2 then
       
   184 				spawnTarget(1730,1040)
       
   185 			elseif score == 3 then
       
   186 				spawnTarget(2080,780)
       
   187 			elseif score == 4 then
       
   188 				blowUp(1730,1226)
       
   189 				blowUp(1440,1595)
       
   190 				blowUp(1527,1575)
       
   191 				blowUp(1614,1595)
       
   192 				blowUp(1420,1675)
       
   193 				blowUp(1527,1675)
       
   194 				blowUp(1634,1675)
       
   195 				blowUp(1440,1755)
       
   196 				blowUp(1527,1775)
       
   197 				blowUp(1614,1755)
       
   198 				spawnTarget(1527,1667)
       
   199 			elseif score == 5 then
       
   200 				spawnTarget(1527,1667)
       
   201 			elseif score == 6 then
       
   202 				spawnTarget(2175,1300)
       
   203 			elseif score == 7 then
       
   204 				spawnTarget(2250,940)
       
   205 			elseif score == 8 then
       
   206 				spawnTarget(2665,1540)
       
   207 			elseif score == 9 then
       
   208 				spawnTarget(3040,1160)
       
   209 			elseif score == 10 then
       
   210 				spawnTarget(2930,1500)
       
   211 			elseif score == 11 then
       
   212 				spawnTarget(700,720)
       
   213 			elseif score == 12 then
       
   214 				blowUp(914,1222)
       
   215 				blowUp(1050,1222)
       
   216 				blowUp(1160,1008)
       
   217 				blowUp(1160,1093)
       
   218 				blowUp(1160,1188)
       
   219 				blowUp(375,911)
       
   220 				blowUp(510,911)
       
   221 				blowUp(640,911)
       
   222 				blowUp(780,911)
       
   223 				blowUp(920,911)
       
   224 				blowUp(1060,913)
       
   225 				blowUp(1198,913)
       
   226 				spawnTarget(1200,730)
       
   227 			elseif score == 13 then
       
   228 				spawnTarget(1200,830)
       
   229 			elseif score == 14 then
       
   230 				spawnTarget(1430,450)
       
   231 			elseif score == 15 then
       
   232 				spawnTarget(796,240)
       
   233 			elseif score == 16 then
       
   234 				spawnTarget(300,10)
       
   235 			elseif score == 17 then
       
   236 				spawnTarget(2080,820)
       
   237 			elseif score == 18 then
       
   238 				blowUp(2110,920)
       
   239 				blowUp(2210,920)
       
   240 				blowUp(2200,305)
       
   241 				blowUp(2300,305)
       
   242 				blowUp(2300,400)
       
   243 				blowUp(2300,500)
       
   244 				blowUp(2300,600)
       
   245 				blowUp(2300,700)
       
   246 				blowUp(2300,800)
       
   247 				blowUp(2300,900)
       
   248 				blowUp(2401,305)
       
   249 				blowUp(2532,305)
       
   250 				blowUp(2663,305)
       
   251 				spawnTarget(2300,760)
       
   252 			elseif score == 19 then
       
   253 				spawnTarget(2300,760)
       
   254 			elseif score == 20 then
       
   255 				spawnTarget(2738,190)
       
   256 			elseif score == 21 then
       
   257 				spawnTarget(2590,-100)
       
   258 			elseif score == 22 then
       
   259 				blowUp(2790,305)
       
   260 				blowUp(2930,305)
       
   261 				blowUp(3060,305)
       
   262 				blowUp(3190,305)
       
   263 				blowUp(3310,305)
       
   264 				blowUp(3393,613)
       
   265 				blowUp(2805,370)
       
   266 				blowUp(2805,500)
       
   267 				blowUp(2805,630)
       
   268 				blowUp(2805,760)
       
   269 				blowUp(2805,890)
       
   270 				blowUp(2700,890)
       
   271 				blowUp(3258,370)
       
   272 				blowUp(3258,475)
       
   273 				blowUp(3264,575)
       
   274 				spawnTarget(3230,240)
       
   275 			elseif score == 23 then
       
   276 				spawnTarget(3230,290)
       
   277 			elseif score == 24 then
       
   278 				spawnTarget(3670,250)
       
   279 			elseif score == 25 then
       
   280 				spawnTarget(2620,-100)
       
   281 			elseif score == 26 then
       
   282 				spawnTarget(2870,300)
       
   283 			elseif score == 27 then
       
   284 				spawnTarget(3850,900)
       
   285 			elseif score == 28 then
       
   286 				spawnTarget(3780,300)
       
   287 			elseif score == 29 then
       
   288 				spawnTarget(3670,0)
       
   289 			elseif score == 30 then
       
   290 				spawnTarget(3480,1200)
       
   291 			end
       
   292 		else
       
   293 			if not game_lost then
       
   294 			-- Otherwise show that the goal was accomplished
       
   295 			ShowMission(loc("Sniper Training"), loc("Aiming Practice"), loc("Congratulations! You've eliminated all targets|within the allowed time frame."), 0, 0)
       
   296 			-- Also let the hogs shout "victory!"
       
   297 			PlaySound(sndVictory)
       
   298 			-- Save the time left so we may keep it.
       
   299 			time_goal = TurnTimeLeft
       
   300 			end
       
   301 		end
       
   302 	end
       
   303 end