share/hedgewars/Data/Scripts/TargetPractice.lua
author Wuzzy <Wuzzy2@mail.ru>
Sat, 20 Apr 2019 16:46:07 +0200
changeset 14828 358d81e29a88
parent 14659 8bd005067f4a
child 14933 4c5fb1ee75b7
permissions -rw-r--r--
Space Invasion: Fix minor visual glitches of shield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
     1
--[=[
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
     2
Target Practice Mission Framework for Hedgewars
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
     3
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
     4
This is a simple library intended to make setting up simple training missions a trivial
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
     5
task requiring just. The library has been created to reduce redundancy in Lua scripts.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
     6
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
     7
The training framework generates complete and fully usable training missions by just
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
     8
one function call.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
     9
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    10
The missions generated by this script are all the same:
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    11
- The player will get a team with a single hedgehog.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    12
- The team gets a single predefined weapon infinitely times.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    13
- A fixed sequence of targets will spawn at predefined positions.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    14
- When a target has been destroyed, the next target of the target sequence appears
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    15
- The mission ends successfully when all targets have been destroyed
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    16
- The mission ends unsuccessfully when the time runs out or the hedgehog dies
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    17
- When the mission ends, a score is awarded, based on the performance (hit targets,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    18
  accuracy and remaining time) of the hedgehog. When not all targets are hit, there
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    19
  will be no accuracy and time bonuses.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    20
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    21
To use this library, you first have to load it and to call TrainingMission once with
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    22
the appropriate parameters. Really, that’s all!
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    23
See the comment of TrainingMission for a specification of all parameters.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    24
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    25
Below is a template for your convenience, you just have to fill in the fields and delete
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    26
optional arguments you don’t want.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    27
----- snip -----
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    28
HedgewarsScriptLoad("/Scripts/Training.lua")
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    29
params = {
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    30
	missionTitle = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    31
	map = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    32
	theme = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    33
	time = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    34
	ammoType = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    35
	gearType = ,
13103
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
    36
	secondaryGearType = ,
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    37
	targets = {
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    38
		{ x = , y = },
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    39
		{ x = , y = },
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    40
		-- etc.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    41
	},
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    42
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    43
	wind = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    44
	solidLand = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    45
	artillery = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    46
	clanColor = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    47
	goalText = ,
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    48
	shootText =
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    49
}
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    50
TargetPracticeMission(params)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    51
----- snip -----
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    52
]=]
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    53
14480
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14469
diff changeset
    54
HedgewarsScriptLoad("/Scripts/Utils.lua")
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    55
HedgewarsScriptLoad("/Scripts/Locale.lua")
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    56
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    57
local player = nil
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    58
local scored = 0
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    59
local shots = 0
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    60
local end_timer = 1000
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    61
local game_lost = false
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    62
local time_goal = 0
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    63
local total_targets
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    64
local targets
14598
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
    65
local target_radar = false
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
    66
local next_target_circle = nil
13103
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
    67
local gearsInGameCount = 0
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
    68
