|
1 -- Hedgewars Grenade 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 = 5 |
|
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 = 4000 -- 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 ensures that the death function isn't called |
|
30 -- repeatedly when game is over. |
|
31 local team_death = false |
|
32 -- This variable will point to the hog's gear |
|
33 local player = nil |
|
34 -- This variable will grab the time left at the end of the round |
|
35 local time_goal = 0 |
|
36 |
|
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() |
|
42 -- add a new target gear |
|
43 gear = AddGear(0, 0, gtTarget, 0, 0, 0, 0) |
|
44 |
|
45 -- move it to a random position within 0 and |
|
46 -- LAND_WIDTH - the width of the map |
|
47 FindPlace(gear, true, 0, LAND_WIDTH-1350) |
|
48 |
|
49 -- move the target to a higher vertical position |
|
50 -- to ensure it's not somewhere down below |
|
51 x, y = GetGearPosition(gear) |
|
52 SetGearPosition(gear, x, 500) |
|
53 end |
|
54 |
|
55 -- This function is called before the game loads its |
|
56 -- resources. |
|
57 -- It's one of the predefined function names that will |
|
58 -- be called by the game. They give you entry points |
|
59 -- where you're able to call your own code using either |
|
60 -- provided instructions or custom functions. |
|
61 function onGameInit() |
|
62 -- At first we have to overwrite/set some global variables |
|
63 -- that define the map, the game has to load, as well as |
|
64 -- other things such as the game rules to use, etc. |
|
65 -- Things we don't modify here will use their default values. |
|
66 |
|
67 -- The base number for the random number generator |
|
68 Seed = 1 |
|
69 -- Game settings and rules |
|
70 GameFlags = gfInfAttack + gfOneClanMode |
|
71 -- The time the player has to move each round (in ms) |
|
72 TurnTime = 60000 |
|
73 -- The frequency of crate drops |
|
74 CaseFreq = 0 |
|
75 -- The number of mines being placed |
|
76 MinesNum = 0 |
|
77 -- The number of explosives being placed |
|
78 Explosives = 0 |
|
79 -- The delay between each round |
|
80 Delay = 1 |
|
81 -- The map to be played |
|
82 Map = "Battlefield" |
|
83 -- The theme to be used |
|
84 Theme = "Castle" |
|
85 |
|
86 -- Create the player team |
|
87 AddTeam(loc("Grenadiers"), 14483456, "Simple", "Island", "Default") |
|
88 -- And add a hog to it |
|
89 player = AddHog(loc("Nade Boy"), 0, 1, "war_grenadier1") |
|
90 SetGearPosition(player, 1530, 1100) |
|
91 end |
|
92 |
|
93 -- This function is called when the round starts |
|
94 -- it spawns the first target that has to be destroyed. |
|
95 -- In addition it shows the scenario goal(s). |
|
96 function onGameStart() |
|
97 -- Spawn the first target. |
|
98 spawnTarget() |
|
99 |
|
100 -- Show some nice mission goals. |
|
101 -- Parameters are: caption, sub caption, description, |
|
102 -- extra text, icon and time to show. |
|
103 -- A negative icon parameter (-n) represents the n-th weapon icon |
|
104 -- A positive icon paramter (n) represents the (n+1)-th mission icon |
|
105 -- A timeframe of 0 is replaced with the default time to show. |
|
106 ShowMission(loc("Grenade Training"), loc("Aiming Practice"), loc("Eliminate all targets before your time runs out.|You have unlimited ammo for this mission."), -amGrenade, 0) |
|
107 end |
|
108 |
|
109 function onNewTurn() |
|
110 ParseCommand("setweap " .. string.char(amGrenade)) |
|
111 end |
|
112 |
|
113 -- This function is called every game tick. |
|
114 -- Note that there are 1000 ticks within one second. |
|
115 -- You shouldn't try to calculate too complicated |
|
116 -- code here as this might slow down your game. |
|
117 function onGameTick() |
|
118 -- If time's up, set the game to be lost. |
|
119 -- We actually check the time to be "1 ms" as it |
|
120 -- will be at "0 ms" right at the start of the game. |
|
121 if TurnTimeLeft == 1 and score < score_goal then |
|
122 game_lost = true |
|
123 -- ... and show a short message. |
|
124 ShowMission(loc("Grenade Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0) |
|
125 -- How about killing our poor hog due to his poor performance? |
|
126 SetHealth(player, 0) |
|
127 -- Just to be sure set the goal time to 1 ms |
|
128 time_goal = 1 |
|
129 end |
|
130 -- If the goal is reached or we've lost ... |
|
131 if score == score_goal or game_lost then |
|
132 -- ... check to see if the time we'd like to |
|
133 -- wait has passed and then ... |
|
134 if end_timer == 0 then |
|
135 -- Override the 'Draw' message with the appropriate message. |
|
136 if game_lost then |
|
137 AddCaption("Mission lost!", 0xffba00ff,capgrpGameState) |
|
138 else |
|
139 AddCaption("Mission won!", 0xffba00ff,capgrpGameState) |
|
140 end |
|
141 -- Remove the team to end the game. Only do this once. |
|
142 if team_death == false then |
|
143 team_death = true |
|
144 ParseCommand("teamgone " .. "Grenadiers") |
|
145 end |
|
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 bazooka ammo |
|
159 SetAmmo(amGrenade, 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 -- We're only interested in target gears. |
|
172 if GetGearType(gear) == gtTarget then |
|
173 -- Add one point to our score/counter |
|
174 score = score + 1 |
|
175 -- If we haven't reached the goal ... |
|
176 if score < score_goal then |
|
177 -- ... spawn another target. |
|
178 spawnTarget() |
|
179 else |
|
180 if not game_lost then |
|
181 -- Otherwise show that the goal was accomplished |
|
182 ShowMission(loc("Grenade Training"), loc("Aiming Practice"), loc("Congratulations! You've eliminated all targets|within the allowed time frame."), 0, 0) |
|
183 -- Also let the hogs shout "victory!" |
|
184 PlaySound(sndVictory) |
|
185 -- Save the time left so we may keep it. |
|
186 time_goal = TurnTimeLeft |
|
187 end |
|
188 end |
|
189 end |
|
190 end |