Script options to check if party has various items or not

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Conchron
Posts: 137
Joined: Mon Dec 31, 2012 1:20 am
Location: Sweden

Script options to check if party has various items or not

Post by Conchron »

I can't write a working script even if my life depend on it and I have to read through the forum looking for the bits I need. However, I can't find this.

I need to check the party for specific items, golden_crown, golden_dragon and golden_orb and open door1 if the party has all of them and door2 if they don't.

Can anyone help with this script?

Does it help if I tell you that I think you all are fantastic and you make amazing stuff? ;) I've been lurking around here for months and I still find things that make me go "wow!" :o every day.
Last edited by Conchron on Sat Feb 09, 2013 12:33 pm, edited 1 time in total.
User avatar
Filipsan
Posts: 84
Joined: Fri Mar 16, 2012 10:18 am

Re: Can anyone help me with a scrip?

Post by Filipsan »

Conchron wrote:
I need to check the party for specific items, golden_crown, golden_dragon and golden_orb and open door1 if the party has all of them and door2 if they don't.

Can anyone help with this script?
Hi there :)

Try this:

viewtopic.php?f=14&t=4903&p=52094&hilit ... eck#p52094

or maybe

viewtopic.php?f=14&t=3099&p=31872&hilit ... eck#p31872
User avatar
Conchron
Posts: 137
Joined: Mon Dec 31, 2012 1:20 am
Location: Sweden

Re: Can anyone help me with a scrip?

Post by Conchron »

Thank you Filipsan, but, I've seen those and I can't put them together so that they do what I need them to do. :oops:
User avatar
crisman
Posts: 305
Joined: Sat Sep 22, 2012 9:23 pm
Location: Italy

Re: Can anyone help me with a scrip?

Post by crisman »

You can try this, but I warn you, this kind of scripts needs grimq
viewtopic.php?f=14&t=4256&hilit=grimq
because the script I'm going to write will not work if the items are inside sacks, boxes, mortars, ecc...
A script with grimq will provide that kind of stuff, but unfortunately I'm not good using it :(
Anyway, here's the script, I've tested it and it's working :)

Code: Select all

function checkItems()
	
	local treasure1 = false
	local treasure2 = false
	local treasure3 = false
	
	local treasures = {
		"golden_crown", "golden_dragon", "golden_orb"}
		
	local checks = {
		treasure1, treasure2, treasure3}
	
	for i = 1, 4 do
		for j = 1, 31 do
			local item = party:getChampion(i):getItem(j)
			for k, v in ipairs(treasures) do
				if item then
					if item.name == v then
						checks[k] = true
					end
				end
			end
		end
	end
	
	for i, v in ipairs(checks) do
		if not v then
			door2:open()
			return
		end
	end
	
	door1:open()

end
EDIT:
Okay, I've managed to create a code that seek for items even inside sacks, boxes, etc... and seems working good.
Here's the new code.
SpoilerShow

Code: Select all

function checkItems()
	
	local treasure1 = false
	local treasure2 = false
	local treasure3 = false
	
	local treasures = {
		"golden_crown", "golden_dragon", "golden_orb"}
		
	local checks = {
		treasure1, treasure2, treasure3}
	
	for i = 1, 4 do
		for j = 1, 31 do
			local item = party:getChampion(i):getItem(j)
			for k, v in ipairs(treasures) do
				if item then
					if item.name == "sack" or item.name == "mortar" or item.name == "wooden_box" then
						for x in item:containedItems() do
							if x then
								if x.name == "mortar" then
									for y in x:containedItems() do
										if y then
											if y.name == v then
												checks[k] = true
											end
										end
									end
								end
							end
							if x.name == v then
								checks[k] = true
							end
						end
					end
					if item.name == v then
						checks[k] = true
					end
				end
			end
		end
	end
	
	for i, v in ipairs(checks) do
		if not v then
			door2:open()
			return
		end
	end
	
	door1:open()

end
Let me know if you have problems :)
User avatar
Komag
Posts: 3654
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Can anyone help me with a scrip?

Post by Komag »

my head just pirouetted across the office!
Finished Dungeons - complete mods to play
User avatar
Conchron
Posts: 137
Joined: Mon Dec 31, 2012 1:20 am
Location: Sweden

Re: Can anyone help me with a scrip?

Post by Conchron »

Thank you crisman! That works perfectly. :D
User avatar
Komag
Posts: 3654
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Can anyone help me with a scrip? (Solved)

Post by Komag »

minor correction to crisman's very good code:

Code: Select all

function checkItems()
   
   local treasure1 = false
   local treasure2 = false
   local treasure3 = false
   
   local treasures = {
      "golden_crown", "golden_dragon", "golden_orb"}
      
   local checks = {
      treasure1, treasure2, treasure3}
   
   for i = 1, 4 do
      for j = 1, 31 do
         local item = party:getChampion(i):getItem(j)
         for k, v in ipairs(treasures) do
            if item then
               if item.name == "sack" or item.name == "mortar" or item.name == "wooden_box" then
                  for x in item:containedItems() do
                     if x then
                        if x.name == "mortar" then
                           for y in x:containedItems() do
                              if y then
                                 if y.name == v then
                                    checks[k] = true
                                 end
                              end
                           end
                        end
                        if x.name == v then
                           checks[k] = true
                        end
                     end
                  end
               end
               if item.name == v then
                  checks[k] = true
               end
            end
         end
      end
   end
   
   for i, v in ipairs(checks) do
      if not v then
         door2:open()
         return
      end
   end
   
   door1:open()

end
I just moved an "end" from right before the "if x.name" section to right after it (and adjusted the spacing accordingly), so the "if x" (existence) check above that is still in place for the x.name check
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3654
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Can anyone help me with a scrip? (Solved)

Post by Komag »

And here's my (squished!) version for a single item:

Code: Select all

function checkGoldOrb()
  local goldOrb = false
  local n = "golden_orb"

  for i = 1, 4 do
    for j = 1, 31 do
      local item = party:getChampion(i):getItem(j)
      if item then
         if item.name == "sack" or 
            item.name == "mortar" or 
            item.name == "wooden_box" then
            for x in item:containedItems() do
              if x then
                 if x.name == "mortar" then
                    for y in x:containedItems() do
                      if y then if y.name == n then goldOrb = true end end
                 end end
                 if x.name == n then goldOrb = true end
         end end end
         if item.name == n then goldOrb = true end
  end end end

  if goldOrb then door1:open() else door2:open() end
end
Finished Dungeons - complete mods to play
User avatar
crisman
Posts: 305
Joined: Sat Sep 22, 2012 9:23 pm
Location: Italy

Re: Script options to check if party has various items or no

Post by crisman »

Thanks Komag, didn't notice that ;)
Its' funny that 5 minutes before writing the code I didn't remember that item:contaniedItems() was added to the editor :mrgreen:
Surely for an item is way much more simple, like you did!
PoonMoon
Posts: 1
Joined: Mon Mar 11, 2013 4:03 pm

Re: Script options to check if party has various items or no

Post by PoonMoon »

Hi everyone,

Just an additional question - is it possible to check for the 'hand' i.e. if you take something from a champs inventory or equipped hands, the cursor (in game) changes to that object. This script does not detect / check that 'hand' - which is scuppering my plans....

Great forum btw - excellent resource, thanks to all for contributing.
Post Reply