local gearsInGame = {}
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    69
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    70
--[[
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    71
TrainingMission(params)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    72
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    73
This function sets up the *entire* training mission and needs one argument: params.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    74
The argument “params” is a table containing fields which describe the training mission.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    75
	mandatory fields:
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    76
	- missionTitle:	the name of the mission
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    77
	- map:		the name map to be used
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    78
	- theme:	the name of the theme (does not need to be a standalone theme)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    79
	- time:		the time limit in milliseconds
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    80
	- ammoType:	the ammo type of the weapon to be used
13103
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
    81
	- gearType:	the gear type of the gear which is fired (used to count shots and re-center camera)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    82
	- targets:	The coordinates of where the targets will be spawned.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    83
			It is a table containing tables containing coordinates of format
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    84
			{ x=value, y=value }. The targets will be spawned in the same
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    85
			order as specified the coordinate tables appear. Example:
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    86
				targets = {
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    87
					{ x = 324, y = 43 },
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    88
					{ x = 123, y = 56 },
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    89
					{ x = 6, y = 0 },
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    90
				}
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    91
			There must be at least 1 target.
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    92
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    93
	optional fields:
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    94
	- wind:		the initial wind (-100 to 100) (default: 0 (no wind))
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    95
	- solidLand:	weather the terrain is indestructible (default: false)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
    96
	- artillery:	if true, the hog can’t move (default: false)
13103
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
    97
	- secGearType:	cluster of projectile gear (if present) (used to re-center camera)
13588
141cdfe0f3ca Switch almost all Lua calls of AddTeam to using default clan colors instead of hardcoded color
Wuzzy <Wuzzy2@mail.ru>
parents: 13104
diff changeset
    98
	- clanColor:	color of the (only) clan (default: -1, default first clan color)
14653
be8af70adf2c Tweak hog spawn height and facing direction in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14599
diff changeset
    99
	- faceLeft:	if true, hog starts facing left, otherwise right (default: false)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   100
	- goalText:	A short string explaining the goal of the mission
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   101
			(default: "Destroy all targets within the time!")
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   102
	- shootText:	A string which says how many times the player shot, “%d” is replaced
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   103
			by the number of shots. (default: "You have shot %d times.")
14598
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   104
	- useRadar	Whether to use target radar (small circles that mark the position
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   105
			of the next target). (default: true). Note: Still needs to be unlocked.
14599
825fe522f4f1 Target practice: Change target radar color to red
Wuzzy <Wuzzy2@mail.ru>
parents: 14598
diff changeset
   106
	- radarTint:	RGBA color of the target radar  (default: 0xFF3030FF). Use this field
14598
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   107
			if the target radar would be hard to see against the background.
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   108
]]
13070
a297e06d1607 Display score next to team bar in singleplayer challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 13039
diff changeset
   109
a297e06d1607 Display score next to team bar in singleplayer challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 13039
diff changeset
   110
a297e06d1607 Display score next to team bar in singleplayer challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 13039
diff changeset
   111
