Compare commits
5 Commits
7137f46303
...
59ff757c80
Author | SHA1 | Date |
---|---|---|
Cy | 59ff757c80 | |
Cy | 5878a255c3 | |
Cy | 0dfb76dbbe | |
Cy | bef8a1af5f | |
Cy | 7f7f259e7b |
57
init.lua
57
init.lua
|
@ -10,6 +10,12 @@ function min(a,b)
|
|||
end
|
||||
end
|
||||
|
||||
if not string.starts then
|
||||
function string.starts(String,Start)
|
||||
return string.sub(String,1,string.len(Start))==Start
|
||||
end
|
||||
end
|
||||
|
||||
--------------------------------------------------------
|
||||
|
||||
function sortInventory(inv,sorter)
|
||||
|
@ -28,6 +34,8 @@ end
|
|||
|
||||
sorters = {
|
||||
wise = function(a,b)
|
||||
-- XXX: this needs to have stricter ordering!
|
||||
-- (why is air scoring higher than the end?)
|
||||
if(a == nil) then
|
||||
if b == nil then
|
||||
return true
|
||||
|
@ -66,14 +74,17 @@ sorters = {
|
|||
local acount = a:get_count()
|
||||
local bcount = b:get_count()
|
||||
if(acount == bothmax) then
|
||||
return bcount == bothmax
|
||||
--print('same?',acount,bcount,bothmax)
|
||||
return bcount ~= bothmax
|
||||
elseif (bcount == bothmax) then
|
||||
--print('bcount bothmax derp')
|
||||
return false
|
||||
end
|
||||
local num = min(bcount,bothmax-acount)
|
||||
a:add_item(b:take_item(num))
|
||||
-- nothing can have both count AND wear, right?
|
||||
-- now a:count > b:count so a should go first
|
||||
--print('numnum',num)
|
||||
return true
|
||||
end,
|
||||
amount = function(a,b)
|
||||
|
@ -125,8 +136,14 @@ function registerWand(method,sorter)
|
|||
return
|
||||
end
|
||||
-- Sokomine's shared chest locks
|
||||
if locks ~= nil and not locks:lock_allow_use(pos,user) then
|
||||
minetest.chat_send_player(user:get_player_name(),"That's not yours!","Sorter -!-")
|
||||
if locks ~= nil then
|
||||
if punched == nil or punched.name == nil then
|
||||
punched = minetest.get_node(pos)
|
||||
end
|
||||
if punched and punched.name:starts('locks:') and not locks:lock_allow_use(pos,user) then
|
||||
-- the error has already been reported (yay side effects)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if(sortInventory(inv,sorter)) then
|
||||
|
@ -136,8 +153,25 @@ function registerWand(method,sorter)
|
|||
})
|
||||
end
|
||||
|
||||
function debugSorter(a,b)
|
||||
result = sorters.wise(a,b)
|
||||
function derp(a)
|
||||
return a:get_name()..":"..a:get_count()..":"..a:get_wear()
|
||||
end
|
||||
if a then
|
||||
a = derp(a)
|
||||
end
|
||||
if b then
|
||||
b = derp(b)
|
||||
end
|
||||
if result then
|
||||
print('a goes first',a,b)
|
||||
else
|
||||
print('b goes first',a,b)
|
||||
end
|
||||
return result
|
||||
end
|
||||
function test()
|
||||
table.sort({nil,nil,nil},sorters.wise)
|
||||
function thingy(name,stack_max)
|
||||
return {
|
||||
get_name=function(self) return name end,
|
||||
|
@ -152,21 +186,6 @@ function test()
|
|||
}
|
||||
end
|
||||
tabl = {thingy('thing1',1),thingy('hting2',1),nil,thingy('thing1',2),thingy('thing1',4),thingy('thing1',10)}
|
||||
function sorter(a,b)
|
||||
result = sorters.wise(a,b)
|
||||
if a then
|
||||
a = a:derp()
|
||||
end
|
||||
if b then
|
||||
b = b:derp()
|
||||
end
|
||||
if result then
|
||||
print('a goes first',a,b)
|
||||
else
|
||||
print('b goes first',a,b)
|
||||
end
|
||||
return result
|
||||
end
|
||||
table.sort(tabl,sorter)
|
||||
for n,v in pairs(tabl) do
|
||||
print(n,v:get_name(),v:get_count())
|
||||
|
|
Loading…
Reference in New Issue