News:

SMF - Just Installed!

Main Menu

LUA-question

Started by doublejack, September 03, 2012, 03:22:17 PM

Previous topic - Next topic

Romanio(Rus)

Thank you Stephen!
Indeed, as soon as I remove the brackets after the spawn, it worked :o
Thank you for your detailed response.
And, as said ImpassIve, I need to use kts.AddMonster instead C.add_initial_monsters

ImpassIve

#31
Hm... just some random thoughts about mods...

If you want to make your mod switchable (on/off) you will meet several questions.

- First. How to determine the moment when the game started? Yes, "features" function on menu elements is nice, but there are some special moments... What if you have none elements in the menu? Ok, let's suppose what you have something in menu, but you need to make something after generation of the dungeon is finished (place special monsters or replace/move some tiles)...

- Well, in that case, you can use kts.SetRespawnFunction(f). But only one mod (as I suppose) can use this in the same time. So, if you decided to "install" your custom respawn function, others won't be able to do it... or even you will "lose" your already installed respawn function if someone else needs this function too. Because while using hooks, you can find an old hook function and manually call it after your function... but here is no kts.GetRespawnFunction.

- Hm... but you can make some plugin what will claim this function for himself and send to registered mod some kind of notification about respawn event. Well, I have thought about adding such feature to my Toolbox mod (in the same way as hooks were implemented in it)... But it can't (as far as I know) call kts.SetRespawnFunction because this mod doesn't have any elements in the menu, lol...

- I am not talking about using existing hooks, because if you want to generate monsters in the dungeon - it is a bad idea to wait until someone will be hurt/step on some special tile etc. etc.

- The second question is how to determine the fact what the game already finished? So, you can delete all your hooks, free data tables, etc? Because there is a strong chance what in the next match your mod will be disabled, but you will get no notification about that... so, in some cases (excluding custom respawn function, initial mosters and other "auto-disabling" options) your mod will be acting as enabled. How to avoid such situation?


Stephen

1) kts.SetRespawnFunction() is used to control where the knight respawns. It is not meant as a general "initialization" function.

For example, the Tutorial uses kts.SetRespawnFunction to make the knight respawn close to where he died, instead of all the way back at the beginning of the tutorial.

It would not make sense for two mods to install their own respawn function, because a knight can logically only respawn in a single place :)

2) It sounds like you want to be able to call functions at the start of the game, but after the dungeon has been created. To do that you need to replace "kts.MENU.start_game_func" with your own function.

For example:

require ("classic")

local old_start_game_func = kts.MENU.start_game_func

kts.MENU.start_game_func = function(S)
   -- call the original start_game_func first... this will create the dungeon
   old_start_game_func(S)

   -- now do your stuff here
end


3) There is no way to "do something at the end of the game" currently. Maybe there should be an "end_game_func" to complement "start_game_func"?

ImpassIve

Quotekts.MENU.start_game_func

Oh, thanks, I didn't notice this function!

QuoteIt would not make sense for two mods to install their own respawn function, because a knight can logically only respawn in a single place