local getTargetsScore = function()
a297e06d1607 Display score next to team bar in singleplayer challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 13039
diff changeset
   112
	return scored * math.ceil(6000/#targets)
a297e06d1607 Display score next to team bar in singleplayer challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 13039
diff changeset
   113
end
a297e06d1607 Display score next to team bar in singleplayer challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 13039
diff changeset
   114
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   115
function TargetPracticeMission(params)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   116
	if params.goalText == nil then params.goalText = loc("Eliminate all targets before your time runs out.|You have unlimited ammo for this mission.") end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   117
	if params.shootText == nil then params.shootText = loc("You have shot %d times.") end
13588
141cdfe0f3ca Switch almost all Lua calls of AddTeam to using default clan colors instead of hardcoded color
Wuzzy <Wuzzy2@mail.ru>
parents: 13104
diff changeset
   118
	if params.clanColor == nil then params.clanColor = -1 end
14653
be8af70adf2c Tweak hog spawn height and facing direction in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14599
diff changeset
   119
	if params.faceLeft == nil then params.faceLeft = false end
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   120
	if params.wind == nil then params.wind = 0 end
14599
825fe522f4f1 Target practice: Change target radar color to red
Wuzzy <Wuzzy2@mail.ru>
parents: 14598
diff changeset
   121
	if params.radarTint == nil then params.radarTint = 0xFF3030FF end
14598
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   122
	if params.useRadar == nil then params.useRadar = true end
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   123
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   124
	local solid, artillery
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   125
	if params.solidLand == true then solid = gfSolidLand else solid = 0 end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   126
	if params.artillery == true then artillery = gfArtillery else artillery = 0 end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   127
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   128
	targets = params.targets
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   129
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   130
	total_targets = #targets
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   131
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   132
	_G.onAmmoStoreInit = function()
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   133
		SetAmmo(params.ammoType, 9, 0, 0, 0)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   134
	end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   135
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   136
	_G.onGameInit = function()
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   137
		Seed = 1
13104
071dcdf33f86 Fix artillery being broken in some missions
Wuzzy <Wuzzy2@mail.ru>
parents: 13103
diff changeset
   138
		ClearGameFlags()
13628
f66d8779e4dd Fix bee target practice removing target selection after first shot
Wuzzy <Wuzzy2@mail.ru>
parents: 13625
diff changeset
   139
		local attackMode
f66d8779e4dd Fix bee target practice removing target selection after first shot
Wuzzy <Wuzzy2@mail.ru>
parents: 13625
diff changeset
   140
		if (params.ammoType == amBee) then
f66d8779e4dd Fix bee target practice removing target selection after first shot
Wuzzy <Wuzzy2@mail.ru>
parents: 13625
diff changeset
   141
			attackMode = gfInfAttack
f66d8779e4dd Fix bee target practice removing target selection after first shot
Wuzzy <Wuzzy2@mail.ru>
parents: 13625
diff changeset
   142
		else
f66d8779e4dd Fix bee target practice removing target selection after first shot
Wuzzy <Wuzzy2@mail.ru>
parents: 13625
diff changeset
   143
			attackMode = gfMultiWeapon
f66d8779e4dd Fix bee target practice removing target selection after first shot
Wuzzy <Wuzzy2@mail.ru>
parents: 13625
diff changeset
   144
		end
f66d8779e4dd Fix bee target practice removing target selection after first shot
Wuzzy <Wuzzy2@mail.ru>
parents: 13625
diff changeset
   145
		EnableGameFlags(gfDisableWind, attackMode, gfOneClanMode, solid, artillery)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   146
		TurnTime = params.time
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   147
		Map = params.map
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   148
		Theme = params.theme
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   149
		Goals = params.goalText
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   150
		CaseFreq = 0
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   151
		MinesNum = 0
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   152
		Explosives = 0
12229
d62d6f8ebef1 Disable Sudden Death consistently in all missions which don't require it
Wuzzy <almikes@aol.com>
parents: 12054
diff changeset
   153
		-- Disable Sudden Death
d62d6f8ebef1 Disable Sudden Death consistently in all missions which don't require it
Wuzzy <almikes@aol.com>
parents: 12054
diff changeset
   154
		WaterRise = 0
d62d6f8ebef1 Disable Sudden Death consistently in all missions which don't require it
Wuzzy <almikes@aol.com>
parents: 12054
diff changeset
   155
		HealthDecrease = 0
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   156
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   157
		SetWind(params.wind)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   158
14485
208359558642 Use player-chosen team identity for training and a few challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14480
diff changeset
   159
		AddMissionTeam(params.clanColor)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   160
14485
208359558642 Use player-chosen team identity for training and a few challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14480
diff changeset
   161
		player = AddMissionHog(1)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   162
		SetGearPosition(player, params.hog_x, params.hog_y)
14653
be8af70adf2c Tweak hog spawn height and facing direction in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14599
diff changeset
   163
		HogTurnLeft(player, params.faceLeft)
14598
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   164
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   165
		local won = GetMissionVar("Won")
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   166
		-- Unlock the target radar when the player has completed
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   167
		-- the target practice before (any score).
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   168
		-- Target radar might be disabled by config, however.
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   169
		if won == "true" and params.useRadar == true then
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   170
			target_radar = true
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   171
		end
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   172
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   173
	end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   174
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   175
	_G.onGameStart = function()
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   176
		SendHealthStatsOff()
14480
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14469
diff changeset
   177
		local recordInfo = getReadableChallengeRecord("Highscore")
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14469
diff changeset
   178
		ShowMission(params.missionTitle, loc("Aiming practice"), params.goalText .. "|" .. recordInfo, -params.ammoType, 5000)
14485
208359558642 Use player-chosen team identity for training and a few challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14480
diff changeset
   179
		SetTeamLabel(GetHogTeamName(player), "0")
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   180
		spawnTarget()
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   181
	end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   182
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   183
	_G.onNewTurn = function()
11772
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   184
		SetWeapon(params.ammoType)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   185
	end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   186
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   187
	_G.spawnTarget = function()
14598
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   188
		-- Spawn next target
14236
74bf2d906097 Turn accidental globals to locals in Lua libraries
Wuzzy <Wuzzy2@mail.ru>
parents: 13745
diff changeset
   189
		local gear = AddGear(0, 0, gtTarget, 0, 0, 0, 0)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   190
14236
74bf2d906097 Turn accidental globals to locals in Lua libraries
Wuzzy <Wuzzy2@mail.ru>
parents: 13745
diff changeset
   191
		local x = targets[scored+1].x
74bf2d906097 Turn accidental globals to locals in Lua libraries
Wuzzy <Wuzzy2@mail.ru>
parents: 13745
diff changeset
   192
		local y = targets[scored+1].y
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   193
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   194
		SetGearPosition(gear, x, y)
11772
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   195
14598
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   196
		-- Target radar: Highlight position of the upcoming target.
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   197
		-- This must be unlocked by the player first.
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   198
		if target_radar then
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   199
			if (not next_target_circle) and targets[scored+2] then
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   200
				next_target_circle = AddVisualGear(0,0,vgtCircle,90,true)
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   201
			end
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   202
			if targets[scored+2] then
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   203
				SetVisualGearValues(next_target_circle, targets[scored+2].x, targets[scored+2].y, 205, 255, 1, 20, nil, nil, 3, params.radarTint)
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   204
			elseif next_target_circle then
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   205
				DeleteVisualGear(next_target_circle)
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   206
				next_target_circle = nil
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   207
			end
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   208
		end
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   209
11772
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   210
		return gear
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   211
	end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   212
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   213
	_G.onGameTick20 = function()
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   214
		if TurnTimeLeft < 40 and TurnTimeLeft > 0 and scored < total_targets and game_lost == false then
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   215
			game_lost = true
13648
690cc84e9fd6 Use capcolDefault in Lua scripts
Wuzzy <Wuzzy2@mail.ru>
parents: 13628
diff changeset
   216
			AddCaption(loc("Time’s up!"), capcolDefault, capgrpGameState)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   217
			SetHealth(player, 0)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   218
			time_goal = 1
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   219
		end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   220
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   221
		if band(GetState(player), gstDrowning) == gstDrowning and game_lost == false and scored < total_targets then
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   222
			game_lost = true
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   223
			time_goal = 1
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   224
		end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   225
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   226
		if scored == total_targets  or game_lost then
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   227
			if end_timer == 0 then
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   228
				generateStats()
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   229
				EndGame()
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   230
			end
14531
797e3f441c14 Make turn timer gray when finishing target practice
Wuzzy <Wuzzy2@mail.ru>
parents: 14485
diff changeset
   231
			end_timer = end_timer - 20
13103
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   232
		end
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   233
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   234
		for gear, _ in pairs(gearsInGame) do
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   235
			if band(GetState(gear), gstDrowning) ~= 0 then
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   236
				-- Re-center camera on hog if projectile gears drown
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   237
				gearsInGame[gear] = nil
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   238
				gearsInGameCount = gearsInGameCount - 1
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   239
				if gearsInGameCount == 0 and GetHealth(CurrentHedgehog) then
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   240
					FollowGear(CurrentHedgehog)
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   241
				end
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   242
			end
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   243
		end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   244
	end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   245
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   246
	_G.onGearAdd = function(gear)
14370
a7769dbd7088 Fix cluster bomb target practice counting clusters as "shots"
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
   247
		if GetGearType(gear) == params.gearType then
a7769dbd7088 Fix cluster bomb target practice counting clusters as "shots"
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
   248
			shots = shots + 1
a7769dbd7088 Fix cluster bomb target practice counting clusters as "shots"
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
   249
		end
13103
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   250
		if GetGearType(gear) == params.gearType or (params.secGearType and GetGearType(gear) == params.secGearType) then
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   251
			gearsInGameCount = gearsInGameCount + 1
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   252
			gearsInGame[gear] = true
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   253
		end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   254
	end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   255
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   256
	_G.onGearDamage = function(gear, damage)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   257
		if GetGearType(gear) == gtTarget then
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   258
			scored = scored + 1
14485
208359558642 Use player-chosen team identity for training and a few challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14480
diff changeset
   259
			SetTeamLabel(GetHogTeamName(player), tostring(getTargetsScore()))
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   260
			if scored < total_targets then
13648
690cc84e9fd6 Use capcolDefault in Lua scripts
Wuzzy <Wuzzy2@mail.ru>
parents: 13628
diff changeset
   261
				AddCaption(string.format(loc("Targets left: %d"), (total_targets-scored)), capcolDefault, capgrpMessage)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   262
				spawnTarget()
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   263
			else
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   264
				if not game_lost then
14469
ead8928a59f8 Report mission victory for most missions
Wuzzy <Wuzzy2@mail.ru>
parents: 14370
diff changeset
   265
					SaveMissionVar("Won", "true")
13648
690cc84e9fd6 Use capcolDefault in Lua scripts
Wuzzy <Wuzzy2@mail.ru>
parents: 13628
diff changeset
   266
					AddCaption(loc("You have destroyed all targets!"), capcolDefault, capgrpGameState)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   267
					ShowMission(params.missionTitle, loc("Aiming practice"), loc("Congratulations! You have destroyed all targets within the time."), 0, 0)
14659
8bd005067f4a Play "Flawless" voice when winning target practice mission with perfect accuracy
Wuzzy <Wuzzy2@mail.ru>
parents: 14653
diff changeset
   268
					if shots <= scored then
8bd005067f4a Play "Flawless" voice when winning target practice mission with perfect accuracy
Wuzzy <Wuzzy2@mail.ru>
parents: 14653
diff changeset
   269
						-- No misses!
8bd005067f4a Play "Flawless" voice when winning target practice mission with perfect accuracy
Wuzzy <Wuzzy2@mail.ru>
parents: 14653
diff changeset
   270
						PlaySound(sndFlawless, player)
8bd005067f4a Play "Flawless" voice when winning target practice mission with perfect accuracy
Wuzzy <Wuzzy2@mail.ru>
parents: 14653
diff changeset
   271
					else
8bd005067f4a Play "Flawless" voice when winning target practice mission with perfect accuracy
Wuzzy <Wuzzy2@mail.ru>
parents: 14653
diff changeset
   272
						PlaySound(sndVictory, player)
8bd005067f4a Play "Flawless" voice when winning target practice mission with perfect accuracy
Wuzzy <Wuzzy2@mail.ru>
parents: 14653
diff changeset
   273
					end
13039
fe9c12209f15 Make hog invulnerable after winning SpeedShoppa or TargetPractice mission
Wuzzy <Wuzzy2@mail.ru>
parents: 12785
diff changeset
   274
					SetEffect(player, heInvulnerable, 1)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   275
					SetState(player, bor(GetState(player), gstWinner))
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   276
					time_goal = TurnTimeLeft
12785
659ab77b52c0 Disable hog control after winning target practice challenge
Wuzzy <Wuzzy2@mail.ru>
parents: 12430
diff changeset
   277
					-- Disable control
659ab77b52c0 Disable hog control after winning target practice challenge
Wuzzy <Wuzzy2@mail.ru>
parents: 12430
diff changeset
   278
					SetInputMask(0)
659ab77b52c0 Disable hog control after winning target practice challenge
Wuzzy <Wuzzy2@mail.ru>
parents: 12430
diff changeset
   279
					AddAmmo(player, params.ammoType, 0)
14531
797e3f441c14 Make turn timer gray when finishing target practice
Wuzzy <Wuzzy2@mail.ru>
parents: 14485
diff changeset
   280
					SetTurnTimePaused(true)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   281
				end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   282
			end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   283
		end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   284
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   285
		if GetGearType(gear) == gtHedgehog then
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   286
			if not game_lost then
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   287
				game_lost = true
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   288
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   289
				SetHealth(player, 0)
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   290
				time_goal = 1
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   291
			end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   292
		end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   293
	end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   294
11772
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   295
	_G.onGearDelete = function(gear)
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   296
		if GetGearType(gear) == gtTarget and band(GetState(gear), gstDrowning) ~= 0 then
13648
690cc84e9fd6 Use capcolDefault in Lua scripts
Wuzzy <Wuzzy2@mail.ru>
parents: 13628
diff changeset
   297
			AddCaption(loc("You lost your target, try again!"), capcolDefault, capgrpGameState)
11772
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   298
			local newTarget = spawnTarget()
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   299
			local x, y = GetGearPosition(newTarget)
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   300
			local success = PlaceSprite(x, y + 24, sprAmGirder, 0, 0xFFFFFFFF, false, false, false)
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   301
			if not success then
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   302
				WriteLnToConsole("ERROR: Failed to spawn girder under respawned target!")
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   303
			end
13103
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   304
		elseif gearsInGame[gear] then
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   305
			gearsInGame[gear] = nil
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   306
			gearsInGameCount = gearsInGameCount - 1
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   307
			if gearsInGameCount == 0 and GetHealth(CurrentHedgehog) then
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   308
				-- Re-center camera to hog after all projectile gears were destroyed
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   309
				FollowGear(CurrentHedgehog)
d3e9f3423ef3 Target Practice: Re-center camera to hog after projectiles get destroyed or drown
Wuzzy <Wuzzy2@mail.ru>
parents: 13070
diff changeset
   310
			end
11772
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   311
		end
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   312
	end
c21278c4218d Target Practice: Make targets respawn after drowning, and minor code cleanup
Wuzzy <almikes@aol.com>
parents: 10656
diff changeset
   313
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   314
	_G.generateStats = function()
14596
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   315
		local accuracy, accuracy_int
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   316
		if(shots > 0) then
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   317
			accuracy = (scored/shots)*100
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   318
			accuracy_int = div(scored*100, shots)
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   319
		end
13070
a297e06d1607 Display score next to team bar in singleplayer challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 13039
diff changeset
   320
		local end_score_targets = getTargetsScore()
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   321
		local end_score_overall
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   322
		if not game_lost then
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   323
			local end_score_time = math.ceil(time_goal/(params.time/6000))
14596
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   324
			local end_score_accuracy = 0
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   325
			if(shots > 0) then
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   326
				end_score_accuracy = math.ceil(accuracy * 60)
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   327
			end
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   328
			end_score_overall = end_score_time + end_score_targets + end_score_accuracy
14485
208359558642 Use player-chosen team identity for training and a few challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14480
diff changeset
   329
			SetTeamLabel(GetHogTeamName(player), tostring(end_score_overall))
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   330
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   331
			SendStat(siGameResult, loc("You have finished the target practice!"))
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   332
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   333
			SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets (+%d points)."), scored, total_targets, end_score_targets))
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   334
			SendStat(siCustomAchievement, string.format(params.shootText, shots))
14596
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   335
			if(shots > 0) then
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   336
				SendStat(siCustomAchievement, string.format(loc("Your accuracy was %.1f%% (+%d points)."), accuracy, end_score_accuracy))
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   337
			end
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   338
			SendStat(siCustomAchievement, string.format(loc("You had %.1fs remaining on the clock (+%d points)."), (time_goal/1000), end_score_time))
14598
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   339
			if (not target_radar) and (#targets > 1) and (params.useRadar == true) then
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   340
				SendStat(siCustomAchievement, loc("You have unlocked the target radar!"))
c3bbe56156e5 Highlight next target for most target practices
Wuzzy <Wuzzy2@mail.ru>
parents: 14596
diff changeset
   341
			end
14596
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   342
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   343
			if(shots > 0) then
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   344
				updateChallengeRecord("AccuracyRecord", accuracy_int)
b4089fa16b34 Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14595
diff changeset
   345
			end
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   346
		else
14595
c5f18710a184 Remove "loser" messages in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14583
diff changeset
   347
			SendStat(siGameResult, loc("Challenge over!"))
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   348
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   349
			SendStat(siCustomAchievement, string.format(loc("You have destroyed %d of %d targets (+%d points)."), scored, total_targets, end_score_targets))
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   350
			SendStat(siCustomAchievement, string.format(params.shootText, shots))
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   351
			if(shots > 0) then
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   352
				SendStat(siCustomAchievement, string.format(loc("Your accuracy was %.1f%%."), accuracy))
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   353
			end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   354
			end_score_overall = end_score_targets
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   355
		end
14583
50f511588635 Outsource commonly used words in siPointType to QTfrontend
Wuzzy <Wuzzy2@mail.ru>
parents: 14531
diff changeset
   356
		SendStat(siPointType, "!POINTS")
14485
208359558642 Use player-chosen team identity for training and a few challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14480
diff changeset
   357
		SendStat(siPlayerKills, tostring(end_score_overall), GetHogTeamName(player))
14480
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14469
diff changeset
   358
		-- Update highscore
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14469
diff changeset
   359
		updateChallengeRecord("Highscore", end_score_overall)
10656
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   360
	end
2093cf51eea1 Add (and slightly tweak) Wuzzy's Target Practice missions
mikade <redgrinner@gmail.com>
parents:
diff changeset
   361
end