#CLASS {database} {kill};
#CLASS {database} {open};


#VARIABLE {dbbookmark_file} {logs/features/dbbookmarksync.tin};
#VARIABLE {map_id_names} {
  {1}{Ankh-Morpork}
  {2}{AM Assassins}
  {3}{AM Buildings}
  {4}{AM Cruets}
  {5}{AM Docks}
  {6}{AM Guilds}
  {7}{AM Isle of Gods}
  {8}{Shades Maze}
  {9}{Temple of Small Gods}
  {10}{AM Temples}
  {11}{AM Thieves}
  {12}{Unseen University}
  {13}{AM Warriors}
  {14}{Pseudopolis Watch House}
  {15}{Magpyr's Castle}
  {16}{Bois}
  {17}{Bes Pelargic}
  {18}{BP Buildings}
  {19}{BP Estates}
  {20}{BP Wizards}
  {21}{Brown Islands}
  {22}{Death's Domain}
  {23}{Djelibeybi}
  {24}{IIL - DJB Wizards}
  {25}{Ephebe}
  {26}{Ephebe Underdocks}
  {27}{Genua}
  {28}{Genua Sewers}
  {29}{GRFLX Caves}
  {30}{Hashishim Caves}
  {31}{Klatch Region}
  {32}{Lancre Region}
  {33}{Mano Rossa}
  {34}{Monks of Cool}
  {35}{Netherworld}
  {37}{Pumpkin Town}
  {38}{Ramtops Regions}
  {39}{Sto-Lat}
  {40}{Academy of Artificers}
  {41}{Cabbage Warehouse}
  {42}{AoA Library}
  {43}{Sto-Lat Sewers}
  {44}{Sprite Caves}
  {45}{Sto Plains Region}
  {46}{Uberwald Region}
  {47}{UU Library}
  {48}{Klatchian Farmsteads}
  {49}{CTF Arena}
  {50}{PK Arena}
  {51}{AM Post Office}
  {52}{Ninja Guild}
  {53}{The Travelling Shop}
  {54}{Slippery Hollow}
  {55}{House of Magic - Creel}
  {56}{Special Areas}
  {57}{Skund Wolf Trail}
}

#FUNCTION {map_parse} {
    #unvar {map_term} {search_map};
    #format {search_map} {%l} {%1};

    #regex {$search_map} {^am} { #var {map_term} {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 51} };
    #regex {$search_map} {^ankh-morpork$} { #var {map_term} {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 51} };
    #regex {$search_map} {^bp$} { #var {map_term} {17, 18, 19, 20, 21} };
    #regex {$search_map} {^bes pelargic$} { #var {map_term} {17, 18, 19, 20, 21} };
    #regex {$search_map} {^djb$} { #var {map_term} {23, 24} };
    #regex {$search_map} {^djelibeybi} { #var {map_term} {23, 24} };
    #regex {$search_map} {^ephebe$} { #var {map_term} {25, 26} };
    #regex {$search_map} {^genua$} { #var {map_term} {27, 28, 16} };
    #regex {$search_map} {^ramtops$} { #var {map_term} {38, 32, 54, 55} };
    #regex {$search_map} {^sto$} { #var {map_term} {29, 45} };
    #regex {$search_map} {^sto plains$} { #var {map_term} {29, 45} };
    #regex {$search_map} {^sto-lat$} { #var {map_term} {39, 40, 41, 42, 43} };

    #return {$search_map};
};

#LIST {last_query_list} {create};

