T4 Modules Howto Guide/Objects

From Tales of Maj'Eyal
Revision as of 22:11, 6 July 2013 by Ibanix (Talk | contribs)

Jump to: navigation, search

Objects: Loot and Equipment

Editing the Example Module to use ActorInventory

If your module will make use of objects and equipment, some changes are needed to the code since the example module does not by default use the ActorInventory class.

In your load.lua you should call at the top of the file

local ActorInventory = require "engine.interface.ActorInventory"


You can then define slots through the command:

ActorInventory:defineInventory("SLOT", "slot name", IS_WORN, "slot desc")


with SLOT being the slot's codeside identifier, "slot name" being the display name of the slot, IS_WORN being a boolean that determines if its a worn slot or a container slot and "slot_desc" is the description of the slot.

You will also then have to edit the module's /class/Actor.lua file.
Add

require "engine.interface.ActorInventory"


to the requires at the top of the file and add the engine.interface.ActorInventory to its inheritances:

module(..., package.seeall, class.inherit(
    engine.Actor,
    engine.interface.ActorInventory,    -- Add this line to whatever is already there!
    engine.interface.ActorTemporaryEffects,
    engine.interface.ActorLife,
    engine.interface.ActorProject,
    engine.interface.ActorLevel,
    engine.interface.ActorStats,
    engine.interface.ActorTalents,
    engine.interface.ActorResource,
    engine.interface.ActorFOV,
    mod.class.interface.Combat
))

THEN, in the _M:init(t, no_default) function add:

engine.interface.ActorInventory.init(self, t)

Now you need to tell the game what slots each specific Actor can use. Say you used the defineInventory function to add the "MAINHAND", "OFFHAND", "HEAD", and "BODY" slots to the game. You should put the following table into the birth descriptor for your player:

    body = { INVEN = 10, MAINHAND = 1, OFFHAND = 1, BODY = 1, HEAD = 1 },


Your player can know carry 10 items (the INVEN slots) and have one piece of equipment in each of the other slots. NPCs work the same way, by adding a body table to their NPCs newEntity definition. For example a headless horseman would use this code:

    body = { INVEN = 10, MAINHAND = 1, OFFHAND = 1, BODY = 1 },


Notice the missing HEAD slot, which tells the game that this NPC cannot use any equipment marked for the HEAD slot!


Adding objects

Now that the game has an inventory system implemented you can start making objects. For now let us put the objects into the "data/zones/dungeon/objects.lua" file. How about adding some weapons to the Example module? Let us start with a two-handed battleaxe:


newEntity{
    define_as = "BASE_BATTLEAXE",
    slot = "MAINHAND",
    slot_forbid = "OFFHAND",
    type = "weapon", subtype="battleaxe",
    display = "/", color=colors.SLATE,
    encumber = 3,
    rarity = 5,
    combat = { sound = "actions/melee", sound_miss = "actions/melee_miss", },
    name = "a generic battleaxe",
    desc = [[t4modules massive two-handed battleaxes.]],
}

newEntity{ base = "BASE_BATTLEAXE",
    name = "iron battleaxe",
    level_range = {1, 10},
    require = { stat = { str=11 }, },
    cost = 5,
    combat = {
        dam = 10,
    },
}

There is a lot going on in there. First, notice we define a "BASE_BATTLEAXE" that specific types of battleaxes can inherit from. This stores things like which slots the battleaxe can be equipped in (this is what we set up above), which slots this will stop us from using, and some other properties like encumbrance, rarity and description. Second, we make a specific type of iron battleaxe that inherits using the "base" variable. The resulting entity will have all of the properties of the base entity except when there is an explicit conflict. An example of this is the "name" variable. The name variable from the iron battleaxe will overwrite the "a generic battleaxe" name from the base entity. Tables will not overwrite each other, instead they will merge. This can be seen in the "combat" variable, which in the iron battleaxe will be a table with the "sound" and "sound_miss" from the base class and the "dam" from the child class.


Adding objects to a Zone

The "objects.lua" file for the dungeon zone now has some objects in it, but we need to tell T-Engine how many objects to add to the zone and where to put them. This is handled by a generator, and we tell T-Engine which generator to use in the "zone.lua" file. If you open up zone.lua you see that there are already generator entries for the map and the actors:

    generator =  {
        map = {
            class = "engine.generator.map.Roomer",
            nb_rooms = 10,
            rooms = {"simple", "pilar"},
            lite_room_chance = 100,
            ['.'] = "FLOOR",
            ['#'] = "WALL",
            up = "UP",
            down = "DOWN",
            door = "DOOR",
        },
        actor = {
            class = "engine.generator.actor.Random",
            nb_npc = {20, 30},
        },
    },


Well we need to add a generator for objects, too! This code will do the trick if added right after the actor = {...} line:

        object = {
            class = "engine.generator.object.Random",
            nb_object = {20, 30},
        },


Now T-Engine will randomly generate and drop 20-30 objects. Give it a try!

The next step is to implement Inventory Dialogs

Go back to T4 Modules Howto Guide