Yes, that seems unlikely now (because I didn't knew about start_game_func before). But that scheme is still possible if the 'first' function in most cases passes (returns nil). So, in 'regular' situation spawn position would be determined by the second function... but in some 'extreme' situations (maybe, the knight have lost all his 'lives'?) first function will setup some specific spawn point, or e.g. eliminate this knight.
Well, I realize, that this situation is far-fetched, but it still may occur...

QuoteMaybe there should be an "end_game_func" to complement "start_game_func"?

I can't give many examples of using such function, but it could be useful in some specific cases...


Stephen

Quote from: ImpassIve_rus on October 11, 2012, 07:42:10 PM
Yes, that seems unlikely now (because I didn't knew about start_game_func before). But that scheme is still possible if the 'first' function in most cases passes (returns nil). So, in 'regular' situation spawn position would be determined by the second function... but in some 'extreme' situations (maybe, the knight have lost all his 'lives'?) first function will setup some specific spawn point, or e.g. eliminate this knight.
Well, I realize, that this situation is far-fetched, but it still may occur...

Hmm, well, it would not be hard to add "kts.GetRespawnFunction" so I should probably do it :)

Quote
QuoteMaybe there should be an "end_game_func" to complement "start_game_func"?

I can't give many examples of using such function, but it could be useful in some specific cases...

OK, I'll put it down as a low-priority feature.

doublejack

maybe if we are going to use toolbox in all knights mods so it would be a good idea to make a new button in game menu, something like "addons" where you can switch on/off ALL installed mods, taking information from toolbox?

ImpassIve

Hm...
Quote from: doublejackmaybe if we are going to use toolbox in all knights mods so it would be a good idea to make a new button in game menu, something like "addons" where you can switch on/off ALL installed mods, taking information from toolbox?
1) Term "all mods" is not quite correct. Toolbox is an optional mod, because you can implement all functions from it by yourself. So, if you have made a mod, which don't need to interact with others - why would you use toolbox in it?
2) Anyway, this feature requires free space in menu screen. Imho, it is better to make such disabling feature in each mod independently. Because all existing at this moment mods (Telewand, Living Armours, Spiders) can be "disabled" from menu (L.A. and Spiders - just set corresponding activity to 0; Telewand - just do not select this wand in the menu). Why should we add some new menu elements to do the same thing?


ImpassIve

Hm... looks like knights_editor is not working with the current version of knights?
That is sad...


Stephen

Yes, unfortunately, that is true, the knights editor does not work with the current version of Knights.

I do need to fix that but unfortunately I don't really have time right now...

ImpassIve

#39
Hm... you have said

Quote from: StephenIt sounds like you want to be able to call functions at the start of the game, but after the dungeon has been created. To do that you need to replace "kts.MENU.start_game_func" with your own function.

I have modified a bit Romanio's previous example:
module(...)
local C = require("classic")
local M = require("menu")

local thelayout = nil
local mapwidth = 0
local mapheight = 0

function getlayout()
if thelayout == nil then
thelayout = C.Dsetup.layout.func()
mapwidth  = (thelayout.width  * 12) + thelayout.width  + 1
mapheight = (thelayout.height * 12) + thelayout.height + 1
end
end


local function spawn()
    local pos = { x, y }
    local xmax = mapwidth
    local ymax = mapheight
print("Spawn begin!")
for i=1,10000 do
pos.x = kts.RandomRange(0, xmax)
pos.y = kts.RandomRange(0, ymax)
cr = kts.AddMonster(pos, C.m_vampire_bat)
--print("x",pos.x,"y",pos.y)
if cr ~= nil then
print("A creature was added")
break
end
end
print("Spawn end!")
if old_game_f then
old_game_f()
end
end

spawn_item = {
id = "spawn-bat",
text = "Spawn",
choice_min = 1,
choice_max = 1,
features = function(S)
getlayout()
--kts.SetRespawnFunction(spawn)
end
}

table.insert(kts.MENU.items, spawn_item)
old_game_f = kts.MENU.start_game_func
--kts.MENU.start_game_func = spawn


So why if I would uncomment
--kts.SetRespawnFunction(spawn)
-the game will print "Spawn begin!/A creature was added/Spawn end!"

but if instead I would uncomment
--kts.MENU.start_game_func = spawn
-the game will print only "Spawn begin!/Spawn end!"

Does that mean that we can't place monsters using this function?
____________
Also:
Quotekts.HOOK_WEAPON_DOWNSWING -- called when any creature swings a weapon
Does that really means "any creature" as stated or "knights only"?


Stephen

1) You need to call old_game_f() before you do your spawning. Move "if old_game_f then old_game_f() end" up to the top of the spawn() function. Then it works.

