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