#ALIAS {db gatherable %1} {
    #unvariable {gatherable_display};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #format {query} {SELECT item_name, room_short, shop_items.room_id, map_id, INSTR(item_name, '%s') pos FROM shop_items INNER JOIN rooms on rooms.room_id = shop_items.room_id WHERE pos > 0 AND sale_price = 'gather' LIMIT 10;} {$search_term};
    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};

    #script {gatherable_query_result} {$db_command};
    #list {gatherable_query_result} {size} {gatherable_result_size};

    #list {last_query_list} {clear};

    #if {$gatherable_result_size > 0} {
        #format {gatherable_display} {%cResults for gatherables matching "%c%s%c":<099>} {Azure}{Yellow}{%1}{Azure};
        #echo {$gatherable_display};

        #var {gather_count} {0};

        #foreach {$gatherable_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list gatherable_results create {${inner_query_result}};

            #variable {gatherable_info} {
                {item_name}{$gatherable_results[1]}
                {room_short}{$gatherable_results[2]}
                {room_id}{$gatherable_results[3]}
                {map_name}{$map_id_names[$gatherable_results[4]]}
            };

            #showme {<acf>  [<cfe>@dbroute_link{$gather_count;$gather_count}<acf>]: <cfa>@dbroute_link{$gather_count;$gatherable_info[item_name]}<acf> found in <fca>@dbroute_link{$gather_count;$gatherable_info[room_short]}<acf>,<fca> $gatherable_info[map_name]<099>};

            #variable {last_query_list[$gather_count]} {$gatherable_info};
            #math {gather_count} {$gather_count + 1};
        };
        #showme {<acf>Generate a walking path with "<cfa>db route #<acf>" or *clicking* the number or name of one of the results above.};
        #showme {};
    } {
        #format {gatherable_display} {%cQuery Result: %c%s%c was not found.<099>} {Azure}{Green}{%1}{Azure};
        #echo {$gatherable_display};
    };
};

#ALIAS {db npc {[^{}\n]+}{( \{([\w\s-]+)\})?}$} {
    #unvariable {npc_display} {search_term} {query};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #nop If we found an area name in curly braces, try to match a map-set;
    #var {search_map} {@map_parse{%4}};

    #if {&{map_term}} {
        #format {query} {SELECT npc_name, rooms.map_id, rooms.room_id, rooms.room_short, INSTR(npc_name, '%s') pos FROM npc_info INNER JOIN rooms on rooms.room_id = npc_info.room_id WHERE pos > 0 AND rooms.map_id IN (%s) LIMIT 10} {$search_term} {$map_term};
    };
    #else {
        #format {query} {SELECT npc_name, rooms.map_id, rooms.room_id, rooms.room_short, INSTR(npc_name, '%s') pos FROM npc_info INNER JOIN rooms on rooms.room_id = npc_info.room_id WHERE pos > 0 LIMIT 10} {$search_term};
    };

    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};
    #script {npc_query_result} {$db_command};
    #list {npc_query_result} {size} {npc_result_size};

    #list {last_query_list} {clear};

    #if {$npc_result_size > 0} {
        #format {npc_display} {%cResults for NPCs matching "%c%s%c":<099>} {Azure}{Yellow}{%1}{Azure};
        #echo {$npc_display};

        #var {npc_count} {0};

        #foreach {$npc_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list npc_results create {${inner_query_result}};

            #variable {npc_info} {
                {npc_name}{$npc_results[1]}
                {map_name}{$map_id_names[$npc_results[2]]}
                {room_id}{$npc_results[3]}
                {room_short}{$npc_results[4]}
            };

            #showme {<acf>  [<cfe>@dbroute_link{$npc_count;$npc_count}<acf>]: <cfa>@dbroute_link{$npc_count;$npc_info[npc_name]}<acf> found in <fca>@dbroute_link{$npc_count;$npc_info[room_short]}<acf>,<fca> $npc_info[map_name]<099>};

            #variable {last_query_list[$npc_count]} {$npc_info};
            #math {npc_count} {$npc_count + 1};
        };
        #showme {<acf>Generate a walking path with "<cfa>db route #<acf>" or *clicking* the number or name of one of the results above.};
        #showme {};
    } {
        #format {npc_display} {%cQuery Result: %c%s%c was not found.<099>} {Azure}{Green}{%1}{Azure};
        #echo {$npc_display};
    }
};

