#summary API for writing Lua scripts in Hedgewars. == Global available Constants ==
LAND_WIDTH, LAND_HEIGHT
Game flags:
gfForts, gfMultiWeapon, gfSolidLand, gfDivideTeams, gfLowGravity, gfLaserSight, gfInvulnerable, gfMines, gfVampiric, gfKarma, gfArtillery, gfOneClanMode, gfRandomOrder, gfKing, gfPlaceHog, gfSharedAmmo, gfDisableGirders, gfExplosives, gfDisableLandObjects, gfAISurvival, gfInfAttack, gfResetWeps, gfResetHealth, gfPerHogAmmo, gfDisableWind, gfMoreWind, gfTagTeam
*Note:* A few of these, e.g. gfResetHealth (and probably gfTagTeam) are not yet exposed to lua. More constants at [GearTypes Gear Types] , [AmmoTypes Ammo Types], [Sounds], [States] Additional global changing variables accessible: * !ClansCount - number of clans in the game (teams with the same color belong to one clan) * !TeamsCount - number of teams in the game * !TurnTimeLeft - number of game ticks left until the current turn ends * !GameTime - number of total game ticks * !RealTime - number of total ticks * !TotalRounds - number of round that has passed * !CurrentHedgehog - the hedgehog gear that is currently in play == Event Handlers == === onGameInit() ===
This function is called before the game loads its resources. One can modify various game variables here:
* Seed = 0 - sets the seed of the random number generator * !GameFlags = gfSolidLand + gfArtillery + … - sets the gameflags, see above for the available flags * !TurnTime = 60000 - set the turntime in ms * !CaseFreq = 0 - frequency of crate drops * !HealthCaseProb = 35 - chance of receiving a health crate * !HealthCaseAmount = 25 - amount of health in a health crate * !DamagePercent = 100 - percent of damage to inforce * !MinesNum = 0 - number of mines being placed (before 0.9.14 !LandAdds) * !MinesTime = 3 - time for a mine to explode from activated, -1 for random * !MineDudPercent = 0 - chance of mine being a dud * Explosives = 0 - number of explosives being placed * Delay = 0 - delay between each round * !SuddenDeathTurns = 30 - turns until sudden death begins * !WaterRise = 47 - height of water rise at sudden death in pixels * !HealthDecrease = 5 - amount of health decreased on sudden death * Map = "Bamboo" - the map being played * Theme = "Bamboo" - the theme to be used * Goals = "Jumping is disabled" - if you want to add info to the game mode dialog, use "|" to separate lines If you want to add teams or hogs manually you have to do it here. === onGameStart() ===
This function is called when the first round starts.
Can be used to show the mission and for more setup, for example initial target spawning. === onGameTick() ===
This function is called on every game tick, i.e. 1000 times a second.
=== onNewTurn() (0.9.15) ===
This function calls at the start of every turn.
=== onGearAdd(gearUid) ===
This function is called when a new gear is added. Useful in combination with !GetGearType(gearUid).
=== onGearDelete(gearUid) ===
This function is called when a new gear is deleted. Useful in combination with !GetGearType(gearUid).
=== onGearDamage(gearUid, damage) (0.9.15) ===
This function is called when a gear is damaged.
Example: function onGearDamage(gear, damage) if (!GetGearType(gear) == gtHedgehog) then -- adds a message saying, e.g. "Hoggy H took 25 points of damage" !AddCaption(!GetHogName(gear) .. ' took ' .. damage .. ' points of damage') end end === onGearResurrect(gearUid) (0.9.14) ===
This function is called when a gear is resurrected. CPU Hogs will resurrect if gfAISurvival is included in !GameFlags. Alternatively, specific gears can have heResurrectable set to true via !SetEffect.
=== onAmmoStoreInit() ===
This function is called when the game is initialized to request the available ammo and ammo probabilities. Use !SetAmmo here.
=== onAttack() ===
This function is called when your Hedgehog attacks.
=== onHJump() ===
This function is called when you press the high jump key.
=== onLJump() ===
This function is called when you press the long jump key.
=== onPrecise() ===
This function is called when you press the precise key.
=== onLeft() ===
This function is called when you press the left key.
=== onRight() ===
This function is called when you press the right key.
=== onUp() ===
This function is called when you press the up key.
=== onDown() ===
This function is called when you press the down key.
== Functions for creating gears == === !AddGear(x, y, gearType, state, dx, dy, timer) ===
This creates a new gear at position x,y (measured from top left) of kind gearType (see [GearTypes Gear Types]). The initial velocities are dx and dy. All arguments are numbers. The function returns the uid of the gear created.
Example: local gear = !AddGear(0, 0, gtTarget, 0, 0, 0, 0) !FindPlace(gear, true, 0, LAND_WIDTH) === !AddVisualGear(x, y, viusalGearType, state, critical) ===
This creates a new visual gear at position x,y (measured from top left) of kind visualGearType (see [VisualGearTypes Visual Gear Types]). The function returns the uid of the visual gear created. Set critical to true if the visual gear is crucial to game play. False if it is just an effect, and can be skipped when in fastforward (such as when joining a room). A critical visual gear will always be created, a non-critical one may fail. Most visual gears delete themselves.
Example: -- adds an non-critical explosion at position 1000,1000. Returns 0 if it was not created. vgear = !AddVisualGear(1000, 1000, vgtExplosion, 0, false) === !SpawnHealthCrate(x, y) (0.9.14) ===
Spawns a health crate at the specified position.
=== !SpawnAmmoCrate(x, y, ammoType) (0.9.14) ===
Spawns an ammo crate at the specified position with content of ammoType (see [AmmoTypes Ammo Types]). Because by default settings the number of ammo in crates is zero it has to be increased to at least one with SetAmmo first, see the example:
Example: !SetAmmo(amGrenade, 0, 0, 0, 1) -- see below !SpawnAmmoCrate(0, 0, amGrenade) -- x=y=0 means random position on map === !SpawnUtilityCrate(x, y, ammoType) (0.9.14) ===
Spawns an utility crate at specified position with content of ammoType.
Example: !SetAmmo(amLaserSight, 0, 0, 0, 1) !SpawnUtilityCrate(0, 0, amLaserSight) === !AddTeam(teamname, color, grave, fort, voicepack, flag) ===
Adds a new team. Note that this can only be done in onGameInit(), not at a later time. First argument is the team name followed by color, grave, fort, voicepack and flag settings.
Notice: This works only for singleplayers training missions for now and will desync multiplayer games. Flag setting is dev only. Example: !AddTeam("team 1", 14483456, "Simple", "Island", "Default", "hedgewars") === !AddHog(hogname, botlevel, health, hat) ===
Adds a new hedgehog for current team (last created one), with botlevel and specified health also head.
Notice: This works only for singleplayers training missions for now and will desync multiplayer games. Example: local player = !AddHog("HH 1", 0, 100, "!NoHat") -- botlevel 0 means human player !SetGearPosition(player, 1500, 1000) == Functions to get gear properties == === !GetGearType(gearUid) ===
returns one of [GearTypes Gear Types] for the specified gear
=== !GetGearPosition(gearUid) ===
returns tuple of x,y coordinates for the specified gear
=== !GetGearVelocity(gearUid) (0.9.15) ===
returns tuple of dx,dy values for the specified gear
=== !GetGearElasticity(gearUid) (0.9.15) ===
Returns the elasticity of the specified gear. Useful for determining if a hog is on a rope or not. If a hog is attached to a rope, or is busy firing one, the elasticity of the rope will be non-zero.
=== !GetHogClan(gearUid) ===
returns the clan id of the specified hedgehog gear
=== !GetHogTeamName(gearUid) ===
returns the name of the specified hedgehog gear's team
=== !GetHogName(gearUid) ===
returns the name of the specified hedgehog gear
=== GetX(gearUid) ===
returns x coordinate of the gear
=== GetY(gearUid) ===
returns y coordinate of the gear
=== !GetState(gearUid) ===
returns the state of the gear. This is one of [States]
=== !GetFollowGear(gearUid) ===
Returns the uid of the gear that is currently being followed.
=== !GetTimer(gearUid) (0.9.14) ===
returns the timer of the gear. This is for example the time it takes for watermelon, mine, etc. to explode. This is also the time used to specify the blowtorch or rcplane time.
=== !GetHealth(gearUid) (0.9.14) ===
returns the health of the gear
=== !GetBotLevel(gearUid) (0.9.14) ===
returns the bot level from 0 to 5. 0 means human player.
=== !GetVisualGearValues(vgUid) (0.9.15) ===
This returns the typically set visual gear values, useful if manipulating things like smoke or bubbles or circles. It returns the following values: X, Y, dX, dY, Angle, Frame, FrameTicks, State, Timer, Tint X, Y typically position, dX, dY typically speed, Angle is usually the rotation angle, Frame is typically the animation frame, FrameTicks is usually an animation counter. State can have a variety of values, but is typically bit packed, Timer is usually the gear lifetime and Tint is the colour. Most visual gears require little to no modification of parameters.
Example: !GetVisualGearValues(vgUid) -- return visual gear values == Functions to modify gears == === !DeleteGear(gearUid) ===
Deletes a Gear
Example: gear = !AddGear(…) !DeleteGear(gear) -- Delete the newly created gear. === !DeleteVisualGear(vgUid) (0.9.15) ===
Deletes a Visual Gear. Note, most visual gears delete themselves.
Example: vgear = !AddVisualGear(…) !DeleteVisualGear(vgear) -- Delete the newly created visual gear. === !SetVisualGearValues(vgUid, X, Y, dX, dY, Angle, Frame, FrameTicks, State, Timer, Tint) (0.9.15) ===
This allows manipulation of many of the visual gear values. Calling GetVisualGearValues first is recommended on most visual gears unless you are controlling all the key values. In the case of vgtCircle, the visual gear values are mapped as follows. X, Y: position. State: radius. Timer: Thickness. FrameTicks: pulsation speed (0 to disable). dX, dY: min/max pulsation opacity (0-255). Tint: colour, RGBA. Most visual gears require little to no modification of parameters.
Example: -- set a circle to position 1000,1000 pulsing from opacity 20 to 200 (8%-78%), radius of 50, 3px thickness, bright red. !SetVisualGearValues(circleUid, 1000,1000, 20, 200, 0, 0, 100, 50, 3, 0xff0000ff) === !FindPlace(gearUid, fall, left, right) ===
Finds a place for the specified gear between x=left and x=right and places it there.
Example: gear = !AddGear(…) !FindPlace(gear, true, 0, LAND_WIDTH) -- places the gear randomly between 0 and LAND_WIDTH === !HogSay(gearUid, text, manner) ===
Makes the specified gear say, think, or shout some text in a bubble.
Example: !HogSay(!CurrentHedgehog, "I wonder what to do...", SAY_THINK) -- thought bubble with text !HogSay(!CurrentHedgehog, "I'm hungry...", SAY_SAY) -- speech bubble with text !HogSay(!CurrentHedgehog, "I smell CAKE!", SAY_SHOUT) -- exclamatory bubble with text === !HogTurnLeft(gearUid, boolean) ===
Faces the specified hog left or right.
Example: !HogTurnLeft(!CurrentHedgehog, true) -- turns !CurrentHedgehog left !HogTurnLeft(!CurrentHedgehog, false) -- turns !CurrentHedgehog right === !SetGearPosition(gearUid, x, y) ===
Places the specified gear exactly at the position (x,y).
=== !SetGearVelocity(gearUid, dx, dy) (0.9.15) ===
Gives the specified gear the velocity of dx, dy.
=== !SetAmmo(ammoType, count, probability, delay, numberInCrate) ===
This updates the settings for a specified [AmmoTypes Ammo Type] as of count available for players, spawn probability, availability delay in turns, and the number available in crates. This is supposed to be used in the onAmmoStoreInit() event handler.
Example: !SetAmmo(amShotgun, 9, 0, 0, 0) -- unlimited amount of shotgun ammo for players !SetAmmo(amGrenade, 0, 0, 0, 3) -- crates should contain always three grenade === !AddAmmo(gearUid, ammoType) ===
Adds ammoType to the specified gear. The amount added is determined by the arguments passed via !SetAmmo() in the onAmmoStoreInit() event handler. *Note:* The effectiveness of this function may be limited due to problems with gfPerHogAmmo in lua scripting.
=== !SetHealth(gearUid, health) ===
Sets the health of the specified gear. This can be used for purposes other than killing hedgehogs. For example: * Starting the RC Plane 10 shots * Starting Flying Saucer (gtJetpack) with only 50% fuel. * Setting all the mines to duds. * (And more!) function onGearAdd(gear) if (!GetGearType(gear) == gtRCPlane) then !SetHealth(gear, 10) end if (!GetGearType(gear) == gtJetpack) then !SetHealth(gear, 1000) end if (!GetGearType(gear) == gtMine) then !SetHealth(gear, 0) end end
=== !SetEffect(gearUid, effect, true/false) (0.9.14 / dev) ===
Enables (true) or disables (false) one of the effects heInvulnerable, heResurrectable, hePoisoned for the specified hedgehog gear.
Example: (sets all bots poisoned) function onGearAdd(gear) if (!GetGearType(gear) == gtHedgehog) and (!GetBotLevel(gear) > 0) then !SetEffect(gear, hePoisoned, true) end end Notice: In 0.9.14 this function takes 0 or 1 instead of true/false. === CopyPV(gearUid, gearUid) ===
This sets the position and velocity of the second gear to the first one.
=== CopyPV2(gearUid, gearUid) (deprecated) ===
This sets the position of the second gear to that of the first one, but makes its velocity twice the one of the first.
Notice: This is removed in dev, use GetGearVelocity and SetGearVelocity instead. === !FollowGear(gearUid) ===
Makes the gameclient follow the specifiec gear.
=== !SetState(gearUid, state) ===
Sets the state of the specified gear to one of [States].
=== !SetTag(gearUid, tag) ===
Sets the tag of the specified gear, different for every gear.
=== !SetTimer(gearUid, timer) (0.9.14) ===
Sets the timer of the specified gear. Also see !GetTimer.
=== !SetBotLevel(gearUid) (dev) ===
Sets the bot level from 0 to 5. 0 means human player.
== Other Functions == === !EndGame() ===
Makes the game end.
=== !ParseCommand(string) ===
Makes the gameclient parse the specified custom command.
=== !ShowMission(caption, subcaption, text, icon, time) ===
Use to tell the player what he is supposed to do. As of (0.9.15), icon currently accepts: * -amBazooka, -amShotgun etc. (and other ammo types) * 0 (Gold Crown icon) * 1 (Target icon) * 2 (Exclamation mark) * 3 (Question mark) * 4 (Gold star)
=== !HideMission() ===
Hides the mission.
=== !AddCaption(text) (0.9.14) ===
Display event text in the upper part of the screen.
=== !PlaySound(soundId) ===
Plays the specified sound.
=== !PlaySound(soundId, gearUid) (0.9.15) ===
Plays the specified sound for the chosen hedgehog's team.
=== !SetInputMask(mask) (0.9.15) ===
Masks specified player input.
Example: -- masks the long and high jump commands SetInputMask(band(0xFFFFFFFF, bnot(gmLJump + gmHJump))) -- clears input mask, allowing player to take actions SetInputMask(0xFFFFFFFF) Note: Using the input mask is an effective way to script uninterrupted cinematics, or create modes such as No Jumping. === !SetZoom(zoomLevel) (0.9.14) ===
Sets the zoom level. The value for maximum zoom is currently 1.0 and for minimum 3.0 The default zoom level is 2.0
=== !GetZoom() (0.9.14) ===
Returns the current zoom level
=== !GetRandom(number) (0.9.14) ===
Returns a randomly generated number in the range of 0 to number - 1. This random number uses the game seed, so is synchronised, and thus safe for multiplayer and saved games. Use GetRandom for anything that could impact the engine state. For example, a visual gear can use the Lua random, but adding a regular gear should use GetRandom.
=== !SetWind(windSpeed) (0.9.15) ===
Sets the current wind in the range of -100 to 100. Use together with gfDisableWind for full control.
=== !GetDataPath() (dev) ===
Returns the path to the data directory, used when adding libraries.
=== !GetClanColor(clan) (0.9.15) ===
Returns the colour of the chosen clan by its number.
=== !PlaceGirder(x, y, state) (dev) ===
Places a girder with centre points x, y and the chosen state. These are the accepted states: * 0: short, vertical * 1: short, decreasing * 2: short, horizontal * 3: short, increasing * 4: long, vertical * 5: long, decreasing * 6: long, horizontal * 7: long, increasing
=== !GetCurAmmoType() (dev) ===
Returns the currently selected [AmmoTypes Ammo Type].
== Debugging Functions == === !WriteLnToConsole(string) (0.9.14) ===
Writes (string) to the chat place, as if you had pressed t and typed it.