• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Search for an array of items

thedoc

Member
Joined
Jan 28, 2014
Messages
13
Reaction score
6
Hi, again! :p I would like to know how to make this search for an array of items instead of putting one search for each item.

the array:
local gold_CENTRALS_IDS = {14973, 14974, 14975, 14976, 14977, 14978}

the search:
local vault1_search1 = getTileItemById(vault_coord1, 2160).uid

also can I define an array of coordinates for the search? I want to search for the Item 14973 in the following coordinates:
local vault_coord1 = Position(959, 1001, 7}
local vault_coord2 = Position(958, 1001, 7}
local vault_coord3 = Position(957, 1001, 7}
local vault_coord4 = Position(959, 1002, 7}
local vault_coord5 = Position(958, 1002, 7}
local vault_coord6 = Position(957, 1002, 7}
How can I search for an array of items or an item and an array of coordinates for the search instead of using one line for each?

Thanks for the help!! :)
 
It's not 100% clear what you want, but let's start here:

Code:
itemPosArray = {
[14973] = { {959, 1001, 7}, {958, 1001, 7} },
[14974] = { {957, 1001, 7}, {959, 1002, 7} },
[14976] = { {958, 1002, 7}, {957, 1002, 7} }
}

for item, positionArray in pairs(itemPosArray ) do
    for indx, positionCoords in ipairs(positionArray ) do
         -- check if item is located at the position defined by positionCoords, and if so do something useful and exit the inner loop
    end
end
  • The outer loop steps through the item-keyed array
  • The inner loop steps through the positions
  • Given the choice I'd use Position objects, but I can't find Position:new() and I CBA checking if it's there or not
  • If each item can only be at one of the positions, there's a Lua instruction ("break") to exit the inner loop early, after you get a hit
 
Last edited:
@Summ Thanks for the help! My TFS version is 1.0
@Pteryx thanks!
Basically I am making a prototype for a vault in the city and when the player deposit funds there it will really add gold when every 100k is donated to the city funds . So I want the script to check if there are golds on the tiles, if they are the central parts, move on. if they are gold BORDERS, it will remove the borders and add a central part and on pos x - 1 (left side) it will add a new border. I will make the gold being added only in 1 direction for now because the vault is small so there is no need to make a complex script that checks all kinds of borders and fills the vault in all directions.

For you to understand better:

Filling central parts
wi2255.png

(IDS 14973;14974;14975;14976;14977;14978)

Border
jqs5jd.png

(14986)

Vault
2dlkcnt.png


Its a NPC.
 
local gold_borders_id = {14986}

local gold_centers_ids = {

[60002] = {14973, 14974, 14975, 14976, 14977, 14978}
}



-- vault coordinates

local vault_coords = {

[60001] = {{x = 959, y = 1001, z = 7}, {x = 958, y = 1001, z = 7}, {x = 957, y = 1001, z = 7}, {x = 959, y = 1002, z = 7}, {x = 958, y = 1002, z = 7},
{x = 957, y = 1002, z = 7}}

}

-- search for golds

local x = gold_borders_ids[item.uid]
local z = gold_centers_ids[item.uid]
local i = vault_coords[item.uid]
local coin = getTileItemById(i[1], z[1]).uid
local coin2 = getTileItemById(i[1], x[1]).uid

if i then
if itemId in pairs(z[1]) // if it is a central part, do nothing
return true
else if itemId in pairs(x[1]) // if it is a border > remove > add central part > move to x-1> create a border

Is this on the way? Or should I add a loop to use in pairs?
 
@Pteryx I tried your way

It will look for the border, if it finds I want it to remove it and create a random goldcenter, move x - 1 and create a new goldborder
---- EDIT
Thism might work?
--EDIT 2 can anyone set a loop for it keep checking for borders? I this is the only thing I am missing.
The holding funds are for it to add a new gold sqm every 100k donated.
vault = {}

