kts.MonsterType

Name

kts.MonsterType – create a new monster type

Synopsis

monster_type = kts.MonsterType{
    type = <"walking" or "flying">,

    -- For any monster type:
    anim = <anim>,
    corpse_tiles = <list of Tiles>,
    health = <function or integer>,
    on_attack = <function or nil>,
    on_damage = <function or nil>,
    on_death = <function or nil>,
    on_move = <function or nil>,
    speed = <integer>,
    
    -- For walking monsters:
    weapon = <item type>,
    ai_fear = <list of item types>,
    ai_hit = <list of item types>,
    ai_avoid = <list of tiles>,

    -- For flying monsters:
    attack_damage = <integer>,
    attack_stun_time = <function or integer>
}

Description

This function creates a new MonsterType. A MonsterType is a "category" of monsters, e.g. "vampire bat" or "zombie".

The function takes one parameter which is a table with the following fields:

type must be set to either "walking" or "flying". The type determines various properties of the monster, including which "tile access level" (walking or flying) it uses, how it attacks, and what sort of AI it uses.

anim gives the Anim to be used for the monster.

corpse_tiles is a list of tiles that can be used to represent the monster's corpse. When a monster dies, the first tile from the list is placed in that monster's square. If a second monster dies in the same square, that tile is removed, and the second tile from the list is placed instead; and so on. (So, for example, corpse_tiles for vampire bats contains three entries: the first contains an image of one dead bat, the second contains two dead bats, and the third contains three.)

health is the number of hitpoints given to the monster, or a function returning a number. (In the case of a function, the function is called once every time a monster of this type is created, which is good for giving different amounts of hitpoints to different instances of this monster.)

on_attack is a Lua function called when the monster attacks.

on_damage is a Lua function called when the monster takes damage.

on_death is a Lua function called when the monster dies.

on_move is a Lua function called when the monster moves.

(Each of the above four functions takes no parameters, and returns no results, but the cxt table will be available during the execution of the function. These functions are optional, and can be omitted or set to nil if required.)

speed controls how fast the monster moves (compared to a knight's normal speed, which is 100).

weapon gives an ItemType used to represent the monster's weapon. This item is never dropped into the dungeon, but it does set the stats for the monster's attack.

ai_fear gives a list of ItemTypes that the monster will run away from. For example, zombies fear the Wand of Undeath.

ai_hit gives a list of ItemTypes that the monster will try to attack. For example, zombies will hit open bear trap tiles.

ai_avoid gives a list of Tiles that the monster will not want to walk over. For example, zombies do not walk into open pit tiles.

attack_damage gives the amount of damage dealt by a flying monster's attack (flying monsters do not carry weapons, but simply attack their target when flying over them).

attack_stun_time gives the amount of time that a target will be stunned for, after suffering a flying monster's attack. This is measured in milliseconds. This can also be a function (returning an integer) if it is desired to randomise the stun time.

Return Value

A new MonsterType (a Lua userdata object) is returned.

Errors

Errors may be generated if any of the input parameters are incorrect.

Examples

In the Knights data files, vampire bats and zombies are defined as follows.

m_vampire_bat = kts.MonsterType {
   type = "flying",
   
   -- general properties:
   health = rng_range(1,2),     -- bats are not very tough
   speed = 86,                  -- slightly slower than a knight
   anim = a_vbat,
   corpse_tiles = { t_dead_vbat_1, t_dead_vbat_2, t_dead_vbat_3 },
   on_damage = snd_bat_screech,

   -- properties specific to flying monsters:
   attack_damage = 1,
   attack_stun_time = rng_time_range(2, 3)
}

m_zombie = kts.MonsterType {
   type = "walking",

   -- general properties:
   health = rng_range(2, 6),    -- zombies are tough (need 3 sword hits on average)
   speed = 67,                  -- zombies are slow
   anim = a_zombie,
   corpse_tiles = { t_dead_zombie },

   on_attack = snd_zombie,   -- called when zombie swings weapon
   on_damage = snd_zombie,   -- called when zombie takes non-fatal damage
   on_death = snd_zombie,    -- called when zombie dies
   on_move = function()      -- called when zombie moves
      -- There is a 5% chance of a zombie making a sound while it is
      -- moving around.
      if kts.RandomChance(0.05) then
         snd_zombie()
      end
   end,

   -- properties specific to walking monsters:
   weapon = zombie_weapon,

   -- list of tiles that zombies don't want to walk onto:
   ai_avoid = all_open_pit_tiles,

   -- list of items that zombies will whack with sword instead of walking onto:
   ai_hit = {i_bear_trap_open},

   -- list of items that zombies will run away from (if a knight is carrying one of them):
   ai_fear = {i_wand_of_undeath}
}

Notes:

See Also

cxt

kts.Anim

kts.ItemType

kts.Tile