(done) Pain sounds for hitting monsters?

This forum is for the Lua scriptable clone of DM/CSB called Dungeon Strikes Back by Sophia. Use DSB to build your own highly customised games.

Moderator: Sophia

Forum rules
Please read the Forum rules and policies before posting. You may Image to help finance the hosting costs of this forum.
Post Reply
User avatar
Gambit37
Should eat more pies
Posts: 13773
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

(done) Pain sounds for hitting monsters?

Post by Gambit37 »

In RTC you can specify a playlist of sounds that will be randomly played when you successfully hit a monster. It adds to the dynamism of the creatures when they make different noises :)

I can't see a property for this in the object definitions for monsters. Could this be added to DSB?

Also, related, when specifying values for an objects properties, can any value also be a function? For example, if I called a function on the attack_sound property, could the function generate a random sound and pass it back, something like this? (Note, this is my first attempt at Lua in DSB and may be all sorts of wrong :D)

Code: Select all

attack_sound=random_orc_attack_sound

Code: Select all

function random_orc_attack_sound()
		local sound = "_files\\_sfx\\foe\\orc\\orc-attack" .. math.random(3) ..".wav"
		local snd = dsb_get_sound("orc_attack",sound),
		return snd
	end
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: Pain sounds for hitting monsters?

Post by Sophia »

Gambit37 wrote:I can't see a property for this in the object definitions for monsters. Could this be added to DSB?
There are a few functions that can help you. on_damage(arch, id, dmg_amount, weapon_arch, damage_type) is executed any time a monster takes any sort of damage. Often, you only want to respond to melee hits, so there is also the on_take_melee_damage(arch, id, ppos, who, what, method_name, hit_power) event. Weapons also have the on_melee_damage_monster(weapon_arch, weapon_id, ppos, who, monster_id, hit_power) event. I don't think any of this is very well documented, as with many things in DSB, so it's probably not that hard to miss.

For example, here is some code to play whatever sound is contained in the pain_sound property. All you have to do to use it is make sure to set on_damage or on_take_melee_damage to monster_hit_sound. It delays the sound by 1 tick so it doesn't disappear into any weapon attack sound.

Code: Select all

function monster_hit_sound(arch, id)
   dsb_delay_func(1, function () local_sound(id, arch.pain_sound) end)
end
Gambit37 wrote:Also, related, when specifying values for an objects properties, can any value also be a function? For example, if I called a function on the attack_sound property, could the function generate a random sound and pass it back, something like this? (Note, this is my first attempt at Lua in DSB and may be all sorts of wrong :D)
I've tried to make many object properties able to be value-returning functions. This one wasn't set up to be... but will be in DSB 0.48, so you'll be able to do exactly that.

As to how to actually do this, you had the basic right idea, but there are two caveats. I'll take this chance to correct your code at the same time. :)
:arrow: First, never use math.random in DSB; always use dsb_rand.

:arrow: Second, you should load all of your sounds ahead of time. Then just do something like this:

Code: Select all

function random_orc_attack_sound(arch, id)
   return snd.orc_attack[dsb_rand(1, 4)]
end
Your load code (somewhere in your own custom graphics.lua) could look like:

Code: Select all

   snd.orc_attack = { } -- Declare it as a table
   for num=1,4 do
      snd.orc_attack[num] = dsb_get_sound("ORCATTACK" .. num, "files/sfx/foe/orc/orc_attack" .. num .. ".wav")
   end
User avatar
Gambit37
Should eat more pies
Posts: 13773
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: (done) Pain sounds for hitting monsters?

Post by Gambit37 »

OK, cool, some code to copy :D NOt sure I fully understand all you wrote here, but will analyse and fiddle with it...

A few things:
For example, here is some code to play whatever sound is contained in the pain_sound property. All you have to do to use it is make sure to set on_damage or on_take_melee_damage to monster_hit_sound. It delays the sound by 1 tick so it doesn't disappear into any weapon attack sound.
1) I din't understand this at all. What is pain_sound? Where do I add it (in the code) and where do I add on_damage etc? In my creature object definition? A function? Somewhere else? (I need code rleated logic spelled out very literally for me to understand -- anything that makes assumptions will just make me ask more questions for clarification) ;-)

2) Why not use math.random?

3) I tried setting up a table of sounds using the loop you gave, but DSB complains that "Sound ORCATTACK1" doesn't exist then bombs out?
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: (done) Pain sounds for hitting monsters?

Post by Sophia »

Gambit37 wrote:What is pain_sound? Where do I add it (in the code) and where do I add on_damage etc? In my creature object definition? A function? Somewhere else?
Yes, in your monster's archetype definition.

The function I gave adds a pain_sound property that can now be part of a monster's archetype. For example, assuming you've copied the code snippet from above that defines a monster_hit_sound function and loaded a snd.mummy_pain_sound somewhere prior, you can do this:

Code: Select all

obj.mummy.on_damage = monster_hit_sound
obj.mummy.pain_sound = snd.mummy_pain_sound
This will make a mummy play snd.mummy_pain_sound any time it takes damage. To only play the sound when the mummy takes melee damage, change on_damage to on_take_melee_damage.
Gambit37 wrote:Why not use math.random?
I don't have control over the seed value. This can break things that need to be deterministic, and will cause terrible errors if I ever add the ability to save a replay like CSBwin has. (DSB speedruns?)
Gambit37 wrote:DSB complains that "Sound ORCATTACK1" doesn't exist then bombs out?
That code tries to load sound files called files/sfx/foe/orc/orc_attack1.wav through files/sfx/foe/orc/orc_attack4.wav. Do those files actually exist?
User avatar
Gambit37
Should eat more pies
Posts: 13773
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: (done) Pain sounds for hitting monsters?

