author | Wuzzy <almikes@aol.com> |
Tue, 11 Apr 2017 02:44:59 +0200 | |
changeset 12224 | d62d6f8ebef1 |
parent 12049 | 030464f34d47 |
child 12345 | b0293e4ef0de |
permissions | -rw-r--r-- |
11015 | 1 |
-- Hedgewars Bazooka 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 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 = 1000 -- 1000 ms = 1 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 |
-- This variable stores the number of bazooka shots |
|
34 |
local shots = 0 |
|
35 |
||
36 |
-- This is a custom function to make it easier to |
|
37 |
-- spawn more targets with just one line of code |
|
38 |
-- You may define as many custom functions as you |
|
39 |
-- like. |
|
40 |
function spawnTarget() |
|
41 |
-- add a new target gear |
|
42 |
gear = AddGear(0, 0, gtTarget, 0, 0, 0, 0) |
|
43 |
||
44 |
-- move it to a random position within 0 and |
|
45 |
-- LAND_WIDTH - the width of the map |
|
46 |
FindPlace(gear, true, 0, LAND_WIDTH) |
|
47 |
||
48 |
-- move the target to a higher vertical position |
|
49 |
-- to ensure it's not somewhere down below |
|
50 |
x, y = GetGearPosition(gear) |
|
51 |
SetGearPosition(gear, x, 0) |
|
52 |
end |
|
53 |
||
54 |
-- This function is called before the game loads its |
|
55 |
-- resources. |
|
56 |
-- It's one of the predefined function names that will |
|
57 |
-- be called by the game. They give you entry points |
|
58 |
-- where you're able to call your own code using either |
|
59 |
-- provided instructions or custom functions. |
|
60 |
function onGameInit() |
|
61 |
-- At first we have to overwrite/set some global variables |
|
62 |
-- that define the map, the game has to load, as well as |
|
63 |
-- other things such as the game rules to use, etc. |
|
64 |
-- Things we don't modify here will use their default values. |
|
65 |
||
66 |
-- The base number for the random number generator |
|
67 |
Seed = 1 |
|
68 |
-- Game settings and rules |
|
69 |
EnableGameFlags(gfMultiWeapon, gfOneClanMode, gfSolidLand) |
|
70 |
-- Uncommenting this wouldn't do anything |
|
71 |
--EnableGameFlags(gfMultiWeapon, gfOneClanMode, gfSolidLand) |
|
72 |
-- Neither this |
|
73 |
--DisableGameFlags(gfArtillery) |
|
74 |
-- Uncommenting this would make the terrain damageable |
|
75 |
--DisableGameFlags(gfSolidLand) |
|
76 |
-- Uncommenting this would remove all flags set previously |
|
77 |
--ClearGameFlags() |
|
78 |
-- The time the player has to move each round (in ms) |
|
79 |
TurnTime = 60000 |
|
80 |
-- The frequency of crate drops |
|
81 |
CaseFreq = 0 |
|
82 |
-- The number of mines being placed |
|
83 |
MinesNum = 0 |
|
84 |
-- The number of explosives being placed |
|
85 |
Explosives = 0 |
|
86 |
-- The delay between each round |
|
87 |
Delay = 0 |
|
88 |
-- The map to be played |
|
89 |
Map = "Bamboo" |
|
90 |
-- The theme to be used |
|
91 |
Theme = "Bamboo" |
|
12224
d62d6f8ebef1
Disable Sudden Death consistently in all missions which don't require it
Wuzzy <almikes@aol.com>
parents:
12049
diff
changeset
|
92 |
-- Setting these 2 values to 0 is the official way to disable Sudden Death cleanly |
d62d6f8ebef1
Disable Sudden Death consistently in all missions which don't require it
Wuzzy <almikes@aol.com>
parents:
12049
diff
changeset
|
93 |
HealthDecrease = 0 -- Sudden Death damage |
d62d6f8ebef1
Disable Sudden Death consistently in all missions which don't require it
Wuzzy <almikes@aol.com>
parents:
12049
diff
changeset
|
94 |
WaterRise = 0 -- Water rise in Sudden Death |
11015 | 95 |
|
96 |
-- Create the player team |
|
12049
030464f34d47
Tweak flags used in all missions to fit more to the theme
Wuzzy <almikes@aol.com>
parents:
11242
diff
changeset
|
97 |
AddTeam(loc("'Zooka Team"), 14483456, "Simple", "Island", "Default", "cm_crosshair") |
11015 | 98 |
-- And add a hog to it |
99 |
player = AddHog(loc("Hunter"), 0, 1, "NoHat") |
|
100 |
SetGearPosition(player, 936, 136) |
|
101 |
end |
|
102 |
||
103 |
-- This function is called when the round starts |
|
104 |
-- it spawns the first target that has to be destroyed. |
|
105 |
-- In addition it shows the scenario goal(s). |
|
106 |
function onGameStart() |
|
107 |
-- Disable the graph in the stats screen, we don't need it |
|
108 |
SendHealthStatsOff() |
|
109 |
-- Spawn the first target. |
|
110 |
spawnTarget() |
|
111 |
||
112 |
-- Show some nice mission goals. |
|
113 |
-- Parameters are: caption, sub caption, description, |
|
114 |
-- extra text, icon and time to show. |
|
115 |
-- A negative icon parameter (-n) represents the n-th weapon icon |
|
116 |
-- A positive icon paramter (n) represents the (n+1)-th mission icon |
|
117 |
-- A timeframe of 0 is replaced with the default time to show. |
|
118 |
ShowMission(loc("Bazooka Training"), loc("Aiming Practice"), loc("Eliminate all targets before your time runs out.|You have unlimited ammo for this mission."), -amBazooka, 0) |
|
119 |
end |
|
120 |
||
121 |
function onNewTurn() |
|
122 |
SetWeapon(amBazooka) |
|
123 |
end |
|
124 |
||
125 |
-- This function is called every game tick. |
|
126 |
-- Note that there are 1000 ticks within one second. |
|
127 |
-- You shouldn't try to calculate too complicated |
|
128 |
-- code here as this might slow down your game. |
|
129 |
function onGameTick20() |
|
130 |
-- If time's up, set the game to be lost. |
|
131 |
-- We actually check the time to be "1 ms" as it |
|
132 |
-- will be at "0 ms" right at the start of the game. |
|
133 |
if TurnTimeLeft < 40 and TurnTimeLeft > 0 and score < score_goal and not game_lost then |
|
134 |
game_lost = true |
|
135 |
-- ... and show a short message. |
|
136 |
ShowMission(loc("Bazooka Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0) |
|
137 |
-- How about killing our poor hog due to his poor performance? |
|
138 |
SetHealth(player, 0) |
|
139 |
-- Just to be sure set the goal time to 1 ms |
|
140 |
time_goal = 1 |
|
141 |
end |
|
142 |
||
143 |
if band(GetState(player), gstDrowning) == gstDrowning and game_lost == false and score < score_goal then |
|
144 |
game_lost = true |
|
145 |
time_goal = 1 |
|
146 |
AddCaption(loc("You lose!"), 0xFFFFFFFF, capgrpGameState) |
|
11242
1e55e4c5dda0
- Partial rus localization for User Missions and Basic Trainings in ru.lua
antonc27 <antonc27@mail.ru>
parents:
11015
diff
changeset
|
147 |
ShowMission(loc("Bazooka Training"), loc("Aiming Practice"), loc("Oh no! You failed! Just try again."), -amSkip, 0) |
11015 | 148 |
end |
149 |
||
150 |
-- If the goal is reached or we've lost ... |
|
151 |
if score == score_goal or game_lost then |
|
152 |
-- ... check to see if the time we'd like to |
|
153 |
-- wait has passed and then ... |
|
154 |
if end_timer == 0 then |
|
155 |
-- Let’s create some stats for the stats screen! |
|
156 |
-- We will expose the number of hit targets hit, launched bazooka and the accuracy |
|
157 |
||
158 |
SendStat(siPointType, loc("hits")) |
|
159 |
SendStat(siPlayerKills, tostring(score), loc("'Zooka Team")) |
|
160 |
SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets."), score, score_goal)) |
|
161 |
SendStat(siCustomAchievement, string.format(loc("You have launched %d bazookas."), shots)) |
|
162 |
||
163 |
-- We must avoid a division by zero |
|
164 |
if(shots > 0) then |
|
165 |
SendStat(siCustomAchievement, string.format(loc("Your accuracy was %.1f%%."), (score/shots)*100)) |
|
166 |
end |
|
167 |
if score == score_goal then |
|
11242
1e55e4c5dda0
- Partial rus localization for User Missions and Basic Trainings in ru.lua
antonc27 <antonc27@mail.ru>
parents:
11015
diff
changeset
|
168 |
SendStat(siGameResult, loc("You have finished the bazooka training!")) |
11015 | 169 |
SendStat(siCustomAchievement, string.format(loc("%.1f seconds were remaining."), (time_goal/1000), math.ceil(time_goal/12))) |
170 |
end |
|
171 |
if game_lost then |
|
11242
1e55e4c5dda0
- Partial rus localization for User Missions and Basic Trainings in ru.lua
antonc27 <antonc27@mail.ru>
parents:
11015
diff
changeset
|
172 |
SendStat(siGameResult, loc("You lose!")) |
11015 | 173 |
end |
174 |
||
175 |
-- Finally we end the game ... |
|
176 |
EndGame() |
|
177 |
else |
|
178 |
-- ... or just lower the timer by 20ms. |
|
179 |
-- Reset the time left to stop the timer |
|
180 |
TurnTimeLeft = time_goal |
|
181 |
end |
|
182 |
end_timer = end_timer - 20 |
|
183 |
end |
|
184 |
end |
|
185 |
||
186 |
-- This function is called when the game is initialized |
|
187 |
-- to request the available ammo and probabilities |
|
188 |
function onAmmoStoreInit() |
|
189 |
-- add an unlimited supply of bazooka ammo |
|
190 |
SetAmmo(amBazooka, 9, 0, 0, 0) |
|
191 |
end |
|
192 |
||
193 |
-- This function is called when a new gear is added. |
|
194 |
-- We don't need it for this training, so we can |
|
195 |
-- keep it empty. |
|
196 |
-- function onGearAdd(gear) |
|
197 |
-- end |
|
198 |
||
199 |
-- This function is called before a gear is destroyed. |
|
200 |
-- We use it to count the number of targets destroyed. |
|
201 |
function onGearDelete(gear) |
|
202 |
-- We're only interested in target gears. |
|
203 |
if GetGearType(gear) == gtTarget then |
|
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 |
spawnTarget() |
|
210 |
else |
|
211 |
if not game_lost then |
|
212 |
-- Otherwise show that the goal was accomplished |
|
213 |
ShowMission(loc("Bazooka Training"), loc("Aiming Practice"), loc("Congratulations! You've eliminated all targets|within the allowed time frame."), 0, 0) |
|
214 |
-- Also let the hogs shout "victory!" |
|
215 |
PlaySound(sndVictory) |
|
216 |
-- Save the time left so we may keep it. |
|
217 |
time_goal = TurnTimeLeft |
|
218 |
end |
|
219 |
end |
|
220 |
end |
|
221 |
end |
|
222 |
||
223 |
-- This function is called when a gear has been damaged. |
|
224 |
-- We only use it to determine wheather our hog took damage in order to abort the mission. |
|
225 |
function onGearDamage(gear, damage) |
|
226 |
if GetGearType(gear) == gtHedgehog then |
|
227 |
if not game_lost then |
|
228 |
game_lost = true |
|
229 |
AddCaption(loc("You lose!", 0xFFFFFFFF, capgrpGameState)) |
|
11242
1e55e4c5dda0
- Partial rus localization for User Missions and Basic Trainings in ru.lua
antonc27 <antonc27@mail.ru>
parents:
11015
diff
changeset
|
230 |
ShowMission(loc("Bazooka Training") , loc("Aiming Practice"), loc("Oh no! You failed! Just try again."), -amSkip, 0) |
11015 | 231 |
|
232 |
time_goal = 1 |
|
233 |
end |
|
234 |
end |
|
235 |
end |
|
236 |
||
237 |
||
238 |
-- This function is called after a gear is added. |
|
239 |
-- We use it to count the number of bazooka shots. |
|
240 |
function onGearAdd(gear) |
|
241 |
-- Count the number of bazooka shots for our stats |
|
242 |
if GetGearType(gear) == gtShell then |
|
243 |
shots = shots + 1 |
|
244 |
end |
|
245 |
end |