Counting items in an alcove

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Phitt
Posts: 442
Joined: Tue Aug 14, 2012 9:43 am

Counting items in an alcove

Post by Phitt »

If I wanted to check for a specific number of specific items in an alcove, how would I do that? I know how to check for specific items in an alcove, but what if there are two or more of the same item type?

To be more specific: I have three different items (item types) that can be placed in the alcove and each of them exists numerous times throughout the dungeon. I want the player to put exactly four of those items into the alcove (and no other items), in any combination. He can put in four times item 1, or three times item 2 and item 1 once or twice item 1 and twice item 3 or...you hopefully understand what I'm trying to do.

Any ideas how to script this?
Decayer
Posts: 65
Joined: Sat Oct 13, 2012 3:19 pm

Re: Counting items in an alcove

Post by Decayer »

Code: Select all

function check()
	local valid = {"blue_gem", "green_gem", "red_gem"}
	local counter = 0
	
	for i in alcove:containedItems() do
		local ok = false
		for j = 1,#valid do
			if i.name == valid[j] then
				counter = counter + 1
				ok = true
				break
			end
		end
		if not ok then
			counter = -1
			break
		end
	end
	
	--print(counter)
	
	if counter == 4 then
		teleporter_1:activate()
	else
		teleporter_1:deactivate()
	end
end
Set the alcove to activate always and to trigger on "any" event.
User avatar
Neikun
Posts: 2457
Joined: Thu Sep 13, 2012 1:06 pm
Location: New Brunswick, Canada
Contact:

Re: Counting items in an alcove

Post by Neikun »

Is that spam I see? o-o;

It was! It was spam I saw!
Last edited by Neikun on Sat Oct 20, 2012 1:19 pm, edited 1 time in total.
"I'm okay with being referred to as a goddess."
Community Model Request Thread
See what I'm working on right now: Neikun's Workshop
Lead Coordinator for Legends of the Northern Realms Project
  • Message me to join in!
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Counting items in an alcove

Post by Batty »

....
Last edited by Batty on Mon Jan 23, 2017 10:22 pm, edited 1 time in total.
User avatar
TheLastOrder
Posts: 104
Joined: Wed Oct 17, 2012 1:56 am

Re: Counting items in an alcove

Post by TheLastOrder »

Batty wrote:well, we have a good name for a custom monster — the haohao
xDDDDDD

Final boss with unlimited life...

p.d: I hate spammers!
User avatar
Phitt
Posts: 442
Joined: Tue Aug 14, 2012 9:43 am

Re: Counting items in an alcove

Post by Phitt »

Haha, now that the spam was deleted it sounds as if Neikun is a troll. :)

Thank you Decayer, it works like a charm!
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Counting items in an alcove

Post by Komag »

yeah, I wiped him and all posts out, probably messed up a few threads ;)
Finished Dungeons - complete mods to play
User avatar
TheLastOrder
Posts: 104
Joined: Wed Oct 17, 2012 1:56 am

Re: Counting items in an alcove

Post by TheLastOrder »

First of all sorry for posting that here, maybe this isn't the most suitable place...
I didn't find a better leak... as I used the forum search tool, the word "alcove" returned at least 744 results. xDDDDDDD

I'm trying to create a room with 4 alcoves, with 4 items inside that alcoves (one item per alcove).
There's only one door, and that door only remains open IF in each alcove there's AT LEAST ONE item, doesn't matter if there's 8 items per alcove, doesn't matter what kind of item there are...
BUT if any alcove is empty, then the door remains closed.

That's the code I'm using, and I know that something is wrong... MAYBE with the code item_name?

Code: Select all

alcove_1_had_item = false
alcove_2_had_item = false
alcove_3_had_item = false
alcove_4_had_item = false

function altarHasItem(altar,item_name,had_item)
   
   for i in altar:containedItems() do
      if i.name == item_name then
         if(had_item) then
            playSound("lever")
         end
         return true
      end
   end
   
   return false
   
end

function areItemsThere()

   local alcove_1_ok = altarHasItem(alcove_1, "item", alcove_1_had_i)
   local alcove_2_ok = altarHasItem(alcove_2, "item", alcove_2_had_i)
   local alcove_3_ok = altarHasItem(alcove_3, "item", alcove_3_had_i)
   local alcove_4_ok = altarHasItem(alcove_4, "item", alcove_4_had_i)
   
   if(alcove_1_ok) then 
      alcove_1_had_item = true 
   end
   if(alcove_2_ok) then 
      alcove_2_had_item = true 
   end
   if(alcove_3_ok) then 
      alcove_3_had_item = true 
   end
   if(alcove_4_ok) then 
      alcove_4_had_item = true 
   end

   if alcove_1_ok and alcove_2_ok and alcove_3_ok and alcove_4_ok then
      temple_alcove_door:open()
   else
      temple_alcove_door:close()
   end
end
Any help will be rewarded with the publication of a 5 levels dungeon (tested & detailed!!!) -coming soon-
:D
Thanks everybody!!!
User avatar
TheLastOrder
Posts: 104
Joined: Wed Oct 17, 2012 1:56 am

Re: Counting items in an alcove

Post by TheLastOrder »

Please, some caritative soul that helps me!!!! :D

I'm really having trouble with the code asking for placed items (look at my previous post).

I've tried everything I know about scripting but with no success...

What's the appropiate script to ask for "any" objetc in an alcove.

It's important to make mention that a counter will not work properly, as if I put two objects in an alcove BUT another alcove remains empty, the counter will work as if each alcove has two objects in them.

Thanks everybody (and again and again)!!!! Hehehe.
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: Counting items in an alcove

Post by Grimwold »

There is a way to do it with just counters, but you'd need to have 5 of them... 1 counter for each alcove initial value 1 which decrement when the alcove has an item inserted, and increment when the item is removed. Each of the 4 counters is linked to the 5th counter with decrement and increment events. the 5th counter has initial value 4 and when it reaches 0 the door opens.. if it deactivates then close the door.


To do the same puzzle with scripts all you need is to check alcove:getItemCount() ~= 0 for each one. e.g.

Code: Select all

function checkAlcoves()
  if alcove_1:getItemCount() ~= 0 and alcove_2:getItemCount() ~= 0 and alcove_3:getItemCount() ~= 0 and alcove_4:getItemCount() ~ 0 then
    temple_alcove_door:open()
  else
    temple_alcove_door:close()
  end
end
Post Reply