How to check for different coins in alcove (shop idea)

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
fazzasx
Posts: 104
Joined: Mon May 27, 2013 12:35 am

How to check for different coins in alcove (shop idea)

Post by fazzasx »

Hi Everyone,

I am in the midsts of making a dungeon to post here and I want to make a shop that uses 1 alcove, a pressure plate and has dialogue with a modified monster.

So far I have got most of this working and the first item is automatically delivered when the correct amount of gold is deposited into the alove. However if I put more gold in and script for this using an if statement it just does not work :(

Can someone please advise? I am quite new to this scripting LUA so please forgive this script. Also note that I did use parts from around the forum to get it working how I wanted it. Its great so far but I need to check for lots options, i.e. if I put 5 gold I get a dagger, if I put 6 gold I get a shield. Here is the code. Please if anyone can help I would be so happy :?

Code: Select all

    -- to add some dialogue :)
    traded = false
    function Merchant1()
       if traded == false then
          hudPrint("Greetings!")
          hudPrint("I am a trader here.")
          hudPrint("please place your item into the alove to receive your item!")
       else
          hudPrint("At the moment I have nothing to offer")
       end
    end



    function checkAlcoveForItems()
       -- Offer
       local itemName = "dm_coin_gor_gold"
              -- offer alcove
       local alcoveID = alcove_offer1
       -- checks if the offer alcove has an item
       if alcove_offer1:getItemCount() == 0 then
          hudPrint("Trade not worked?")
       end

     
       -- checks if the offer is equal to the item you want
       for i in alcoveID:containedItems() do
          if i.name == itemName  then
          local amount = i:getStackSize()

	      if (amount == 5) then
               hudPrint("Thou art a fine scholar!")
			   for i in alcove_offer1:containedItems() do
               if i.name == "dm_coin_gor_gold" then
               i:destroy()

               -- spawns the item which you will get
               alcove_offer1:addItem(spawn("dagger"))
			   end

	      if (amount == 6) then
               hudPrint("Thou art a fine scholar!")
			   for i in alcove_offer1:containedItems() do
               if i.name == "dm_coin_gor_gold" then
               i:destroy()

               -- spawns the item which you will get
               alcove_offer1:addItem(spawn("round_shield"))
               end
         -- else
         --      hudPrint("This has not worked!")
          end
          end
       end
    end
end
end
end
end
end
User avatar
msyblade
Posts: 792
Joined: Fri Oct 12, 2012 4:40 am
Location: New Mexico, USA
Contact:

Re: How to check for different coins in alcove (shop idea)

Post by msyblade »

You want to PM drakkan on these forums, he ran into this problem and solved it effectively :).
Currently conspiring with many modders on the "Legends of the Northern Realms"project.

"You have been captured by a psychopathic diety who needs a new plaything to torture."
Hotel Hades
User avatar
remma
Posts: 36
Joined: Mon Apr 15, 2013 11:03 pm
Location: Newcastle upon Tyne, England

Re: How to check for different coins in alcove (shop idea)

Post by remma »

I'm not sure but could be worth a try reversing the order you check in, so check for 7 coins first, if that hits then spawn the item, then 6 then 5 etc.
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: How to check for different coins in alcove (shop idea)

Post by JohnWordsworth »

I've not looked at it in full detail, but your code nesting is a bit messed up. Every if/for statement potentially starts a block of code (assuming the if is true, and the for has more than one loop) and each if/for block should be terminated with the 'end' word.

That big stack of 'end' statements at the end of the script is a sign that you've actually missed some 'end' statements out of the code (there will often be some at the end of a script, like 3 or 4 end statements, but rarely 9 as you have!).

Note, the following part of your code

Code: Select all

         if (amount == 5) then
               hudPrint("Thou art a fine scholar!")
            for i in alcove_offer1:containedItems() do
               if i.name == "dm_coin_gor_gold" then
               i:destroy()

               -- spawns the item which you will get
               alcove_offer1:addItem(spawn("dagger"))
            end

        if (amount == 6) then
           ..
You have 3 'if' statements in the first test against the amount of coins, but only one end. This means that the next if statement is actually running INSIDE of the above code. You can fix this by matching and end statement for each statement that causes branching into a code block. I'll reformat your code a little so it's easier to spot - what most coders do is 'tab' in every time a new block/branch starts, and then tab out everytime they put an end in. If you finish a big if block and the last end doesn't line up with the original if statement, there's almost definitely something wrong.

Fixing this bit of code specifically...

Code: Select all

if (amount == 5) then    -- Tab in one, as this is branch A
  hudPrint("Thou art a fine scholar!")
  
  for i in alcove_offer1:containedItems() do     -- Tab in again for block B
    if i.name == "dm_coin_gor_gold" then         -- Another if statement we have to end (block C).
      i:destroy()
    end    -- This marks the end of the specific code we want to run if we are looking at a coin in the alcove

    -- spawns the item which you will get
    alcove_offer1:addItem(spawn("dagger"))
  end   -- This is the end of the block of code we want to repeat on every item in the for loop (in the alcove).
end -- This ends branch A, which is the block of code we want to run if there are 5 coins in the alcove.

if ( amount == 6) then    -- Start the next check
  ...
end
I've added loads of heavy comments to explain what I'm trying to get across, let me know if this helps.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
fazzasx
Posts: 104
Joined: Mon May 27, 2013 12:35 am

Re: How to check for different coins in alcove (shop idea)

Post by fazzasx »

Thanks to everyone that replied here.

I have modifed the script and this appears to work fine :)

I will indent the script as advised but please let me know if this is better and feel free to use this or improve on it!

Its basically a merchant script that uses 1 alcove where you place your gold. You select a monster to use (so far a non aggressive goromorg) then link it all up to timers and pressure plates to get the desired effect of a shop etc.

Code: Select all

function spawnMerchant1()
   spawn("npcGoromorg", party.level,11,15,2,"npcGoromorg_1")
   :setLevel(1)
   :setAIState("guard")
end

function destroyMerchant1()
npcGoromorg_1:destroy()
end

    -- to add some dialogue :)
    traded = false
    function Merchant1()
       if traded == false then
          hudPrint("Greetings!")
          hudPrint("Thou art a trader in these parts.")
          hudPrint("If thou hast seen thy scroll let thee place thy gold to trade?")
       else
          hudPrint("At the moment I have nothing to offer")
       end
    end

    function checkAlcoveForItems()
       -- Offer
       local itemName = "dm_coin_gor_gold"
	 --  
       -- offer alcove
       local alcoveID = alcove_offer1
       -- checks if the offer alcove has an item
       if alcove_offer1:getItemCount() == 0 then
          hudPrint("Wouldest thou art rip me off?")
       end

     
       -- checks if the offer is equal to the item you want#
	for i in alcoveID:containedItems() do
		local amount = i:getStackSize()

 	  if i.name == itemName and (amount == 1) then
 	 	 hudPrint("Thou art a fine scholar!")
  	 	 i:destroy()
      	 alcove_offer1:addItem(spawn("dagger"))

  elseif i.name == itemName and (amount == 2) then
  	 	 hudPrint("Thou art a fine scholar!")
  	     i:destroy()
       	 alcove_offer1:addItem(spawn("round_shield"))

  elseif i.name == itemName and (amount == 3) then
  	 	 hudPrint("Thou art a fine scholar!")
  	     i:destroy()
       	 alcove_offer1:addItem(spawn("long_sword"))

  		end   -- This is the end of the block of code we want to repeat on every item in the for loop (in the alcove).
	end
end
Post Reply