|
= Stendhal Application =
== Objects and Functions ==
The following objects & functions are exposed to the Lua engine:
=== luajava ===
This is an object of the LuajavaLib library. It can be used to coerce Java static objects to Lua or create new Java object instances.
Example of exposing a static object & enums to Lua:
<pre>
-- store a Java enum in a Lua global variable
ConversationStates = luajava.bindClass("games.stendhal.server.entity.npc.ConversationStates")
-- access the enum values like so
ConversationStates.IDLE
</pre>
Example of creating an object instance:
<pre>
-- store instance in local variable
local dog = luajava.newInstance("games.stendhal.server.entity.npc.SilentNPC")
-- access object methods like so
dog:setEntityClass("animal/puppy")
dog:setPosition(2, 5)
-- class with constructor using parameters
local speaker = luajava.newInstance("games.stendhal.server.entity.npc.SpeakerNPC", "Frank")
speaker:setOutfit("body=0,head=0,eyes=0,hair=5,dress=5")
speaker:setPosition(2, 6)
</pre>
To make scripting easier, Stendhal employs a {{StendhalFile|master|src/games/stendhal/server/core/scripting/lua/init.lua|master script}} & some helper objects & methods to handle the functionality mentioned above. An explanation of these objects & methods follows.
=== game ===
The main object that handles setting zone & adding entities to game.
Methods:
* <code>game:add(object)</code> - Adds an object to the current zone.
* <code>game:setZone(name)</code> - Sets the current zone.
* <code>game:createSign(visible)</code> - Creates a new {{StendhalFile|master|src/games/stendhal/server/entity/mapstuff/sign/Sign.java|Sign}} instance.
* <code>game:createShopSign(name, title, caption, seller)</code> - Creates a new {{StendhalFile|master|src/games/stendhal/server/entity/mapstuff/sign/ShopSign.java|ShopSign}} instance.
=== npcHelper ===
This object helps to create instances of {{StendhalFile|master|src/games/stendhal/server/entity/npc/SpeakerNPC.java|SpeakerNPC}} & {{StendhalFile|master|src/games/stendhal/server/entity/npc/SilentNPC.java|SilentNPC}} classes.
Methods:
* <code>npcHelper:createSpeakerNPC(name)</code> - Creates a new SpeakerNPC.
* <code>npcHelper:createSilentNPC()</code> - Creates a new SilentNPC.
* <code>npcHelper:setPath(npc, path, loop)</code> - Sets the path for the specified NPC.
* <code>npcHelper:setPathAndPosition(npc, path, loop)</code> - Sets the path & starting position of the specified NPC.
* <code>npcHelper:addMerchant(merchantType, npc, items, offer)</code> - Adds merchant behavior to <code>npc</code> of either a buyer or seller defined by <code>merchantType</code>.
* <code>npcHelper:addSeller(npc, items, offer)</code> - Adds seller merchant behavior to <code>npc</code>.
* <code>npcHelper:addBuyer(npc, items, offer)</code> - Adds buyer merchant behavior to <code>npc</code>.
== Zones ==
<pre>
game:setZone("0_semos_city")
</pre>
The logger is exposed to Lua via the <code>logger</code> object:
<pre>
local zone = "0_semos_city"
if game:setZone(zone) then
-- do something
else
logger:error("Could not set zone: " .. zone)
end
</pre>
=== Signs ===
Signs can be created with <code>gameentities:createSign</code> and <code>gameentities:createShopSign</code>:
<pre>
if game:setZone(zone) then
-- create the sign instance
local sign = gameentities:createSign()
sign:setEntityClass("signpost")
sign:setPosition(12, 55)
=== NPCs ===
Use the <code>gameentities:createSpeakerNPC</code> method to create an interactive NPC:
<pre>
if game:setZone(zone) then
-- Use helper object to create a new NPC
local npc = npcHelperentities:createSpeakerNPC("Lua")
npc:setEntityClass("littlegirlnpc")
npc:setPosition(10, 55)
-- Use helper object to create NPC path
npcHelperentities:setPath(npc, nodes)
-- Dialogue
A simple example of adding a chat transition can be done without any special functionality:
<pre>
local frank = npcHelperentities:createSpeakerNPC("Frank")
frank:add(ConversationStates.IDLE,
ConversationPhrases.GREETING_MESSAGES,
</pre>
This simply adds a response to saying "hello" & sets the NPC to attend to the player (equivalent of <code>frank:addGreeting("Hello")</code>).
For more complicated behavior, we need to use some helper methods. If we want to check a condition we use the <code>newCondition</code> global function:
In this scenario, the NPC will only respond if the player is carrying <item>money</item>.
A NotCondition instance can be created with the <code>newNotCondition</code> global function or using the <code>conditions.not</code> method:
Example usage:
<pre>
-- using newNotCondition
local condition = newNotCondition(newCondition("PlayerHasItemWithHimCondition", "money"))
-- using conditions.not
local condition = conditions.not(newCondition("PlayerHasItemWithHimCondition", "money")
</pre>
==== Adding Merchant Behavior ====
The <code>merchants</code> object is used for adding merchant behavior (buying/selling) to an NPC.
Merchant behavior ''(buying/selling)'' can be set with one of the following helper functions:
* ''npcHelper:addMerchant(merchantType, npc, prices, addOffer)''
* ''npcHelper:addBuyer(npc, prices, addOffer)''
* ''npcHelper:addSeller(npc, prices, addOffer)''
** Arguments:
*** ''merchantType:'' (string) If set to "buyer", will add buyer behavior, otherwise will be "seller" (may change type to boolean in future).
*** ''npc:'' (SpeakerNPC) The NPC to add the behavior to.
*** ''prices:'' (Map<String, Integer> or LuaTable) List of items & their prices.
*** ''addOffer:'' (boolean) If <code>true</code>, will add default replies for "offer".
Example of adding seller behavior to an NPC:
<pre>
if game:setZone("0_semos_city") then
local frank = npcHelperentities.createSpeakerNPC("Frank")
npcHelpermerchants:addSeller(frank, merchants.shops:get("shopname"), true)
game:add(frank)
Then add the seller behavior using the custom list:
<pre>
npcHelpermerchants:addSeller(frank, priceList, true)
</pre>
== System Properties ==
Java's system properties are exposed to Lua with the <code>game:getPropertyproperties</code>, <code>game:propertyEnabled</code>, and <code>game:propertyEquals</code> methodsobject.
Examples:
<pre>
-- property state
if gameproperties:propertyEnabledenabled("stendhal.testserver") then
print("Test server enabled")
if gameproperties:propertyEqualsequals("stendhal.testserver", "junk") then
print("Junk enabled")
else
-- property value
local prop = gameproperties:getPropertygetValue("stendhal.testserver")
if prop ~= nil then
print("Test server enabled")
|