Writing a function that can handle any map structure?

zarqon

Well-known member
Okay, here's the latest scripting challenge for which I've been unable to come up with a suitable solution. I present it here for the resident wizards, gurus, et. al.

I use this function a lot to load maps from my Map Manager:

Code:
boolean load_current_map(string fname, string[string] dest) {
   string curr = visit_url("http://zachbardon.com/mafiatools/autoupdate.php?f="+fname+"&act=getver");
   file_to_map(fname+".txt",dest);
   if  (count(dest) == 0 || (curr != "" && get_property(fname+".txt") != curr)) {
      print("Updating "+fname+".txt from '"+get_property(fname+".txt")+"' to '"+curr+"'...");
      if (!file_to_map("http://zachbardon.com/mafiatools/autoupdate.php?f="+fname+"&act=getmap",dest) || count(dest) == 0) return false;
      map_to_file(dest,fname+".txt");
      set_property(fname+".txt",curr);
      print("..."+fname+".txt updated.");
   }
   return true;
}

I would love to put this function in ZLib, where I could just have it once for all my scripts. Note that everything inside this function is completely portable. But the problem is that the map may be of any structure -- i.e. it's not always string[string], it may be familiar[location] or convoluted_record[int,float]. So, even though the map structure is not even used in the function, I have to copy and paste this function for every map that I wish to load, since they all have different structures.

This function is actually already in ZLib -- but of course it's useless for updating maps that aren't string[familiar].

It's a pity I can't just use "aggregate" as the type for dest.

Possible workarounds I've thought of so far:

1) There may be a workaround Using cli_execute("ash etc etc"), because you could specify the map structure as a string, but I haven't been able to think of one that doesn't need to load the map more than once from disk. Also, how would the return value remain meaningful?
2) There may also be a workaround using an overlarge string[string,string,string,string,string,string,string] map as the parameter, to accommodate most possible record types. Haven't explored this avenue because it looks so unattractive.
3) A combination of the above two, where instead of a map parameter, the number of data points (strings) per line in the map is given as the second parameter. Then the map type can be specified without extra strings looked for using the cli_execute("ash blah") trick.
4) Is there any way to do this using ASH's "call"? I'm thinking not, but thought I'd ash. Er, ask.

Um, that's all I've got so far. How to handle the return value is tricky with all of these. It's a problem that's been nagging at me for a while now. Any help would be appreciated.
 

Alhifar

Member
Shouldn't a set of helper functions like:
Code:
boolean load_current_map(string fname, string[int] dest) {
   string[string] temp;
   load_current_map(fname,temp);
   foreach key in temp
      dest[key.to_int()]=temp[key];
   if(count(dest) == 0) return false;
   return true;
}
work? I've not tested it, but I'm not really sure why it wouldn't.
 

Veracity

Developer
Staff member
We might be able to let you declare a variable to be an "aggregate". The ONLY use you could make of it would be to pass to internal ASH functions which can take any "aggregate" - file_to_map, map_to_file, count, and clear.

I'll consider it.
 

zarqon

Well-known member
@Alhifar: Correct me if I'm wrong, but it seems that has the same problem -- I would need a separate function for each kind of map. I would like to handle any map structure with a single function if possible.

@Veracity: That would be fantastic!
 

Alhifar

Member
If you included it in zlib, then in practical application, you would still just use load_current_map(fname,aggregate) for any type of aggregate, where it would pick the correct version of the overloaded function for the map you're using.

I can understand preferring a more elegant solution, but wouldn't one that works for now help?
 

zarqon

Well-known member
Oh, I see. But it's still impossible to specify all the possible structures, since many if not most use records which would not be defined in ZLib, i.e. checklist_step[int].

I could change the function to simply update the map rather than actually loading it, which it could do by treating everything as a string, but I'd really like something that keeps the existing functionality.
 

zarqon

Well-known member
We might be able to let you declare a variable to be an "aggregate". The ONLY use you could make of it would be to pass to internal ASH functions which can take any "aggregate" - file_to_map, map_to_file, count, and clear.

I'll consider it.

Still considering? If this is going to happen, which would be stupendous, I'd like to put it into the next ZLib release.
 
Top