[solved] This is very frustrating!

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
Ryeath_Greystalk
Posts: 366
Joined: Tue Jan 15, 2013 3:26 am
Location: Oregon

[solved] This is very frustrating!

Post by Ryeath_Greystalk »

I am begining to think there may be some serious flaws in either the editor, or Lua or Grimrock, or my computer.

I am trying to make sure the weight in 4 alcoves is equal. I went back to spaghetti code because I know I can do it that way and haven't figured out this crazy LUA language. And this should work.

Code: Select all

-- check if all alcove total weights are equal
-- first the talley

local AW1 = 0
local AW2 = 0
local AW3 = 0
local AW4 = 0
local x = 0

	if isPunished == false then
	x = 0
		for i in ScaleAlcove1:containedItems() do
			x = i:getWeight()
			AW1 = AW1 + x	
		end
		hudPrint(""..AW1)
	x = 0
		for i in ScaleAlcove2:containedItems() do
			x = i:getWeight()
			AW2 = AW2 + x	
		end
		hudPrint(""..AW2)
	x = 0
		for i in ScaleAlcove3:containedItems() do
			x = i:getWeight()
			AW3 = AW3 + x	
		end
		hudPrint(""..AW3)
	x = 0
		for i in ScaleAlcove4:containedItems() do
			x = i:getWeight()
			AW4 = AW4 + x	
		end
		hudPrint(""..AW4)
	end
		

