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 HedgewarsScriptLoad("/Scripts/Locale.lua") |
|
17 |
|
18 -- This variable will hold the number of destroyed targets. |
|
19 local score = 0 |
|
20 -- This variable will hold the number of shots from the sniper rifle |
|
21 local shots = 0 |
|
22 -- This variable represents the number of targets to destroy. |
|
23 local score_goal = 31 |
|
24 -- This variable controls how many milliseconds/ticks we'd |
|
25 -- like to wait before we end the round once all targets |
|
26 -- have been destroyed. |
|
27 local end_timer = 1000 -- 1000 ms = 1 s |
|
28 -- This variable is set to true if the game is lost (i.e. |
|
29 -- time runs out). |
|
30 local game_lost = false |
|
31 -- This variable will point to the hog's gear |
|
32 local player = nil |
|
33 -- This variable will grab the time left at the end of the round |
|
34 local time_goal = 0 |
|
35 |
|
36 local target = nil |
|
37 |
|
38 local last_hit_time = 0 |
|
39 |
|
40 local cinematic = false |
|
41 |
|
42 -- This is a custom function to make it easier to |
|
43 -- spawn more targets with just one line of code |
|
44 -- You may define as many custom functions as you |
|
45 -- like. |
|
46 function spawnTarget(x, y) |
|
47 -- add a new target gear |
|
48 target = AddGear(x, y, gtTarget, 0, 0, 0, 0) |
|
49 -- have the camera move to the target so the player knows where it is |
|
50 FollowGear(target) |
|
51 end |
|
52 |
|
53 function blowUp(x, y) |
|
54 if cinematic == false then |
|
55 cinematic = true |
|
56 SetCinematicMode(true) |
|
57 end |
|
58 -- adds some TNT |
|
59 gear = AddGear(x, y, gtDynamite, 0, 0, 0, 0) |
|
60 end |
|
61 |
|
62 function onNewTurn() |
|
63 SetWeapon(amSniperRifle) |
|
64 end |
|
65 |
|
66 -- This function is called before the game loads its |
|
67 -- resources. |
|
68 -- It's one of the predefined function names that will |
|
69 -- be called by the game. They give you entry points |
|
70 -- where you're able to call your own code using either |
|
71 -- provided instructions or custom functions. |
|
72 function onGameInit() |
|
73 -- At first we have to overwrite/set some global variables |
|
74 -- that define the map, the game has to load, as well as |
|
75 -- other things such as the game rules to use, etc. |
|
76 -- Things we don't modify here will use their default values. |
|
77 |
|
78 -- The base number for the random number generator |
|
79 Seed = 0 |
|
80 -- Game settings and rules |
|
81 GameFlags = gfMultiWeapon + gfOneClanMode + gfArtillery |
|
82 -- The time the player has to move each round (in ms) |
|
83 TurnTime = 150000 |
|
84 -- The frequency of crate drops |
|
85 CaseFreq = 0 |
|
86 -- The number of mines being placed |
|
87 MinesNum = 0 |
|
88 -- The number of explosives being placed |
|
89 Explosives = 0 |
|
90 -- The delay between each round |
|
91 Delay = 0 |
|
92 -- The map to be played |
|
93 Map = "Ropes" |
|
94 -- The theme to be used |
|
95 Theme = "Golf" |
|
96 |
|
97 -- Create the player team |
|
98 AddTeam(loc("Sniperz"), 14483456, "Simple", "Island", "Default") |
|
99 -- And add a hog to it |
|
100 player = AddHog(loc("Hunter"), 0, 1, "Sniper") |
|
101 SetGearPosition(player, 602, 1465) |
|
102 end |
|
103 |
|
104 -- This function is called when the round starts |
|
105 -- it spawns the first target that has to be destroyed. |
|
106 -- In addition it shows the scenario goal(s). |
|
107 function onGameStart() |
|
108 -- Disable graph in stats screen |
|
109 SendHealthStatsOff() |
|
110 -- Spawn the first target. |
|
111 spawnTarget(860,1020) |
|
112 |
|
113 -- Show some nice mission goals. |
|
114 -- Parameters are: caption, sub caption, description, |
|
115 -- extra text, icon and time to show. |
|
116 -- A negative icon parameter (-n) represents the n-th weapon icon |
|
117 -- A positive icon paramter (n) represents the (n+1)-th mission icon |
|
118 -- A timeframe of 0 is replaced with the default time to show. |
|
119 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) |
|
120 end |
|
121 |
|
122 -- This function is called every game tick. |
|
123 -- Note that there are 1000 ticks within one second. |
|
124 -- You shouldn't try to calculate too complicated |
|
125 -- code here as this might slow down your game. |
|
126 function onGameTick20() |
|
127 if game_lost then |
|
128 return |
|
129 end |
|
130 -- after a target is destroyed, show hog, then target |
|
131 if (target ~= nil) and (TurnTimeLeft + 1300 < last_hit_time) then |
|
132 -- move camera to the target |
|
133 FollowGear(target) |
|
134 elseif TurnTimeLeft + 300 < last_hit_time then |
|
135 -- move camera to the hog |
|
136 FollowGear(player) |
|
137 end |
|
138 -- If time's up, set the game to be lost. |
|
139 -- We actually check the time to be "1 ms" as it |
|
140 -- will be at "0 ms" right at the start of the game. |
|
141 if TurnTimeLeft < 40 and TurnTimeLeft > 0 and score < score_goal and game_lost == false then |
|
142 game_lost = true |
|
143 -- ... and show a short message. |
|
144 AddCaption(loc("Time's up!")) |
|
145 ShowMission(loc("Sniper Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0) |
|
146 -- and generate the stats and go to the stats screen |
|
147 generateStats() |
|
148 EndGame() |
|
149 -- Just to be sure set the goal time to 1 ms |
|
150 time_goal = 1 |
|
151 end |
|
152 -- If the goal is reached or we've lost ... |
|
153 if score == score_goal or game_lost then |
|
154 -- ... check to see if the time we'd like to |
|
155 -- wait has passed and then ... |
|
156 if end_timer == 0 then |
|
157 -- ... end the game ... |
|
158 generateStats() |
|
159 EndGame() |
|
160 else |
|
161 -- ... or just lower the timer by 1. |
|
162 -- Reset the time left to stop the timer |
|
163 TurnTimeLeft = time_goal |
|
164 end |
|
165 end_timer = end_timer - 20 |
|
166 end |
|
167 end |
|
168 |
|
169 -- This function is called when the game is initialized |
|
170 -- to request the available ammo and probabilities |
|
171 function onAmmoStoreInit() |
|
172 -- add an unlimited supply of shotgun ammo |
|
173 SetAmmo(amSniperRifle, 9, 0, 0, 0) |
|
174 end |
|
175 |
|
176 -- This function is called when a new gear is added. |
|
177 -- We use it to count the number of shots, which we |
|
178 -- in turn use to calculate the final score and stats |
|
179 function onGearAdd(gear) |
|
180 if GetGearType(gear) == gtSniperRifleShot then |
|
181 shots = shots + 1 |
|
182 end |
|
183 end |
|
184 |
|
185 -- This function is called before a gear is destroyed. |
|
186 -- We use it to count the number of targets destroyed. |
|
187 function onGearDelete(gear) |
|
188 local gt = GetGearType(gear) |
|
189 |
|
190 if gt == gtCase then |
|
191 game_lost = true |
|
192 return |
|
193 end |
|
194 |
|
195 if (gt == gtDynamite) and cinematic then |
|
196 cinematic = false |
|
197 SetCinematicMode(false) |
|
198 return |
|
199 end |
|
200 |
|
201 if gt == gtTarget then |
|
202 -- remember when the target was hit for adjusting the camera |
|
203 last_hit_time = TurnTimeLeft |
|
204 -- Add one point to our score/counter |
|
205 score = score + 1 |
|
206 -- If we haven't reached the goal ... |
|
207 if score < score_goal then |
|
208 -- ... spawn another target. |
|
209 if score == 1 then |
|
210 spawnTarget(1520,1350) |
|
211 elseif score == 2 then |
|
212 spawnTarget(1730,1040) |
|
213 elseif score == 3 then |
|
214 spawnTarget(2080,780) |
|
215 elseif score == 4 then |
|
216 AddCaption(loc("Good so far!") .. " " .. loc("Keep it up!")); |
|
217 blowUp(1730,1226) |
|
218 blowUp(1440,1595) |
|
219 blowUp(1527,1575) |
|
220 blowUp(1614,1595) |
|
221 blowUp(1420,1675) |
|
222 blowUp(1527,1675) |
|
223 blowUp(1634,1675) |
|
224 blowUp(1440,1755) |
|
225 blowUp(1527,1775) |
|
226 blowUp(1614,1755) |
|
227 spawnTarget(1527,1667) |
|
228 elseif score == 5 then |
|
229 spawnTarget(1527,1667) |
|
230 elseif score == 6 then |
|
231 spawnTarget(2175,1300) |
|
232 elseif score == 7 then |
|
233 spawnTarget(2250,940) |
|
234 elseif score == 8 then |
|
235 spawnTarget(2665,1540) |
|
236 elseif score == 9 then |
|
237 spawnTarget(3040,1160) |
|
238 elseif score == 10 then |
|
239 spawnTarget(2930,1500) |
|
240 elseif score == 11 then |
|
241 AddCaption(loc("This one's tricky.")); |
|
242 spawnTarget(700,720) |
|
243 elseif score == 12 then |
|
244 AddCaption(loc("Well done.")); |
|
245 blowUp(914,1222) |
|
246 blowUp(1050,1222) |
|
247 blowUp(1160,1008) |
|
248 blowUp(1160,1093) |
|
249 blowUp(1160,1188) |
|
250 blowUp(375,911) |
|
251 blowUp(510,911) |
|
252 blowUp(640,911) |
|
253 blowUp(780,911) |
|
254 blowUp(920,911) |
|
255 blowUp(1060,913) |
|
256 blowUp(1198,913) |
|
257 spawnTarget(1200,730) |
|
258 elseif score == 13 then |
|
259 spawnTarget(1200,830) |
|
260 elseif score == 14 then |
|
261 spawnTarget(1430,450) |
|
262 elseif score == 15 then |
|
263 spawnTarget(796,240) |
|
264 elseif score == 16 then |
|
265 spawnTarget(300,10) |
|
266 elseif score == 17 then |
|
267 spawnTarget(2080,820) |
|
268 elseif score == 18 then |
|
269 AddCaption(loc("Demolition is fun!")); |
|
270 blowUp(2110,920) |
|
271 blowUp(2210,920) |
|
272 blowUp(2200,305) |
|
273 blowUp(2300,305) |
|
274 blowUp(2300,400) |
|
275 blowUp(2300,500) |
|
276 blowUp(2300,600) |
|
277 blowUp(2300,700) |
|
278 blowUp(2300,800) |
|
279 blowUp(2300,900) |
|
280 blowUp(2401,305) |
|
281 blowUp(2532,305) |
|
282 blowUp(2663,305) |
|
283 spawnTarget(2300,760) |
|
284 elseif score == 19 then |
|
285 spawnTarget(2300,760) |
|
286 elseif score == 20 then |
|
287 spawnTarget(2738,190) |
|
288 elseif score == 21 then |
|
289 spawnTarget(2590,-100) |
|
290 elseif score == 22 then |
|
291 AddCaption(loc("Will this ever end?")); |
|
292 blowUp(2790,305) |
|
293 blowUp(2930,305) |
|
294 blowUp(3060,305) |
|
295 blowUp(3190,305) |
|
296 blowUp(3310,305) |
|
297 blowUp(3393,613) |
|
298 blowUp(2805,370) |
|
299 blowUp(2805,500) |
|
300 blowUp(2805,630) |
|
301 blowUp(2805,760) |
|
302 blowUp(2805,890) |
|
303 blowUp(3258,370) |
|
304 blowUp(3258,475) |
|
305 blowUp(3264,575) |
|
306 spawnTarget(3230,240) |
|
307 elseif score == 23 then |
|
308 spawnTarget(3230,290) |
|
309 elseif score == 24 then |
|
310 spawnTarget(3670,250) |
|
311 elseif score == 25 then |
|
312 spawnTarget(2620,-100) |
|
313 elseif score == 26 then |
|
314 spawnTarget(2870,300) |
|
315 elseif score == 27 then |
|
316 spawnTarget(3850,900) |
|
317 elseif score == 28 then |
|
318 spawnTarget(3780,300) |
|
319 elseif score == 29 then |
|
320 spawnTarget(3670,0) |
|
321 elseif score == 30 then |
|
322 AddCaption(loc("Last Target!")); |
|
323 spawnTarget(3480,1200) |
|
324 end |
|
325 else |
|
326 if not game_lost then |
|
327 -- Otherwise show that the goal was accomplished |
|
328 ShowMission(loc("Sniper Training"), loc("Aiming Practice"), loc("Congratulations! You've eliminated all targets|within the allowed time frame."), 0, 0) |
|
329 -- Also let the hogs shout "victory!" |
|
330 PlaySound(sndVictory) |
|
331 -- Save the time left so we may keep it. |
|
332 time_goal = TurnTimeLeft |
|
333 end |
|
334 end |
|
335 end |
|
336 end |
|
337 |
|
338 -- This function calculates the final score of the player and provides some texts and |
|
339 -- data for the final stats screen |
|
340 function generateStats() |
|
341 local accuracy = (score/shots)*100 |
|
342 local end_score_targets = (score * 200) |
|
343 local end_score_overall |
|
344 if not game_lost then |
|
345 local end_score_time = math.ceil(time_goal/5) |
|
346 local end_score_accuracy = math.ceil(accuracy * 100) |
|
347 end_score_overall = end_score_time + end_score_targets + end_score_accuracy |
|
348 |
|
349 SendStat(siGameResult, loc("You have successfully finished the sniper rifle training!")) |
|
350 SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets (+%d points)."), score, score_goal, end_score_targets)) |
|
351 SendStat(siCustomAchievement, string.format(loc("You have made %d shots."), shots)) |
|
352 SendStat(siCustomAchievement, string.format(loc("Accuracy bonus: +%d points"), end_score_accuracy)) |
|
353 SendStat(siCustomAchievement, string.format(loc("You had %.2fs remaining on the clock (+%d points)."), (time_goal/1000), end_score_time)) |
|
354 else |
|
355 SendStat(siGameResult, loc("You lose!")) |
|
356 |
|
357 SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets (+%d points)."), score, score_goal, end_score_targets)) |
|
358 SendStat(siCustomAchievement, string.format(loc("You have made %d shots."), shots)) |
|
359 end_score_overall = end_score_targets |
|
360 end |
|
361 SendStat(siPlayerKills, tostring(end_score_overall), loc("Sniperz")) |
|
362 SendStat(siPointType, loc("points")) |
|
363 end |
|