#ALIAS {db npcitem {[^{}\n]+}{( \{([\w\s-]+)\})?}$} {
    #unvariable {npcitem_display} {search_term} {query};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #nop If we found an area name in curly braces, try to match a map-set;
    #var {search_map} {@map_parse{%4}};

    #if {&{map_term}} {
        #format {query} {SELECT npc_info.map_id, npc_name, npc_info.room_id, item_name, room_short, INSTR(item_name, '%s') pos FROM 'npc_info' INNER JOIN npc_items ON npc_items.npc_id = npc_info.npc_id INNER JOIN rooms on npc_info.room_id = rooms.room_id WHERE pos > 0 AND rooms.map_id IN (%s) LIMIT 10} {$search_term} {$map_term};
    };
    #else {
        #format {query} {SELECT npc_info.map_id, npc_name, npc_info.room_id, item_name, room_short, INSTR(item_name, '%s') pos FROM 'npc_info' INNER JOIN npc_items ON npc_items.npc_id = npc_info.npc_id INNER JOIN rooms on npc_info.room_id = rooms.room_id WHERE pos > 0 LIMIT 10} {$search_term};
    };

    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};
    #script {npcitem_query_result} {$db_command};
    #list {npcitem_query_result} {size} {npcitem_result_size};

    #list {last_query_list} {clear};

    #if {$npcitem_result_size > 0} {
        #format {npcitem_display} {%cResults for NPC items matching "%c%s%c":<099>} {Azure}{Yellow}{%1}{Azure};
        #echo {$npcitem_display};

        #var {npcitem_count} {0};

        #foreach {$npcitem_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list npcitem_results create {${inner_query_result}};

            #variable {npcitem_info} {
                {map_name}{$map_id_names[$npcitem_results[1]]}
                {npc_name}{$npcitem_results[2]}
                {room_id}{$npcitem_results[3]}
                {item_name}{$npcitem_results[4]}
                {room_short}{$npcitem_results[5]}
            };

            #showme {<acf>  [<cfe>@dbroute_link{$npcitem_count;$npcitem_count}<acf>]: <cfa>@dbroute_link{$npcitem_count;$npcitem_info[item_name]}<acf> found on <fff>$npcitem_info[npc_name]<acf> in <fca>@dbroute_link{$npcitem_count;$npcitem_info[room_short]}<acf>,<fca> $npcitem_info[map_name]<099>};

            #variable {last_query_list[$npcitem_count]} {$npcitem_info};
            #math {npcitem_count} {$npcitem_count + 1};
        };
        #showme {<acf>Generate a walking path with "<cfa>db route #<acf>" or *clicking* the number or name of one of the results above.};
        #showme {};
    } {
        #format {npcitem_display} {%cQuery Result: %c%s%c was not found.<099>} {Azure}{Green}{%1}{Azure};
        #echo {$npcitem_display};
    }
};

#ALIAS {^db item {[^{}\n]+}{( \{([\w\s-]+)\})?}$} {
    #unvariable {item_display} {search_term} {query};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #nop If we found an area name in curly braces, try to match a map-set;
    #var {search_map} {@map_parse{%4}};

    #if {&{map_term}} {
        #format {query} {SELECT item_name, sale_price, room_short, shop_items.room_id, map_id, INSTR(item_name, '%s') pos FROM shop_items INNER JOIN rooms on rooms.room_id = shop_items.room_id WHERE pos > 0 AND sale_price != 'gather' AND map_id IN (%s) LIMIT 10;} {$search_term} {$map_term};
    };
    #else {
        #format {query} {SELECT item_name, sale_price, room_short, shop_items.room_id, map_id, INSTR(item_name, '%s') pos FROM shop_items INNER JOIN rooms on rooms.room_id = shop_items.room_id WHERE pos > 0 AND sale_price != 'gather' LIMIT 10;} {$search_term};
    };

    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};
    #script {item_query_result} {$db_command};
    #list {item_query_result} {size} {item_result_size};

    #list {last_query_list} {clear};

    #if {$item_result_size > 0} {
        #format {item_display} {%cResults for items matching "%c%s%c":<099>} {Azure}{Yellow}{%1}{Azure};
        #echo {$item_display};

        #var {item_count} {0};

        #foreach {$item_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list item_results create {${inner_query_result}};

            #variable {item_info} {
                {item_name}{$item_results[1]}
                {sale_price}{$item_results[2]}
                {room_short}{$item_results[3]}
                {room_id}{$item_results[4]}
                {map_name}{$map_id_names[$item_results[5]]}
            };

            #nop #format {item_display} {%c  [%c%d%c]: %c%s%c (%c%s%c) found in %c%s%c,%c %s%c<099>} {Azure}{Cyan}{$item_count}{Azure}{Green}{$item_info[item_name]}{Azure}{Yellow}{$item_info[sale_price]}{Azure}{Orange}{$item_info[room_short]}{Azure}{Orange}{$item_info[map_name]}{Azure};
            #nop #echo {$item_display};
            #showme {<acf>  [<cfe>@dbroute_link{$item_count;$item_count}<acf>]: <cfa>@dbroute_link{$item_count;$item_info[item_name]}<acf> (<ffa>$item_info[sale_price]<acf>) found in <fca>@dbroute_link{$item_count;$item_info[room_short]}<acf>,<fca> $item_info[map_name]<099>};

            #variable {last_query_list[$item_count]} {$item_info};
            #math {item_count} {$item_count + 1};
        };
        #showme {<acf>Generate a walking path with "<cfa>db route #<acf>" or *clicking* the number or name of one of the results above.};
        #showme {};
    } {
        #format {item_display} {%cQuery Result: %c%s%c was not found.<099>} {Azure}{Green}{%1}{Azure};
        #echo {$item_display};
    };
};