-- now the comparison
	if isPunished == false then
		if AW1 ~= AW2 then
			hudPrint(""..AW1.." and "..AW2.." are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
		end
	end
	if isPunished == false then
		if AW1 ~= AW3 then
			hudPrint("AW1 and AW3 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
	--[[elseif AW1 ~= AW4 then
			hudPrint("AW1 and AW4 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
		elseif AW2 ~= AW3 then
			hudPrint("AW2 and AW3 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
		elseif AW2 ~= AW4 then
			hudPrint("AW2 and AW4 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
		elseif AW3 ~= AW4 then
			hudPrint("AW3 and AW4 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true]]
			
		end
	end

-- and finally success

	if isPunished == false then
		hudPrint("All things are equal.")
		hudPrint("You may pass!")
		ScaleDoor1:open()
	end
end
First I get the weights from each alcove and add them up. Working fine through multipe tests.

Then I also print them out to confirm the are correct. Fine.

Then I compare to see if they are not equal, and hudPrint the results

The results show "9.5 and 9.5 are not equal!" In what world is 9.5 and 9.5 not equal? (btw not the real number in case anyone play my mod)
and the kicker is, without doing anything different it worked...once.

This is getting so frustrating.
Ok, rant off now.
Last edited by Ryeath_Greystalk on Sun Feb 03, 2013 5:54 am, edited 1 time in total.
User avatar
crisman
Posts: 305
Joined: Sat Sep 22, 2012 9:23 pm
Location: Italy

Re: This is very frustrating!

Post by crisman »

I'm trying your script, but it's working fine. I'm ignoring the code inside the square parenthesis. I place 3 items with the same weight in the first 3 alcoves and the door opens.
Maybe there's something odd with some items? Which items are you using??

PS you don't have to do all those controls, you just need 4 controls: AW1 with AW2, AW1 with AW3, AW1 with AW4. ;)
Ryeath_Greystalk
Posts: 366
Joined: Tue Jan 15, 2013 3:26 am
Location: Oregon

Re: This is very frustrating!

Post by Ryeath_Greystalk »

Thanks for taking the time to do that crisman.

I'm using assett rocks. I made 12 of them each one with a different weight so the puzle is to find the magic weight that they all equal out too.

And all the weights are coming out right. Not sure why they are not showing equal when the hudPrint cleary shows both variables are the same. Or why it worked once.

Now that I know it is working on your system I will keep plugging away at it.

And thanks for the heads up about not needing all the checks. I did it similiar to your suggestion first but when it didn't work I added the other stuff to try and troubleshoot it.

If you are interested the whole code is

Code: Select all

--Must have three weightstones in each of 4 alcoves and combined
--weights must match.

function allThingsEqual()
local isPunished = false

-- check for three items
	if ScaleAlcove1:getItemCount() ~= 3 then
		hudPrint("There are an incorrect number of items in Alcove 1")
		hudPrint("You have been punished!")
		isPunished = true
	end
	
	if isPunished == false then
		if ScaleAlcove2:getItemCount() ~= 3 then
			hudPrint("There are and incorrect number of items in Alcove 2")
			hudPrint("You have been punished!")
			isPunished = true
		end
	end


	if isPunished == false then
		if ScaleAlcove3:getItemCount() ~= 3 then
			hudPrint("There are and incorrect number of items in Alcove 3")
			hudPrint("You have been punished!")
			isPunished = true
		end
	end
	
	if isPunished == false then
		if ScaleAlcove4:getItemCount() ~= 3 then
			hudPrint("There are and incorrect number of items in Alcove 4")
			hudPrint("You have been punished!")
			isPunished = true
		end
	end

--Check that all items are weightstones
	
	if isPunished == false then
		for i in ScaleAlcove1:containedItems() do
			if i.name:sub(1,11) ~= "weightstone" then
				hudPrint("There is an incorrect item in Alcove 1")
				hudPrint("You have been punished!")
				isPunished = true
			end
		end
	end
	
	if isPunished == false then
		for i in ScaleAlcove2:containedItems() do
			if i.name:sub(1,11) ~= "weightstone" then
				hudPrint("There is an incorrect item in Alcove 2")
				hudPrint("You have been punished!")
				isPunished = true
			end
		end
	end
	
	if isPunished == false then
		for i in ScaleAlcove3:containedItems() do
			if i.name:sub(1,11) ~= "weightstone" then
				hudPrint("There is an incorrect item in Alcove 3")
				hudPrint("You have been punished!")
				isPunished = true
			end
		end
	end
	
	if isPunished == false then
		for i in ScaleAlcove4:containedItems() do
			if i.name:sub(1,11) ~= "weightstone" then
				hudPrint("There is an incorrect item in Alcove 4")
				hudPrint("You have been punished!")
				isPunished = true
			end
		end
	end

-- check if all alcove total weights are equal
-- first the talley

local AW1 = 0
local AW2 = 0
local AW3 = 0
local AW4 = 0
local x = 0

	if isPunished == false then
	x = 0
		for i in ScaleAlcove1:containedItems() do
			x = i:getWeight()
			AW1 = AW1 + x	
		end
		hudPrint(""..AW1)
	x = 0
		for i in ScaleAlcove2:containedItems() do
			x = i:getWeight()
			AW2 = AW2 + x	
		end
		hudPrint(""..AW2)
	x = 0
		for i in ScaleAlcove3:containedItems() do
			x = i:getWeight()
			AW3 = AW3 + x	
		end
		hudPrint(""..AW3)
	x = 0
		for i in ScaleAlcove4:containedItems() do
			x = i:getWeight()
			AW4 = AW4 + x	
		end
		hudPrint(""..AW4)
	end
		

-- now the comparison
	if isPunished == false then
		if AW1 ~= AW2 then
			hudPrint(""..AW1.." and "..AW2.." are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
		end
	end
	if isPunished == false then
		if AW1 ~= AW3 then
			hudPrint("AW1 and AW3 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
	--[[elseif AW1 ~= AW4 then
			hudPrint("AW1 and AW4 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
		elseif AW2 ~= AW3 then
			hudPrint("AW2 and AW3 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
		elseif AW2 ~= AW4 then
			hudPrint("AW2 and AW4 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true
		elseif AW3 ~= AW4 then
			hudPrint("AW3 and AW4 are not equal.")
			--hudPrint("You have been punished.")
			isPunished = true]]
			
		end
	end

-- and finally success

	if isPunished == false then
		hudPrint("All things are equal.")
		hudPrint("You may pass!")
		ScaleDoor1:open()
	end
end
		
	
and the custom assetts are

cloneObject{
name = "weightstone1",
baseObject = "rock",
uiname = "Weight Stone 1",
weight = 1.9,
}


cloneObject{
name = "weightstone2",
baseObject = "rock",
uiname = "Weight Stone 2",
weight = 2.4,
}

cloneObject{
name = "weightstone3",
baseObject = "rock",
uiname = "Weight Stone 3",
weight = 2.6,
}

cloneObject{
name = "weightstone4",
baseObject = "rock",
uiname = "Weight Stone 4",
weight = 2.7,
}

cloneObject{
name = "weightstone5",
baseObject = "rock",
uiname = "Weight Stone 5",
weight = 3.1,
}

cloneObject{
name = "weightstone6",
baseObject = "rock",
uiname = "Weight Stone 6",
weight = 3.3,
}

cloneObject{
name = "weightstone7",
baseObject = "rock",
uiname = "Weight Stone 7",
weight = 3.4,
}

cloneObject{
name = "weightstone8",
baseObject = "rock",
uiname = "Weight Stone 8",
weight = 3.7,
}

cloneObject{
name = "weightstone9",
baseObject = "rock",
uiname = "Weight Stone 9",
weight = 4.3,
}

cloneObject{
name = "weightstone10",
baseObject = "rock",
uiname = "Weight Stone 10",
weight = 4.5,
}

cloneObject{
name = "weightstone11",
baseObject = "rock",
uiname = "Weight Stone 11",
weight = 5.1,
}

cloneObject{
name = "weightstone12",
baseObject = "rock",
uiname = "Weight Stone 12",
weight = 5.4,
}
Ryeath_Greystalk
Posts: 366
Joined: Tue Jan 15, 2013 3:26 am
Location: Oregon

Re: This is very frustrating!

Post by Ryeath_Greystalk »

I did some more testing and found out that it is one group of stones that are the offenders. Stones 3,8 & 9 make up one group of equal weight. All though the weight adds up to the correct amount and the alcove weights show that, no matter what alcove they are in it is one doesn't equal.

All I can think off at this point is to change the weights and adjust stones according and resave the items file.
Ryeath_Greystalk
Posts: 366
Joined: Tue Jan 15, 2013 3:26 am
Location: Oregon

Re: This is very frustrating!

Post by Ryeath_Greystalk »

Ok, changed the weight of the three offending stones and resaved the assets file and it is working now.

Good call on the item causing the problem crisman. Still doesn't explain why the numbers displayed correctly though, but at this point I'm happy.

Now I can finally move on with my dungeon.
User avatar
crisman
Posts: 305
Joined: Sat Sep 22, 2012 9:23 pm
Location: Italy

Re: [solved] This is very frustrating!

Post by crisman »

Surely that's weird... but at least you solved it, and that's the important :D
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: [solved] This is very frustrating!

Post by Xanathar »

This problem is common to every programming language, and to some extension even to Excel and old old versions of the windows calculator :)

The numbers in Lua are all binary floating point - which basically means they are binary numbers with a "decimal" (*) point and some huge (but limited) number of digits , and, depending on the values, some numbers do not have a representation with a limited number of digits (we call those numbers "periodic" as they have a infinitly repeating pattern of digits after the decimal point).

The same happens with "our" decimal numbers. If you want to write 3 * (1/3) and you only use 3 significant digits, you get 3 * 0.333 which is 0.999 which is different from the correct result (=1).

The point is that many more numbers are periodic when written in binary - for example 0.1 in binary has an infinite numbers of digits. However note that when the numbers are printed they are often rounded to have a better readability: so you write 0.1 you read 0.1 but it really is 0.999999999998769 or 0.10000000000000000012 or whatever the value is.

So a golden rule in every programming language is : "when comparing two floating point numbers, never check that they are equal, but check that their difference is below a given threshold". (**)


For example:

Wrong:
if (a == b) then ...

Right:
if (math.abs(a - b) < 0.00001) then ...


(*) = I guess "decimal" point is misleading as they are binary numbers, but hey who cares.

(**) = The only exception is if those two numbers are integers and they come from only integer operations (for example, you can safely compare the count of items in two sacks using ==, since counting is integer in nature).
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org
Ryeath_Greystalk
Posts: 366
Joined: Tue Jan 15, 2013 3:26 am
Location: Oregon

Re: [solved] This is very frustrating!

Post by Ryeath_Greystalk »

Thanks Xanathar.

Looking at my original list of stones the only decimal point difference was the stone with the weight 2.6kg. it was the only one on my list with a .6. I bet that was the offender.
Post Reply