outfit item map

Okay, so I've been trying to learn more about maps and I'm trying to make my first multi-dimensional map! I was going with an outfit item map and I've got this:

Code:
string[item, item, item] outfititems;
outfititems[$item[bugbear beanie], $item[bugbear bungguard], $item[none]] = "Bugbear Costume";

foreach key1, key2, key3, attire in outfititems{
if(key3 == $item[none])
print("The outfit " +attire+ " consists of these items: " +key1+ " & " +key2);
else
print("The outfit " +attire+ " consists of these items: " +key1+ " & " +key2+ " & " +key3);
}

which works as expected, but I realize that mafia has an outfits.txt so I tried to utilize file_to_map without any success. I tried this:

Code:
string[item, item, item] outfititems;
file_to_map("outfits.txt",outfititems);

foreach key1, key2, key3, attire in outfititems{
if(key3 == $item[none])
print("The outfit " +attire+ " consists of these items: " +key1+ " & " +key2);
else
print("The outfit " +attire+ " consists of these items: " +key1+ " & " +key2+ " & " +key3);
}

That just generated an unexpected error. I assume because some outfits would be missing the third key, like the Bugbear Costume in the above example. I also tried:

Code:
string[item, item, item] outfititems;
file_to_map("outfits.txt",outfititems);

foreach key1, key2, attire in outfititems{
print("The outfit " +outfititems[key1,key2]+ " consists of these items: " +key1+ " & " +key2);
}

That did generate a map, but it was very weird and the keys didn't necessarily correspond to the outfit. Again I assume it was because of a lot of outfits having a third item. Anyways, I'm just trying to find out if there is a way to use file_to_map with the outfits.txt to generate a map that follows the syntax of my first example before I spend all that time typing everything all out.

Also, speaking of maps, I don't really understand the record function. I've been looking through several scripts, but am not sure how it works, or when it's required. Thanks for everyones patience with me while I'm learning my very first scripting/programming language - ASH!
 

jasonharper

Developer
There are a few basic problems here:
1. Outfits aren't limited to three pieces - in fact, there are currently three outfits with four pieces, and one with five (and there will be six-piece outfits once Jick gets around to implementing the hardcore reward outfits).

2. outfits.txt is not structured in such a way that file_to_map() can possibly give you the individual pieces as separate keys - they're all in one field, separated by commas. You'll have to read that field as a string, and then use split_string(field, ", ") on it.

3. The fields in each line of the file are: outfit ID, outfit name, pieces. The ID therefore MUST be the first key in the map you read it into, and the name must be either the second key, or the first value if you use only one key. You can't just skip over unwanted fields, or use them in a different order. This may mean that the map you get directly from file_to_map() may not be usable for your purposes; you may need to use it just once, to build a map that does meet your needs. (You're going to have to do that in any case, to split the pieces.)

A record is just a way of meatpasting together several values so that you can treat them as one value. You can store them in a map, so you look up all those values at once via a single key (rather than having a separate map for looking up each value). You can return one from a function, to get the effect of returning multiple values at once.
 
Oh, I see. That's a lot for me to take in. It's very informative, thank you. I'm still digesting it.

So would the best way to make an outfit map be like this:

Code:
string[item, item, item, item, item, item] outfititems;
outfititems[$item[bugbear beanie], $item[bugbear bungguard], $item[none], $item[none], $item[none], $item[none]] = "Bugbear Costume";

or should I try to use the split field that you where talking about? I toyed with something like this the other day:

Code:
item[string] outfititems;
outfititems["Bugbear Costume"] = $items[bugbear beanie, bugbear bungguard];

foreach i in outfititems{
print("The outfit " + i + " consists of these items: " + outfititems[i].to_string() + "","green");
}

After rereading the update where you implemented plural typed constants I guess they don't work for this particular purpose however.
 

jasonharper

Developer
The best way of setting up a map depends on how you intend to use it: generally, you want the information you already know to be the key(s), and the information you're trying to determine be the value(s).

This is actually workable:
Code:
string[item, item, item, item, item, item] outfititems;
outfititems[$item[bugbear beanie], $item[bugbear bungguard], $item[none], $item[none], $item[none], $item[none]] = "Bugbear Costume";
but you'd find it rather difficult to actually use:
* You can't directly look up the components of a specific outfit, because the outfit name is the value, rather than the key. You'd have to iterate through the entire map each time in order to find the entry whose value is the desired outfit.
* With the pieces as separate keys, you cannot write any sort of loop that will process them all: you'd have to replicate the code for handling a piece six times.

Try something like this:
Code:
item[string, int] outfititems;
outfititems["Bugbear Costume", 0] = $item[bugbear beanie];
outfititems["Bugbear Costume", 1] = $item[bugbear bungguard];

As for:
Code:
item[string] outfititems;
outfititems["Bugbear Costume"] = $items[bugbear beanie, bugbear bungguard];
That didn't work because you declared each value in your map to be an item, singular, yet you are trying to store a list of items in each entry. The actual type of the list returned by $items[...] is boolean[item], so to make a map of such lists, keyed by strings, you'd do:
Code:
boolean[string, item] outfititems;
outfititems["Bugbear Costume"] = $items[bugbear beanie, bugbear bungguard];
 
Top