share/hedgewars/Data/Missions/Training/Basic_Training_-_Bazooka.lua
author martin_bede
Tue, 08 Jan 2013 00:40:20 +0400
changeset 8366 67c7ba2b82a3
parent 8043 da083f8d95e6
child 8679 a8bdcf7bec20
permissions -rw-r--r--
lua API functions to enable or disable game flags
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3234
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
     1
-- Hedgewars Bazooka Training
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
     2
-- Scripting Example
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
     3
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
     4
-- Lines such as this one are comments - they are ignored
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
     5
-- by the game, no matter what kind of text is in there.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
     6
-- It's also possible to place a comment after some real
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
     7
-- instruction as you see below. In short, everything
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
     8
-- following "--" is ignored.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
     9
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    15
8043
da083f8d95e6 We need custom script loading function in lua now
unc0rr
parents: 7877
diff changeset
    16
HedgewarsScriptLoad("/Scripts/Locale.lua")
3234
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    17
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    18
-- This variable will hold the number of destroyed targets.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    19
local score = 0
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    20
-- This variable represents the number of targets to destroy.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    21
local score_goal = 5
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    22
-- This variable controls how many milliseconds/ticks we'd
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    23
-- like to wait before we end the round once all targets
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    24
-- have been destroyed.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    25
local end_timer = 5000 -- 5000 ms = 5 s
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    26
-- This variable is set to true if the game is lost (i.e.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    27
-- time runs out).
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    28
local game_lost = false
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    29
-- This variable will point to the hog's gear
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    30
local player = nil
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    31
-- This variable will grab the time left at the end of the round
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    32
local time_goal = 0
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    33
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    34
-- This is a custom function to make it easier to
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    35
-- spawn more targets with just one line of code
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    36
-- You may define as many custom functions as you
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    37
-- like.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    38
function spawnTarget()
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    39
	-- add a new target gear
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    40
	gear = AddGear(0, 0, gtTarget, 0, 0, 0, 0)
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    41
	
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    42
	-- move it to a random position within 0 and
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    43
	-- LAND_WIDTH - the width of the map
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    44
	FindPlace(gear, true, 0, LAND_WIDTH)
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    45
	
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    46
	-- move the target to a higher vertical position
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    47
	-- to ensure it's not somewhere down below
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    50
end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    51
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    52
-- This function is called before the game loads its
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    53
-- resources.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    54
-- It's one of the predefined function names that will
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    55
-- be called by the game. They give you entry points
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    56
-- where you're able to call your own code using either
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    57
-- provided instructions or custom functions.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    58
function onGameInit()
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    59
	-- At first we have to overwrite/set some global variables
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    60
	-- that define the map, the game has to load, as well as
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    61
	-- other things such as the game rules to use, etc.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    62
	-- Things we don't modify here will use their default values.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    63
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    76
	-- The time the player has to move each round (in ms)
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    77
	TurnTime = 60000
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    78
	-- The frequency of crate drops
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    79
	CaseFreq = 0
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    80
	-- The number of mines being placed
4162
923db448ad16 update and fix some lua stuff
Henek
parents: 3476
diff changeset
    81
	MinesNum = 0
3234
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    82
	-- The number of explosives being placed
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    83
	Explosives = 0
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    84
	-- The delay between each round
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    85
	Delay = 0
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    86
	-- The map to be played
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    87
	Map = "Bamboo"
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    88
	-- The theme to be used
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    89
	Theme = "Bamboo"
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    90
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    96
end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    97
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    98
-- This function is called when the round starts
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
    99
-- it spawns the first target that has to be destroyed.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   100
-- In addition it shows the scenario goal(s).
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   101
function onGameStart()
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   102
	-- Spawn the first target.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   103
	spawnTarget()
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   104
	
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   105
	-- Show some nice mission goals.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   106
	-- Parameters are: caption, sub caption, description,
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   107
	-- extra text, icon and time to show.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   108
	-- A negative icon parameter (-n) represents the n-th weapon icon
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   109
	-- A positive icon paramter (n) represents the (n+1)-th mission icon
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   112
end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   118
-- This function is called every game tick.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   119
-- Note that there are 1000 ticks within one second.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   120
-- You shouldn't try to calculate too complicated
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   123
	-- If time's up, set the game to be lost.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   124
	-- We actually check the time to be "1 ms" as it
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   127
		game_lost = true
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   132
		-- Just to be sure set the goal time to 1 ms
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   133
		time_goal = 1
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   134
	end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   135
	-- If the goal is reached or we've lost ...
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   136
	if score == score_goal or game_lost then
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   137
		-- ... check to see if the time we'd like to
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   138
		-- wait has passed and then ...
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   139
		if end_timer == 0 then
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   140
			-- ... end the game ...
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   141
			EndGame()
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   142
		else
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   143
			-- ... or just lower the timer by 1.
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
   144
			end_timer = end_timer - 20
3234
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   145
			-- Reset the time left to stop the timer
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   146
			TurnTimeLeft = time_goal
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   147
		end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   148
	end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   149
end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   150
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   151
-- This function is called when the game is initialized
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   152
-- to request the available ammo and probabilities
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   153
function onAmmoStoreInit()
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   154
	-- add an unlimited supply of bazooka ammo
3346
967fd96f7373 Engine/Frontend:
smxx
parents: 3235
diff changeset
   155
	SetAmmo(amBazooka, 9, 0, 0, 0)
3234
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   156
end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   157
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   158
-- This function is called when a new gear is added.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   159
-- We don't need it for this training, so we can
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   163
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   164
-- This function is called before a gear is destroyed.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   165
-- We use it to count the number of targets destroyed.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   166
function onGearDelete(gear)
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   167
	-- We're only interested in target gears.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   168
	if GetGearType(gear) == gtTarget then
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   169
		-- Add one point to our score/counter
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   170
		score = score + 1
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   171
		-- If we haven't reached the goal ...
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   172
		if score < score_goal then
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   173
			-- ... spawn another target.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   174
			spawnTarget()
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   175
		else
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   176
			if not game_lost then
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   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
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   179
			-- Also let the hogs shout "victory!"
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   180
			PlaySound(sndVictory)
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   181
			-- Save the time left so we may keep it.
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   182
			time_goal = TurnTimeLeft
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   183
			end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   184
		end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   185
	end
668c7d0a00a6 Trainings:
smxx
parents:
diff changeset
   186
end