📜 ⬆️ ⬇️

Tricks and hacks INSTEAD

If you are not familiar with the text game engine and visual stories INSTEAD, then you can read about it here . In short: this is a very convenient and simple text-based text adventure (quest) engine for programmers. What is important, it is not only convenient, it is also easily expandable, since all games are written on Lua. I will not describe how to write games on it. I will describe how to cut them with a file. Any tool is learned in business, isn't it?

Scenario 1: Item + Item


The player has inventory, there are several items in the inventory. The standard behavior of INSTEAD is this: you click on one item, then click on another - so you applied the first item to the second. To catch this event, you must define a use handler in the first item or used in the second. But if you apply the second item to the first item, you will need to intercept the new event again. And what if you just want to combine items, without regard to order?

We write hack.

cobj = function(v)
v.use = function(this,that)
return call(this, 'fuse', that);
end;
v.used = v.use;
return obj(v);
end


Like this. Now it is enough to declare objects of type cobj and give them the function fuse:
')
rope = cobj{
nam = '',
inv = function()
local response = ' .';
return response;
end,
fuse = function(this, that)
if (that == lock) then
inv():del(lock);
inv():del(rope);
inv():add(rope_with_lock);
return ' .'
end;
end,
}


Scenario 2: issuing a random phrase in the room description


If you make the description of the room a function that will simply return a random phrase, then it will be executed only once and will produce the same. To change the description after all, it is necessary to output it not through the return function, but through the text buffer - the functions p and pn:

something = function()
ifsen = ' ';
response = {' .', ' .', ' .', ' -.'};
return ifsen..response[rnd(#response)];
end
dinner = room {
nam = ' ',
dsc = function()
p [[ ]];
p (something())
end
};


If you do not put the brackets after something, then nothing happens. So you can get something like:



Scenario 3: New Object Type


Oh, this is a deep digging. Suppose you need a new type of objects in the rooms. Let's say you write RPGs (you can even have a MMORPG - if only the player had Lua libraries for working with the network), and you need a list of quests. For this, there is no need to rebuild the engine, it is enough to delve a little into its Lua part:

function quest(v) --constructor
if v.nam == nil then error (" .", 2) end
if v.short_dsc == nil then v.short_dsc = "" end
if v.scene == nil then v.scene = room_scene end
if v.completed == nil then v.completed = false end
if v.look == nil then v.look = room_look end
if v.save == nil then v.save = room_save end
v.location_type = true;
if v.way == nil then v.way = { } end
v.isQuest = true;
v.way = list(v.way);
v = obj(v);
return v;
end

function room_look(self)
local i,n,v,ph
for i,o in opairs(self.obj) do
if isObject(ref(o)) and not o.isQuest then
o = ref(o);
if v == nil then v = stead.par(' ',v, o:look());
else v = v .. stead.par(' ',v, o:look());
end
end
end

function room(v)
v.location_type = true;
if v.look == nil then v.look = room_look end
if v.scene == nil then v.scene = room_scene end
if v.quests == nil then v.quests = list {} end
if v.obj == nil then v.obj = v.quests
else for k,m in pairs(v.quests) do v.obj[k] = m end
end
if v.way == nil then v.way = {} end
v = room(v);
return v;
end


First, we define a new type of objects - quests. This is practically a copy-paste from the stead.lua object obj, only the isQuest field is added to make it easier to distinguish between quests and objects, and the completed field is added, which is responsible for completing the quest.

room_look is a function that is called when a player inspects a room. Usually it prints descriptions of all the objects in the room (from the obj array). Only a stub has been added here: do not display descriptions of all quests in the room.

The room itself has also been redefined - it now has an array of quests. The obj array is filled with objects from the quests array - it is equalized if it is empty, and it is connected if there are objects in it.

This is just an example, you can define any types of objects and rooms.

Here it is. And now, when you already know some clever tricks in this great engine, you can read the usual documentation .

Source: https://habr.com/ru/post/116958/


All Articles