#ALIAS {^db room {[^{}\n]+}{( \{([\w\s-]+)\})?}$} {
    #unvariable {room_display} {search_term} {query};

    #variable {search_term} {%1};
    #replace {search_term} {'} {''};

    #nop If we found an area name in curly braces, try to match a map-set;
    #var {search_map} {@map_parse{%4}};

    #if {&{map_term}} {
        #format {query} {SELECT room_id, map_id, room_short, room_type, INSTR(room_short, '%s') pos FROM rooms WHERE pos > 0 AND map_id IN (%s) LIMIT 10;} {$search_term} {$map_term};
    };
    #else {
        #format {query} {SELECT room_id, map_id, room_short, room_type, INSTR(room_short, '%s') pos FROM rooms WHERE pos > 0 LIMIT 10;} {$search_term};
    };

    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};
    #script {room_query_result} {$db_command};
    #list {room_query_result} {size} {room_result_size};

    #list {last_query_list} {clear};

    #if {$room_result_size > 0} {
        #format {room_display} {%cResults for items items matching "%c%s%c":<099>} {Azure}{Yellow}{%1}{Azure};
        #echo {$room_display};

        #var {room_count} {0};

        #foreach {$room_query_result[%*]} {inner_query_result} {
            #replace {inner_query_result} {\\} {};
            #list room_results create {${inner_query_result}};

            #variable {room_info} {
                {room_id}{$room_results[1]}
                {map_name}{$map_id_names[$room_results[2]]}
                {room_short}{$room_results[3]}
                {room_type}{$room_results[4]}
            };

            #nop #format {room_display} {%c  [%c%d%c]: %c%s%c (%c%s%c) found in %c%s%c<099>} {Azure}{Cyan}{$room_count}{Azure}{Green}{$room_info[room_short]}{Azure}{Yellow}{$room_info[room_type]}{Azure}{Orange}{$room_info[map_name]}{Azure};
            #showme {<acf>  [<cfe>@dbroute_link{$room_count;$room_count}<acf>]: <cfa>@dbroute_link{$room_count;$room_info[room_short]} <acf>(<ffa>$room_info[room_type]<acf>) found in <fca>$room_info[map_name]<099>};

            #variable {last_query_list[$room_count]} {$room_info};
            #math {room_count} {$room_count + 1};
        };
        #showme {<acf>Generate a walking path with "<cfa>db route #<acf>" or *clicking* the number or name of one of the results above.};
        #showme {};
    } {
        #format {room_display} {%cQuery Result: %c%s%c was not found.<099>} {Azure}{Green}{%1}{Azure};
        #echo {$room_display};
    };
};

