Destroying a cave-in with a pickaxe

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
The cube
Posts: 94
Joined: Tue Apr 23, 2013 6:09 pm
Location: Barren Desert

Destroying a cave-in with a pickaxe

Post by The cube »

Hi guys! So, i have started playing around in the editor, and i came up withthis idea. Immediatedly as i saw the pickaxe in-game i thought it had some function like the shovel does, but it doesn't. Is it even possible to make the pickaxe do one of these:

1. Allow it to damage a cave-in

2. By right-clicking it in front of a cave-in, trigger a script.

2 Can also be done by always triggering a script and just checking if the character is in front of + facing a cave-in.

Thanks guys.
User avatar
Jgwman
Posts: 144
Joined: Thu Jun 28, 2012 10:14 pm

Re: Destroying a cave-in with a pickaxe

Post by Jgwman »

Cool idea. Doesn't sound very hard. Maybe I'll write one this evening.
swampie
Posts: 9
Joined: Sun Nov 02, 2014 1:12 am

Re: Destroying a cave-in with a pickaxe

Post by swampie »

So I gave this a try and came up with this somewhat boilerplaty code (I crindged a little bit writing it) :D
SpoilerShow

Code: Select all

components = {
		{	
		class="MeleeAttack",
		onAttack = function(self) 				
			local facing = party.facing 
			local level = party.level
			local x = party.x
			local y = party.y
			local foundOne = false	
			local caveIns 
		--get entity in front of the party
			if facing == 0 then								
				for e in Dungeon.getMap(level):entitiesAt(x,y-1) do
					if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then									
						foundOne = true
						caveIns = e
					end
				end					
			elseif facing == 1 then				
				for e in Dungeon.getMap(level):entitiesAt(x+1,y) do
					if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then
						foundOne = true
						caveIns = e
					end
				end
			elseif facing ==2 then				
				for e in Dungeon.getMap(level):entitiesAt(x,y+1) do
					if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then
						foundOne = true
						caveIns = e
					end
				end
			elseif facing == 3 then				
				for e in Dungeon.getMap(level):entitiesAt(x-1,y) do
					if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then
						foundOne = true
						caveIns = e
					end
				end
			else
				return
			end				
			--if it did find one then destroy it
			if foundOne then					
				caveIns:destroy()
			end
		end
		},
	}
What I do is, I get the entity in front of the party, check if its go name is a cave_in, if so I destroy it.
You should probably add some fancy effects/sounds or a shovel-dig like fade in/out to smooth that out :D

So yeah, that's it from me.

//edit: the code is bound to the pickaxe clone/whatever item you wish
//edit2: took the below advice to heart and voilá, somewhat tidier :D
SpoilerShow

Code: Select all

components = {		
		{	
		class="MeleeAttack",
		onAttack = function(self) 				
			local facing = party.facing 
			local level = party.level
			local dx, dy = getForward(facing)
			local inFrontX = party.x + dx
			local inFrontY = party.y + dy			
			local foundOne = false	
			local caveIns 
		--get entity in front of the party										
			for e in Dungeon.getMap(level):entitiesAt(inFrontX,inFrontY) do
				if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then									
					 e:destroy()
				 end
			end
		end	
		},
	}
Last edited by swampie on Sun Nov 02, 2014 2:19 am, edited 2 times in total.
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Destroying a cave-in with a pickaxe

Post by JohnWordsworth »

@Swampie: A little tip. Instead of doing 4 different cases depending on your facing, you can just do this...

Code: Select all

local dx,dy = getForward(facing)
local inFrontX = party.x + dx
local inFrontY = party.y + dy

for e in Dungeon.getMap(level):entitiesAt(inFrontX, inFrontY) do
   ...
end
Then you don't have to duplicate the code 4 times (which is generally a bad idea, because it's very easy to change one of the 4 blocks and forget to update the rest!).
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
Prozail
Posts: 158
Joined: Mon Oct 27, 2014 3:36 pm

Re: Destroying a cave-in with a pickaxe

Post by Prozail »

MeleeAttackComponent has an "onAttack" hook??? Well, that simplifies things ALOT :)

Thank you!
swampie
Posts: 9
Joined: Sun Nov 02, 2014 1:12 am

Re: Destroying a cave-in with a pickaxe

Post by swampie »

@JohnWordsworth Yeah, that's what I meant with boilerplatey :D
But, wow, I did oversee those two functions... would have made that somewhat easier, thank you.
Post Reply