The reason is that old_game_f is the thing that creates the dungeon. If you haven't called that yet, then the dungeon doesn't exist and no monsters can be created.

ALSO: Please replace


pos.x = kts.GetRandomRange(0, xmax)
pos.y = kts.GetRandomRange(0, ymax)


with


pos = kts.GetRandomPos()


You can then delete your getlayout() function and thelayout, mapwidth, mapheight variables. This will simplify your code.

2) It is actually only knights, not any creature.

In my Monster Proposal thread, I proposed to add an "on_attack" function for each monster type. This would be the equivalent of HOOK_WEAPON_DOWNSWING but it would be defined per monster type instead of globally. Therefore you could create different sound effects for different creature types (for example).

ImpassIve

#41
Quote from: Stephen
You need to call old_game_f() before you do your spawning.
oh... thanks. I will have to fix this in Toolbox mod..)

Quote from: Stephenpos = kts.GetRandomPos()
Yes, I remember this. But since there is no such function in current 'official' release (only in dev. versions) I prefer not to use new features yet (or use, but in separated 'dev' version of a mod...)

Quote from: StephenIt is actually only knights, not any creature.
Well, in this case corresponding description of kts.HOOK_WEAPON_DOWNSWING in wiki should be fixed?


Stephen

Quote from: ImpassIve_rus on October 24, 2012, 09:28:14 PM
Quote from: Stephenpos = kts.GetRandomPos()
Yes, I remember this. But since there is no such function in current 'official' release (only in dev. versions) I prefer not to use new features yet (or use, but in separated 'dev' version of a mod...)

OK fair enough :)

Quote
Quote from: StephenIt is actually only knights, not any creature.
Well, in this case corresponding description of kts.HOOK_WEAPON_DOWNSWING in wiki should be fixed?

Thanks, I have updated the wiki page.

ImpassIve

Quote from: ImpassIve_rus on October 16, 2012, 05:16:27 PM
Hm...
Quote from: doublejackmaybe if we are going to use toolbox in all knights mods so it would be a good idea to make a new button in game menu, something like "addons" where you can switch on/off ALL installed mods, taking information from toolbox?
1) Term "all mods" is not quite correct. Toolbox is an optional mod, because you can implement all functions from it by yourself. So, if you have made a mod, which don't need to interact with others - why would you use toolbox in it?
2) Anyway, this feature requires free space in menu screen. Imho, it is better to make such disabling feature in each mod independently. Because all existing at this moment mods (Telewand, Living Armours, Spiders) can be "disabled" from menu (L.A. and Spiders - just set corresponding activity to 0; Telewand - just do not select this wand in the menu). Why should we add some new menu elements to do the same thing?

I have changed my mind.
That is a really nice idea, thanks for it!
This was implemented in 4th version of Toolbox.


ImpassIve

#44
That's me again!
Yesterday, Moo and I were discussing some mod ideas and he pointed out the fact "ai_fear" field wouldn't work for bats.

I had to recheck this, and that's true: monsters with "flying" type are ignoring this field - FlyingMonsterAI class constructor doesn't even has a parameter to take this list.
So, the question is:
- Right now, do we have any way to force flying monster to run away from all knights? Originally, I was planning to add all possible weapon types into "ai_fear" list, but maybe indirect manipulation over "run_away_flag" would somehow work? (I don't know, how about constantly hitting a monster with zero damage strikes via script?)
- Otherwise, is there any chance of implementing such a possibility in the future, or should we stick to a walking type of monsters?

Thanks for attention

P.S. I've tried "hitting a monster with zero damage" idea by adding custom "on_move" function, but that didn't worked for this reason:

bat->move(MT_MOVE);  // It moves
bat->runMovementAction(); // It calls "on_move" lua function
bat->clearRunAwayFlag(); // It resets the flag

"on_move" action is called immediately before resetting the flag, so we cannot use it - at least not for the monster, who caused the event.
But if we would find another reliable event generator - I guess, this method may actually work