vault.coords = {{x = 959, y = 1001, z = 7}, {x = 958, y = 1001, z = 7}, {x = 957, y = 1001, z = 7}, {x = 959, y = 1002, z = 7}, {x = 958, y = 1002, z = 7},
{x = 957, y = 1002, z = 7}}
vault.golds = {goldcenters = {14973, 14974, 14975, 14976, 14977, 14978}, goldborder = {14986}}
vault.funds = {totalfunds = 9898, holdingfunds = 9797}
hfunds = getGlobalStorageValue(vault.holdingfunds)



local x = vaul.goldborder[item.uid]
local z =vault.goldcenters[item.uid]
local i = vault.coords[item.uid]
local searchborder = getTileItemById(i[], x[]).uid

if (searchborder ~= 0) and (hfunds >= 100000) then
doRemoveItem(getTileItemById(i[], x[])).uid,1)
doCreateItem(z[], 1)
doCreateItem(x[], 1, {x - 1})
setGlobalStorageValue(vault.holdingfunds, -100000)
end
 
Last edited:
Thanks for the explanation. Just to be sure, here's my current interpretation:

  • You've made a 3 x 2 room with floor tiles at these positions:
    • {x = 959, y = 1001, z = 7}, {x = 958, y = 1001, z = 7}, {x = 957, y = 1001, z = 7}, {x = 959, y = 1002, z = 7}, {x = 958, y = 1002, z = 7},{x = 957, y = 1002, z = 7}
  • It will be used as a vault, and it will be filled from right to left.
  • You've chosen tiles to represent piles of gold. The gold tiles are:
    • {14973, 14974, 14975, 14976, 14977, 14978}
  • The vault won't always be full of gold, so you distinguish between tiles with gold, and "edge tiles" without gold.
  • Since the vault is filled from R to L, you only need one edge tile, which is tile
    • 14986
  • Vault graphics will be updated for each increment to the total that takes it over a "100K boundary" - e.g. at 100K, 200K, 300K, etc. After 600K, no further graphic updates will be made (but see below - we can do better than that :)
  • Initial graphics state is two edge tiles at {x = 957, y = 1001, z = 7} and {x = 957, y = 1002, z = 7}, representing the range (0, 99,999)
I've probably left something out, but that's why even coders (who tend to hate documentation) are prepared to make written specs :)
Change or remove what's wrong, add what's missing.


A. At this point there are some obvious questions due to the graphics. BTW - feel free to choose the simplest option if you like - in part this is a lesson in specification as well as (hopefully) basic coding :)

1.
Will you always add two gold tiles at a time, so the edge is always two identical tiles side by side in the Y-axis? Your desription says you'll add one fold tile at a time, but that will produce an potentially ugly graphic effect.
Imagine tiles arrayed like this:
edge, gold, gold
edge, edge, gold​
The second column won't look good - but it will still be clear where the gold is stored.

If you're making your own tile graphics, or if there are more available already, you could make more edge tiles: for example in the case above, two "diagonal edge" tiles at (1,1) and (2,2), and a plain floor tile at (2,1) would look better.

2.
Do you want to select gold tiles at random so you have some variation., or do you want them to represent more/less gold?
You could have e.g. three values: plain (lowest, 2 tiles); jewels (medium, two tiles); chest/sword (highest, two tiles)

B. And there's a logic question: do you want the vault to become bigger later (e.g. what if it was 4x4?), and if so, do you have the other tiles available already?
For this you'd need a full set of edge tiles (8 in total) The code for this would be different of course, but:
  • It's always better to consider requirements for the next couple of steps before starting to code
  • Don't be scared to talk about medium-term objectives - it doesn't mean you have build the final solution in one huge step
  • Don't aim too high for the first cut - but don't try to steer the early design by hiding information from your coder. When a good coder realizes you've done this they will transfer off the project ...
  • ... and there's a flip side: if your coder doesn't know how to make a simple, functioning, testable skeleton to start with, fire them :)
 
Back
Top