Page 1 of 2

Magic Dead Zone

Posted: Mon Oct 08, 2012 12:45 am
by Delak
:?:

Is their away to script a magic dead zone where no champion can cast a spell? Just a point in the right direction would be good so I can learn more.

thanks
Delak

Re: Magic Dead Zone

Posted: Mon Oct 08, 2012 1:01 am
by JohnWordsworth
I don't know exactly where you would place this, but I think you can override / hook into some of the Party actions by 'cloning' the Party object in a lua script that is imported in your mod's init.lia script.

A completely untested stab at this would be...

In party.lua (add import 'mod_assets/scripts/party.lua' to mod_assets/scripts/init.lua if it doesn't exist).
SpoilerShow

Code: Select all


cloneObject{
  name = "party",
  baseObject = "party",
  onCastSpell = function(self, champion, spellName)
    if party.level = 1 and party.x > 3 and party.x < 8 and party.y > 3 and party.y < 8 then
      hudPrint("Your magic fails to work here...");
      return false;
    end

    return true;
  end,
This manually checks to see if the party is on level one and in any spaces in the square (4,4) to (7,7). If so, it prints a message to the screen and returns false (so the spell is not cast).

You could obviously check for anything in that function - if they are carrying a certain item, if they are on a given floor, etc.

Re: Magic Dead Zone

Posted: Mon Oct 08, 2012 1:24 am
by Grimwold
Just tested John's method and it seems to work well... (with a couple of very minor corrections)

Code: Select all

cloneObject{
  name = "party",
  baseObject = "party",
  onCastSpell = function(self, champion, spellName)
    if party.level == 1 and party.x > 3 and party.x < 8 and party.y > 3 and party.y < 8 then
      hudPrint("Your magic fails to work here...");
      return false;
    end

  return true;
  end,
}

Re: Magic Dead Zone

Posted: Mon Oct 08, 2012 1:27 am
by JohnWordsworth
@Grimwold: Good spot on the equals. I bashed that out on my iPad, and the autocorrect plays havoc when typing out lua code!

Re: Magic Dead Zone

Posted: Mon Oct 08, 2012 2:29 am
by Delak
Thank you John and Grimwold. I will give this a shoot when I start to build out my first dungeon.

Re: Magic Dead Zone

Posted: Mon Oct 08, 2012 4:27 pm
by Wolfrug
Heya Delak,

I've actually done this myself, so you can just copy-paste my solution. It's based on two things: a lua script from the onCastSpell hook, and a custom object. Here're the entries for objects.lua:

Code: Select all

-- Add the onCastSpell hook and have it run a script called antimagic inside a Lua script entity called checkAntiMagic
cloneObject{
   name = "party",
   baseObject = "party",

   onCastSpell = function(self, spell)
	   local returnValue = checkAntiMagic.antimagic()
       return returnValue
	end,
}

-- Make a custom object that denotes the location of the anti-magic zone: you can use anything, but I used an invisible pressure plate

cloneObject{
	name = "antimagic_zone",
	baseObject = "pressure_plate_hidden",
}
After this, simply add a Lua script entity and name it checkAntiMagic (note the capitalization), and copy-paste in this code:

Code: Select all

function antimagic()
-- Set here the number of levels you want to check for the presence of antimagic_zone objects
local LevelAmount = 5

local returnValue = true
for l=1,LevelAmount do
	for i in allEntities(l) do
		if i.name == "antimagic_zone" then
      		if party.x == i.x and party.y == i.y and party.level == i.level then
				returnValue = false
				hudPrint ("Your spell fizzles! You are standing on an anti-magic zone!")
				break
			end
		end
	end
end
return returnValue
Now you can simply put down the antimagic_zone objects (which are invisible). If the party stands on any such object, all spells will fail to cast. Note that this does not prevent monsters from casting spells - I'm not entirely sure if they are considered to 'cast spells' or more precisely make attacks, so I haven't tried, but presumably something similar could be arranged for them!

Hope that helps!

Re: Magic Dead Zone

Posted: Wed Oct 10, 2012 1:26 pm
by Grimwold
I really liked your idea of antimagic_zone objects, Wolfrug, so I've been working on this a little myself... I thought checking several levels for antimagic zones and checking if the party is there was a bit much though, so I've written a script to use entitiesAt(), to check the parties grid location for an antimagic zone.

Code: Select all

function antimagic(caster,spell)
  for i in entitiesAt(party.level,party.x,party.y) do
    -- hudPrint(i.name .. " with id = " .. i.id)
    if i.name == "antimagic_zone"
    then
      hudPrint(caster:getName() .. "'s spell fizzles! You are standing on an anti-magic zone!")
      return false
    end
  end
  return true
end

Re: Magic Dead Zone

Posted: Wed Oct 10, 2012 3:06 pm
by Wolfrug
@Grimwold

Duh, yes. That's obviously a lot easier and faster. Use that solution instead!

Re: Magic Dead Zone

Posted: Mon Oct 29, 2012 2:33 am
by strangely
Very useful information. Thanks.

Are there other hooks on the party object that can be overriden like onCastSpell?
I want to create a sword that calls some custom lua code every time a player swings it at a monster.
I can't override onUseItem on a cloned object because that isn't called when the player has the sword in their hand and they swing it at a monster.

Re: Magic Dead Zone

Posted: Mon Oct 29, 2012 4:04 am
by nichg
You can use onAttack for that. It gives you the attacker and their weapon, which you could check against the name of the item you want to do something weird.