Post by Gambit37 »

Sophia wrote:The function I gave adds a pain_sound property that can now be part of a monster's archetype.
Ah, right. See, I didn't get that at all ;-)

So since this is a global function that can now be used by all monster archs, which file should that go in? I'm thinking of doing each momsters code in it's own lua file (graphics & sounds declarations and all functions, etc, for neatness). Is that OK, or do I need to do things in certain files?

The rest makes sense now.
Sophia wrote:That code tries to load sound files called files/sfx/foe/orc/orc_attack1.wav through files/sfx/foe/orc/orc_attack4.wav.
I understood what it was doing and double checked for the existence of those files and they definitely exist...
EDIT: Ah, my bad! Files had hyphens, but my code had underscores :P
User avatar
Gambit37
Should eat more pies
Posts: 13773
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: (done) Pain sounds for hitting monsters?

Post by Gambit37 »

Hmm, OK, it loads the sounds now, but when I attack my Orc, I get this error:

Code: Select all

Lua Function do_timer_event: base/util.lua:255: dsb_3dsound requires Sound in param 1
This is my complete code in it's own orc.lua file that's called at the top of my objects.lus using dsb_import_arch().

Code: Select all

function monster_hit_sound(arch, id)
   dsb_delay_func(1, function () local_sound(id, arch.pain_sound) end)
end

snd.orc_pain = { } -- Declare it as a table
   for num=1,4 do
      snd.orc_pain[num] = dsb_get_sound("ORC_PAIN" .. num, "..\\..\\_files\\_sfx\\foe\\orc\\orc-hurt" .. num .. ".wav")
   end
   
function random_orc_pain_sound(arch, id)
   return snd.orc_pain[dsb_rand(1, 4)]
end

obj[ROOT_NAME .. "_leader"] = clone_arch(obj.trolin, {
    name="ORC LEADER",
	type="MONSTER",
	class="HUMANOID",
	front=gfx[ROOT_NAME .. "_front"],
	side=gfx[ROOT_NAME .. "_side"],
	back=gfx[ROOT_NAME .. "_back"],
	attack = gfx[ROOT_NAME .. "_attack"],
	on_damage = monster_hit_sound,
	pain_sound=random_orc_pain_sound,
	step_sound=dsb_get_sound("orc_step","..\\..\\_files\\_sfx\\foe\\orc\\orc-step.wav"),
	hp=200
} )
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: (done) Pain sounds for hitting monsters?

Post by Sophia »

EDIT: Disregard, see my next post.
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: (done) Pain sounds for hitting monsters?

Post by Sophia »

Actually, I got confused. I was still thinking of attack sounds, which are part of the base code. The new stuff in DSB 0.48 won't directly help you with this, but the good news is you can write your own code and make it work right now. If you want to play sounds from a table, you should probably just write a special orc function, rather than try to create a bunch of contrived infrastructure to support using value-returning function here. The base code does it to try to stay generic, but we don't really have to be generic in code that is specifically for your dungeon.

Here's what I had in mind:

Code: Select all

function orc_hit_sound(arch, id)
   dsb_delay_func(1, function () local_sound(id, snd.orc_pain[dsb_rand(1, 4)]) end)
end

obj[ROOT_NAME .. "_leader"] = clone_arch(obj.trolin, {
    name="ORC LEADER",
	type="MONSTER",
	class="HUMANOID",
	front=gfx[ROOT_NAME .. "_front"],
	side=gfx[ROOT_NAME .. "_side"],
	back=gfx[ROOT_NAME .. "_back"],
	attack = gfx[ROOT_NAME .. "_attack"],
	on_damage = orc_hit_sound,
	step_sound=dsb_get_sound("orc_step","..\\..\\_files\\_sfx\\foe\\orc\\orc-step.wav"),
	hp=200
} )
Note that now orc_hit_sound is directly assigned to on_damage and the orc pain sound is coded right in there. That makes it not as generic as the previous approach, but it is cleaner for this one specific arch. The more generic approach obviously works fine if you have only one sound you need to play.

By the way, forward slashes work fine when specifying paths in DSB.
User avatar
Gambit37
Should eat more pies
Posts: 13773
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: (done) Pain sounds for hitting monsters?

Post by Gambit37 »

Cool, thanks, the sound fx work now, but I get this error when the orc is killed. I guess it's because the on_damage sound code is trying to play a sound from a creature that no longer exists?

Code: Select all

Lua function do_timer_event: base/util.lua:244: Invalid instance 244.
BTW, I know I'm running before I can walk here since I still haven't got to grips with the basics of the editor. I just wanted to see how easy it would be to create new monsters and the possibilities for customisation :)
By the way, forward slashes work fine when specifying paths in DSB.
Ah, great! Makes things a bit simpler :)
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: (done) Pain sounds for hitting monsters?

Post by Sophia »

Gambit37 wrote:Cool, thanks, the sound fx work now, but I get this error when the orc is killed. I guess it's because the on_damage sound code is trying to play a sound from a creature that no longer exists?
Yes, oops. I didn't think about this. We can't use local_sound, we'll have to emulate what it does. Fortunately, it's still pretty simple. We'll just tell DSB exactly where to play the sound, rather than associate it with an instance that won't be there any more:

Code: Select all

function orc_hit_sound(arch, id)
   local lev, x, y = dsb_get_coords(id)
   dsb_delay_func(1, function() dsb_3dsound(snd.orc_pain[dsb_rand(1, 4)], lev, x, y) end)
end
User avatar
Gambit37
Should eat more pies
Posts: 13773
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: (done) Pain sounds for hitting monsters?

Post by Gambit37 »

Cool, that works, thanks :-)

I'm beginning to get an understanding of how it all works now. I think I'm gonna enjoy this :-)
Post Reply