Author Topic: LUA-question  (Read 11481 times)

Romanio(Rus)

  • Jr. Member
  • **
  • Posts: 13
  • FOR MOTHER RUSSIA!
Re: LUA-question
« Reply #30 on: October 08, 2012, 09:36:02 PM »
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_rus

  • Sr. Member
  • ****
  • Posts: 84
  • 0_o
Re: LUA-question
« Reply #31 on: October 10, 2012, 06:26:12 PM »
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?
« Last Edit: October 10, 2012, 06:29:25 PM by ImpassIve_rus »


Stephen

  • Administrator
  • Hero Member
  • *****
  • Posts: 259
Re: LUA-question
« Reply #32 on: October 11, 2012, 07:16:51 PM »
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:
Code: [Select]
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_rus

  • Sr. Member
  • ****
  • Posts: 84
  • 0_o
Re: LUA-question
« Reply #33 on: October 11, 2012, 07:42:10 PM »
Quote
kts.MENU.start_game_func

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

Quote
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

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...

Quote
Maybe 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

  • Administrator
  • Hero Member
  • *****
  • Posts: 259
Re: LUA-question
« Reply #34 on: October 11, 2012, 08:04:21 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
Quote
Maybe 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

  • Jr. Member
  • **
  • Posts: 10
Re: LUA-question
« Reply #35 on: October 15, 2012, 07:29:40 PM »
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_rus

  • Sr. Member
  • ****
  • Posts: 84
  • 0_o
Re: LUA-question
« Reply #36 on: October 16, 2012, 05:16:27 PM »
Hm...
Quote from: 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?
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_rus

  • Sr. Member
  • ****
  • Posts: 84
  • 0_o
Re: LUA-question
« Reply #37 on: October 18, 2012, 07:32:01 PM »
Hm... looks like knights_editor is not working with the current version of knights?
That is sad...


Stephen

  • Administrator
  • Hero Member
  • *****
  • Posts: 259
Re: LUA-question
« Reply #38 on: October 20, 2012, 12:43:55 PM »
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_rus

  • Sr. Member
  • ****
  • Posts: 84
  • 0_o
Re: LUA-question
« Reply #39 on: October 23, 2012, 04:13:39 PM »
Hm... you have said

Quote from: Stephen
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.

I have modified a bit Romanio's previous example:
Code: [Select]
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:
Quote
kts.HOOK_WEAPON_DOWNSWING -- called when any creature swings a weapon
Does that really means "any creature" as stated or "knights only"?
« Last Edit: October 24, 2012, 04:47:01 PM by ImpassIve_rus »


Stephen

  • Administrator
  • Hero Member
  • *****
  • Posts: 259
Re: LUA-question
« Reply #40 on: October 24, 2012, 09:10:24 PM »
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

Code: [Select]
pos.x = kts.GetRandomRange(0, xmax)
pos.y = kts.GetRandomRange(0, ymax)

with

Code: [Select]
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_rus

  • Sr. Member
  • ****
  • Posts: 84
  • 0_o
Re: LUA-question
« Reply #41 on: October 24, 2012, 09:28:14 PM »
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: Stephen
pos = 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: Stephen
It is actually only knights, not any creature.
Well, in this case corresponding description of kts.HOOK_WEAPON_DOWNSWING in wiki should be fixed?
« Last Edit: October 24, 2012, 10:53:02 PM by ImpassIve_rus »


Stephen

  • Administrator
  • Hero Member
  • *****
  • Posts: 259
Re: LUA-question
« Reply #42 on: October 25, 2012, 02:34:16 PM »
Quote from: Stephen
pos = 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: Stephen
It 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_rus

  • Sr. Member
  • ****
  • Posts: 84
  • 0_o
Re: LUA-question
« Reply #43 on: November 01, 2012, 07:38:19 PM »
Hm...
Quote from: 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?
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_rus

  • Sr. Member
  • ****
  • Posts: 84
  • 0_o
Re: LUA-question
« Reply #44 on: October 30, 2016, 04:44:13 PM »
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:
Code: [Select]
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
« Last Edit: October 30, 2016, 09:55:33 PM by ImpassIve_rus »