#ALIAS {db route %1} {
    #if {&last_query_list[%1]} {
        #var {target_room} {$last_query_list[%1]};
        #format {route_display} {<acf>[<fff>speedwalk<acf>] Generating speedwalk from current location to %c%s<acf>, %c%s<acf>...<099>} {Green}{$target_room[room_short]}{Orange}{$target_room[map_name]};
        #echo {$route_display};

        #var {route_current_room_id} {$GMCP[room][info][identifier]};
        #var {route_target_room_id} {$target_room[room_id]};

        #format {route_command} {python3 src/maproute.py %s %s} {$route_current_room_id} {$route_target_room_id};
        #script {speedwalk_result} {$route_command};

        #if {"$speedwalk_result[1]" == "0"} {
            #format {route_error} {<acf>[<fff>speedwalk<acf>] Unable to find route to %c%s<acf> or unknown current room.<099>} {Green}{$target_room[room_short]};
            #echo {$route_error};
            #return;
        };
        #send {$speedwalk_result[1]};
        
        #variable {route_length} {$speedwalk_result[1]};
        #replace {route_length} {\\\;} {;};
        #replace {route_length} { } {};
        #list {route_length} {create} {$route_length};
        #list {route_length} {size} {route_length};

        #format {route_confirm} {<acf>[<fff>speedwalk<acf>] A route was found, type "%c@dbspeedwalk_link{}<acf>" to fast travel. [%c%g<acf> steps]<099>} {Green}{Cyan}{$route_length};
        #echo {$route_confirm};
    } {
        #format {route_display} {<acf>[<fff>speedwalk<acf>] No previous query data found, try searching first.<099>};
        #echo {$route_display};
    };
};

#ALIAS {speedwalk} {
    #send {RuhsSpeedRun};
};

#ALIAS {db routeto %1} {
    #var {target_room} {%1};
    #format {route_display} {<acf>[<fff>speedwalk<acf>] Generating speedwalk from current location to specified room. <aaa>%s<099>} {%1};

    #echo {$route_display};

    #var {route_current_room_id} {$GMCP[room][info][identifier]};
    #var {route_target_room_id} {%1};

    #format {route_command} {python3 src/maproute.py %s %s} {$route_current_room_id} {$route_target_room_id};
    #script {speedwalk_result} {$route_command};

    #if {"$speedwalk_result[1]" == "0"} {
        #format {route_error} {<acf>[<fff>speedwalk<acf>] Unable to find route to %c%s<acf> or unknown current room.<099>} {Green}{$target_room[room_short]};
        #echo {$route_error};
        #return;
    };
    #send {$speedwalk_result[1]};

    #variable {route_length} {$speedwalk_result[1]};
    #replace {route_length} {\\\;} {;};
    #replace {route_length} { } {};
    #list {route_length} {create} {$route_length};
    #list {route_length} {size} {route_length};

    #format {route_confirm} {<acf>[<fff>speedwalk<acf>] A route was found, type %c@dbspeedwalk_link{}<acf> to fast travel. [%c%g<acf> steps]<099>} {Green}{Cyan}{$route_length};
    #echo {$route_confirm};
};

#ALIAS {^db$} {
    #showme {<acf>[<fff>speedwalk<acf>] Try "<afa>db help<acf>" for usage instructions.<099>};
};

#ALIAS {db help} {
    #showme {<acf>[<fff>speedwalk<acf>] Speedwalk and Database Lookup Commands:<099>};
    #showme {   <138>*<acf> "<bdc>db room <bbd><query><acf>" - search for a room by name<099>};
    #showme {   <138>*<acf> "<bdc>db item <bbd><query><acf>" - search for an item in shops by name<099>};
    #showme {   <138>*<acf> "<bdc>db npc <bbd><query><acf>" - search for an NPC by name<099>};
    #showme {   <138>*<acf> "<bdc>db npcitem <bbd><query><acf>" - search for an item in NPC inventories by name<099>};
    #showme {   <138>*<acf> "<bdc>db gatherable <bbd><query><acf>" - search for a gatherable item by name<099>};
    #showme {<099>};
    #showme {<acf>After searching with one of the above commands, find a route there with "<bdc>db route <bbd><#><acf>", where "<bbd><#><acf>" is the row number shown in the results. You can then type "<afa>speedwalk<acf>" to start moving. Alternatively, <fbe>mouse-click<acf> on a result to generate a route, then click "<afa>speedwalk<acf>" to begin moving.<099>};
    #showme {<099>};
    #showme {<acf>There's also a bookmark system for frequently used rooms, see "<afa>db bookmark help<acf>" for details.<099>};
    #showme {<099>};
    #showme {<acf>Tips:<099>};
    #showme {   <138>*<acf> When searching, the "<bbd>query<acf>" is case-sensitive.<099>};
    #showme {   <138>*<acf> Searches may be filtered to certain maps by providing a second parameter like "<bbd>\{map_name\}<acf>".<099>};
    #showme {   <138>*<acf> Example, to find swords in the Ramtops, you could "<bdc>db item <bbd>sword \{ramtops\}<acf>".<099>};
    #showme {   <138>*<acf> Valid map names include:<099>};
    #showme {         <138>* <dcf>am/ankh-morpork         <138>* <dcf>bp/bes pelargic<acf><099>};
    #showme {         <138>* <dcf>djb/djelibeybi          <138>* <dcf>ephebe<acf><099>};
    #showme {         <138>* <dcf>genua                   <138>* <dcf>ramtops<acf><099>};
    #showme {         <138>* <dcf>sto/sto plains          <138>* <dcf>sto-lat<acf> <099>};
};

