author | sheepluva |
Fri, 06 Dec 2013 15:37:15 +0100 | |
changeset 9756 | 736abcc1915f |
parent 8679 | a8bdcf7bec20 |
child 10289 | c3a77ff02a23 |
permissions | -rw-r--r-- |
3234 | 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 |
--------------------------------------------------------------- |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
11 |
-- At first we implement the localization library using loadfile. |
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
12 |
-- This allows us to localize strings without needing to think |
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
13 |
-- about translations. |
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
14 |
-- We can use the function loc(text) to localize a string. |
3234 | 15 |
|
8043 | 16 |
HedgewarsScriptLoad("/Scripts/Locale.lua") |
3234 | 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. |
|
8679 | 25 |
local end_timer = 1000 -- 1000 ms = 1 s |
3234 | 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 |
-- This is a custom function to make it easier to |
|
35 |
-- spawn more targets with just one line of code |
|
36 |
-- You may define as many custom functions as you |
|
37 |
-- like. |
|
38 |
function spawnTarget() |
|
39 |
-- add a new target gear |
|
40 |
gear = AddGear(0, 0, gtTarget, 0, 0, 0, 0) |
|
41 |
||
42 |
-- move it to a random position within 0 and |
|
43 |
-- LAND_WIDTH - the width of the map |
|
44 |
FindPlace(gear, true, 0, LAND_WIDTH) |
|
45 |
||
46 |
-- move the target to a higher vertical position |
|
47 |
-- to ensure it's not somewhere down below |
|
48 |
x, y = GetGearPosition(gear) |
|
7877
b3fb94986255
Fix training script positions altered by variable land dimension change. Issue #453
nemo
parents:
7165
diff
changeset
|
49 |
SetGearPosition(gear, x, 0) |
3234 | 50 |
end |
51 |
||
52 |
-- This function is called before the game loads its |
|
53 |
-- resources. |
|
54 |
-- It's one of the predefined function names that will |
|
55 |
-- be called by the game. They give you entry points |
|
56 |
-- where you're able to call your own code using either |
|
57 |
-- provided instructions or custom functions. |
|
58 |
function onGameInit() |
|
59 |
-- At first we have to overwrite/set some global variables |
|
60 |
-- that define the map, the game has to load, as well as |
|
61 |
-- other things such as the game rules to use, etc. |
|
62 |
-- Things we don't modify here will use their default values. |
|
63 |
||
64 |
-- The base number for the random number generator |
|
4665
fa7ad5f3725f
Make basic training solvable again. Freeze RNG at current version for less of this kind of issue in future, and a bit more savable of seeds. Disable offsets in preparation for release.
nemo
parents:
4662
diff
changeset
|
65 |
Seed = 1 |
3234 | 66 |
-- Game settings and rules |
8366
67c7ba2b82a3
lua API functions to enable or disable game flags
martin_bede
parents:
8043
diff
changeset
|
67 |
EnableGameFlags(gfMultiWeapon, gfOneClanMode, gfSolidLand) |
67c7ba2b82a3
lua API functions to enable or disable game flags
martin_bede
parents:
8043
diff
changeset
|
68 |
-- Uncommenting this wouldn't do anything |
67c7ba2b82a3
lua API functions to enable or disable game flags
martin_bede
parents:
8043
diff
changeset
|
69 |
--EnableGameFlags(gfMultiWeapon, gfOneClanMode, gfSolidLand) |
67c7ba2b82a3
lua API functions to enable or disable game flags
martin_bede
parents:
8043
diff
changeset
|
70 |
-- Neither this |
67c7ba2b82a3
lua API functions to enable or disable game flags
martin_bede
parents:
8043
diff
changeset
|
71 |
--DisableGameFlags(gfArtillery) |
67c7ba2b82a3
lua API functions to enable or disable game flags
martin_bede
parents:
8043
diff
changeset
|
72 |
-- Uncommenting this would make the terrain damageable |
67c7ba2b82a3
lua API functions to enable or disable game flags
martin_bede
parents:
8043
diff
changeset
|
73 |
--DisableGameFlags(gfSolidLand) |
67c7ba2b82a3
lua API functions to enable or disable game flags
martin_bede
parents:
8043
diff
changeset
|
74 |
-- Uncommenting this would remove all flags set previously |
67c7ba2b82a3
lua API functions to enable or disable game flags
martin_bede
parents:
8043
diff
changeset
|
75 |
--ClearGameFlags() |
3234 | 76 |
-- The time the player has to move each round (in ms) |
77 |
TurnTime = 60000 |
|
78 |
-- The frequency of crate drops |
|
79 |
CaseFreq = 0 |
|
80 |
-- The number of mines being placed |
|
4162 | 81 |
MinesNum = 0 |
3234 | 82 |
-- The number of explosives being placed |
83 |
Explosives = 0 |
|
84 |
-- The delay between each round |
|
85 |
Delay = 0 |
|
86 |
-- The map to be played |
|
87 |
Map = "Bamboo" |
|
88 |
-- The theme to be used |
|
89 |
Theme = "Bamboo" |
|
90 |
||
91 |
-- Create the player team |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
92 |
AddTeam(loc("'Zooka Team"), 14483456, "Simple", "Island", "Default") |
3234 | 93 |
-- And add a hog to it |
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
94 |
player = AddHog(loc("Hunter"), 0, 1, "NoHat") |
7877
b3fb94986255
Fix training script positions altered by variable land dimension change. Issue #453
nemo
parents:
7165
diff
changeset
|
95 |
SetGearPosition(player, 936, 136) |
3234 | 96 |
end |
97 |
||
98 |
-- This function is called when the round starts |
|
99 |
-- it spawns the first target that has to be destroyed. |
|
100 |
-- In addition it shows the scenario goal(s). |
|
101 |
function onGameStart() |
|
102 |
-- Spawn the first target. |
|
103 |
spawnTarget() |
|
104 |
||
105 |
-- Show some nice mission goals. |
|
106 |
-- Parameters are: caption, sub caption, description, |
|
107 |
-- extra text, icon and time to show. |
|
108 |
-- A negative icon parameter (-n) represents the n-th weapon icon |
|
109 |
-- A positive icon paramter (n) represents the (n+1)-th mission icon |
|
110 |
-- A timeframe of 0 is replaced with the default time to show. |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
111 |
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) |
3234 | 112 |
end |
113 |
||
4662
63aafc9c2a81
Add a bunch of lua from mikade, update translation files
mikade+nemo
parents:
4506
diff
changeset
|
114 |
function onNewTurn() |
63aafc9c2a81
Add a bunch of lua from mikade, update translation files
mikade+nemo
parents:
4506
diff
changeset
|
115 |
ParseCommand("setweap " .. string.char(amBazooka)) |
63aafc9c2a81
Add a bunch of lua from mikade, update translation files
mikade+nemo
parents:
4506
diff
changeset
|
116 |
end |
63aafc9c2a81
Add a bunch of lua from mikade, update translation files
mikade+nemo
parents:
4506
diff
changeset
|
117 |
|
3234 | 118 |
-- This function is called every game tick. |
119 |
-- Note that there are 1000 ticks within one second. |
|
120 |
-- You shouldn't try to calculate too complicated |
|
121 |
-- code here as this might slow down your game. |
|
7165
aad1aea05f1e
add onGameTick20 to basic training, extend laser sight out way more (it was visible at top when completely zoomed out), move call of new turn to after AfterSwitchHedgehog to avoid lua issues in onNewTurn - if this causes problems, lua can do delayed actions in onGameTick
nemo
parents:
4665
diff
changeset
|
122 |
function onGameTick20() |
3234 | 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. |
|
7165
aad1aea05f1e
add onGameTick20 to basic training, extend laser sight out way more (it was visible at top when completely zoomed out), move call of new turn to after AfterSwitchHedgehog to avoid lua issues in onNewTurn - if this causes problems, lua can do delayed actions in onGameTick
nemo
parents:
4665
diff
changeset
|
126 |
if TurnTimeLeft < 40 and TurnTimeLeft > 0 and score < score_goal then |
3234 | 127 |
game_lost = true |
128 |
-- ... and show a short message. |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
129 |
ShowMission(loc("Bazooka Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0) |
3234 | 130 |
-- How about killing our poor hog due to his poor performance? |
4493
45db8e97d282
removing all ; from lua script as an act of temporary rage and perfectionism
Henek
parents:
4350
diff
changeset
|
131 |
SetHealth(player, 0) |
3234 | 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 |
|
8679 | 143 |
-- ... or just lower the timer by 20ms. |
3234 | 144 |
-- Reset the time left to stop the timer |
145 |
TurnTimeLeft = time_goal |
|
146 |
end |
|
8679 | 147 |
end_timer = end_timer - 20 |
3234 | 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 bazooka ammo |
|
3346 | 155 |
SetAmmo(amBazooka, 9, 0, 0, 0) |
3234 | 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. |
|
7165
aad1aea05f1e
add onGameTick20 to basic training, extend laser sight out way more (it was visible at top when completely zoomed out), move call of new turn to after AfterSwitchHedgehog to avoid lua issues in onNewTurn - if this causes problems, lua can do delayed actions in onGameTick
nemo
parents:
4665
diff
changeset
|
161 |
-- function onGearAdd(gear) |
aad1aea05f1e
add onGameTick20 to basic training, extend laser sight out way more (it was visible at top when completely zoomed out), move call of new turn to after AfterSwitchHedgehog to avoid lua issues in onNewTurn - if this causes problems, lua can do delayed actions in onGameTick
nemo
parents:
4665
diff
changeset
|
162 |
-- end |
3234 | 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 |
-- We're only interested in target gears. |
|
168 |
if GetGearType(gear) == gtTarget then |
|
169 |
-- Add one point to our score/counter |
|
170 |
score = score + 1 |
|
171 |
-- If we haven't reached the goal ... |
|
172 |
if score < score_goal then |
|
173 |
-- ... spawn another target. |
|
174 |
spawnTarget() |
|
175 |
else |
|
176 |
if not game_lost then |
|
177 |
-- Otherwise show that the goal was accomplished |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
178 |
ShowMission(loc("Bazooka Training"), loc("Aiming Practice"), loc("Congratulations! You've eliminated all targets|within the allowed time frame."), 0, 0) |
3234 | 179 |
-- Also let the hogs shout "victory!" |
180 |
PlaySound(sndVictory) |
|
181 |
-- Save the time left so we may keep it. |
|
182 |
time_goal = TurnTimeLeft |
|
183 |
end |
|
184 |
end |
|
185 |
end |
|
186 |
end |