#nop *** BOOKMARK SYSTEM IMPLEMENTATION ***;
#ALIAS {^db bookmark help$} {
    #showme {<acf>[<fff>speedwalk<acf>] Speedwalk Bookmark System Commands:<099>};
    #showme {   <138>*<acf> "<bdc>db bookmark <bbd>add here<acf>" - add current location to bookmarks.<099>};
    #showme {   <138>*<acf> "<bdc>db bookmark <bbd>list<acf>" - show a list of saved locations.<099>};
    #showme {   <138>*<acf> "<bdc>db bookmark <bbd>remove <#><acf>" - remove bookmark at index..<099>};
    #showme {<099>};
    #showme {<acf>Generate a route from your current location to a bookmark using "<afa>db bookmark route <bbd><#><acf>" or <fbe>mouse-clicking<acf> on a result from "<afa>db bookmark list<acf>". You can then click or type "<afa>speedwalk<acf> to start moving.<099>};
};

#nop sync database speedwalk bookmarks from file on load;
#class {dbsync} {clear};
#class {dbsync} {read} {$dbbookmark_file};
#variable {db_bookmark_list} {$bookmark_sync};
#class {dbsync} {kill};

#ALIAS {^db bookmark list$} {
    #showme {<acf>[<fff>speedwalk<acf>] Speedwalk Saved Bookmarks:<099>};
    #list {db_bookmark_list} {size} {db_bookmark_size};
    #if {$db_bookmark_size > 0} {
        #var {bookmark_count} {0};
        #foreach {$db_bookmark_list[%*]} {bookmark_result} {
            #math {bookmark_list_index} {$bookmark_count + 1};
            #variable {bookmark_room_id} {$db_bookmark_list[$bookmark_list_index][id]};
            #showme {<acf>  [<cfe>@dbrouteto_link{$bookmark_room_id;$bookmark_count}<acf>]: Room "<cfa>@dbrouteto_link{$bookmark_room_id;$bookmark_result[name]}<acf>" in <fca>$bookmark_result[map]<acf> (<cde>$bookmark_result[type]<acf>).<099>};
            #math {bookmark_count} {$bookmark_count + 1};
        };
    };
    #else {
        #showme {<acf>  No saved bookmarks found, add one with "<cfa>db bookmark add here<acf>" or refer to "<cfa>db bookmark help<acf>".<099>};
    };
};
#ALIAS {^db bookmark add here$} {
    #nop first - are we at our bookmark capacity?;
    #list {db_bookmark_list} {size} {db_bookmark_size};
    #if {$db_bookmark_size > 19} {
        #showme {<acf>[<fff>speedwalk<acf>] Maximum allowed number of bookmarks already set, remove one first.<099>};
        #return;
    };

    #nop next - do we already have this place bookmarked?;
    #foreach {$db_bookmark_list[%*]} {bookmark_result} {
        #if {"$bookmark_result[id]" == "$GMCP[room][info][identifier]"} {
            #showme {<acf>[<fff>speedwalk<acf>] This room is already bookmarked and entitled "<cfa>$bookmark_result[name]<acf>".<099>};
            #return;
        };
    };

    #nop TODO if we decide to allow nicknames then need to check for duplicates there as well;
    #nop TODO saving class to file each time we add/remove so it persists;

    #nop some places don't have a "name" (e.g. Medina or Shades), don't allow bookmarking those for now.;
    #if {"$GMCP[room][info][name]" == ""} {
        #showme {<acf>[<fff>speedwalk<acf>] This room is not able to be bookmarked.<099>};
        #return;
    };

    #nop go to the database to get a MapID to store;
    #format {query} {SELECT room_id, map_id, room_short, room_type FROM rooms WHERE room_id = '%s' LIMIT 1;} {$GMCP[room][info][identifier]};
    #format {db_command} {sqlite3 -separator ";" src/quow.db "%s"} {$query};
    #script {room_query_result} {$db_command};
    #format {room_query_result} {$room_query_result[1]};
    #replace {room_query_result} {\\} {};
    #list {room_results} {create} {${room_query_result}};

    #variable {room_info} {
        {room_id}{$room_results[1]}
        {map_name}{$map_id_names[$room_results[2]]}
        {room_short}{$room_results[3]}
        {room_type}{$room_results[4]}
    };

    #nop finally put the new bookmark in our list;
    #list {db_bookmark_list} {add} {{
        {id}{$room_info[room_id]}
        {name}{$room_info[room_short]}
        {map}{$room_info[map_name]}
        {type}{$room_info[room_type]}
    }};
    #showme {<acf>[<fff>speedwalk<acf>] Added a new bookmark for "<cfa>$GMCP[room][info][name]<acf>" in <fca>$room_info[map_name]<acf> (<cde>$room_info[room_type]<acf>).<099>};
    /writedbbookmarkstofile;
};
#ALIAS {^db bookmark remove %d$} {
    #math {bookmark_removal_index} {%1 + 1};

    #nop is this index in valid range?;
    #list {db_bookmark_list} {size} {db_bookmark_size};
    #if {$db_bookmark_size < $bookmark_removal_index} {
        #showme {<acf>[<fff>speedwalk<acf>] No bookmark with number "<cfa>%1<acf>" set, check "<fca>db bookmark list<acf>".<099>};
        #return;
    };

    #nop otherwise seems valid, so remove it;
    #list {db_bookmark_list} {get} {$bookmark_removal_index} {removed_bookmark};
    #list {db_bookmark_list} {delete} {$bookmark_removal_index};
    #showme {<acf>[<fff>speedwalk<acf>] Removed an existing bookmark for "<cfa>$removed_bookmark[name]<acf>" at index "<fca>$bookmark_removal_index<acf>".<099>};
    /writedbbookmarkstofile;
};
#ALIAS {^db bookmark route %d$} {
    #math {bookmark_route_index} {%1 + 1};

    #nop is this index in valid range?;
    #list {db_bookmark_list} {size} {db_bookmark_size};
    #if {$db_bookmark_size < $bookmark_route_index} {
        #showme {<acf>[<fff>speedwalk<acf>] No bookmark with number "<cfa>%1<acf>" set, check "<fca>db bookmark list<acf>".<099>};
        #return;
    };

    #nop otherwise seems valid, so lets route to it;
    db routeto $db_bookmark_list[$bookmark_route_index][id];
};
#ALIAS {^/writedbbookmarkstofile} {
    #class {dbsync} {clear};
    #class {dbsync} {open};
    #var {bookmark_sync} {$db_bookmark_list};
    #class {dbsync} {close};
    #class {dbsync} {write} {$dbbookmark_file};
};
#nop *** END BOOKMARK SYSTEM IMPLEMENTATION ***;


#nop ===[ Handle Events for Clickable Links... for the newbs ]===;
#nop handle DBROUTE click events;
#EVENT {PRESSED SECURE LINK DBROUTE MOUSE BUTTON ONE} {
    db route %4;
};
#EVENT {PRESSED SECURE LINK DBROUTETO MOUSE BUTTON ONE} {
    db routeto %4;
};
#nop handle SPEEDWALK click events;
#EVENT {PRESSED SECURE LINK DBSPEEDWALK MOUSE BUTTON ONE} {
    speedwalk;
};

#nop ===[ Functions to generate the clickable links ]===;
#FUNCTION {dbroute_link} {
    #nop %1 = query result index, %2 = text to display the link as;
    #return {\e]68;2;DBROUTE;%1\a\e[4;24m%2\e[24m};
};
#FUNCTION {dbrouteto_link} {
    #nop %1 = id to route to, %2 = text to display the link as;
    #return {\e]68;2;DBROUTETO;%1\a\e[4;24m%2\e[24m};
};
#FUNCTION {dbspeedwalk_link} {
    #return {\e]68;2;DBSPEEDWALK;;\a\e[4mspeedwalk\e[24m};
};


#CLASS {database} {close};
