2 * Route searcher - common definitions
5 * This is part of the YARRG website, a tool for assisting
6 * players of Yohoho Puzzle Pirates.
8 * Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24 * are used without permission. This program is not endorsed or
25 * sponsored by Three Rings.
33 #define DEBUG_FLAG_LIST \
44 //#define debug_flags 0
46 #define debug debug_file
50 extern FILE *debug_file;
51 #define DEBUG_DEV "/dev/stdout" /* just for glpk */
56 #define COUNTER_LIST \
57 CTR(commodities_loaded) \
59 CTR(islands_arbitrage) \
60 CTR(ipairs_relevant) \
61 CTR(quantities_loaded) \
62 CTR(routes_considered) \
63 CTR(routes_wrongfinalelim) \
64 CTR(routes_quickelim) \
65 CTR(routes_bucketelim) \
67 CTR(routes_wrongfinal) \
68 CTRA(newbests_granu,GRANUS*2) \
69 CTR(subroute_tails_valued) \
70 CTR(subroutes_valued) \
71 CTR(subroutes_nonempty)
72 #define CTR(x) extern int ctr_##x;
73 #define CTRA(x,n) extern int ctr_##x[n];
78 #define SQL_MUST( call ) ({ \
79 /* `call' is an expression returning result, using const char *sqe; \
80 * chk1 and chk2 are blocks using sqe and int sqr; */ \
81 const char *sql_must_call_string= #call; \
83 if (DEBUGP(sql2)) fprintf(debug,"SQL %s", sql_must_call_string); \
85 if (DEBUGP(sql2)) fprintf(debug," = %d\n", sqr); \
86 if (sqr) sql_fatal("(unknown)", sqr, sql_must_call_string); \
89 void sql_fatal(const char *stmt_what, int sqr, const char *act_what) NORET;
91 #define SQL_STEP(ssh) (sql_step((ssh), #ssh, __FILE__, __LINE__))
92 int sql_step(sqlite3_stmt *ssh, const char *ssh_string,
93 const char *file, int line);
95 #define SQL_DISTINCT_DECL(cols, nintcols) \
98 #define SQL_DISTINCT_STEP(ssh, cols, nkeycols) \
99 (sql_step_distinct((ssh), #ssh, __FILE__, __LINE__, \
100 (cols), sizeof((cols))/sizeof((cols)[0]), nkeycols))
101 int sql_step_distinct(sqlite3_stmt *ssh, const char *ssh_string,
102 const char *file, int line,
103 int *cols, int ncols, int nkeycols);
104 /* These work if we're making a query whose columns consist of:
105 * - keys: integer column(s) on which the results are sorted by the query
106 * - consequences: zero or more integer cols strictly dependent on the keys
107 * - extra: zero or more further (possibly non-integer) columns
109 * Call SQL_DISTINCT_DECL, passing intcols = the total number of keys and
110 * consequences; it will declare int cols[intfields];
112 * Then each SQL_DISTINCT_STEP is like SQL_STEP only you have to
113 * pass the number of key columns and it only returns rows with
114 * distinct keys. Rows with all-identical keys are asserted to
115 * have identical consequences. After each call to
116 * SQL_DISTINCT_STEP the keys and consequences will be stored in
120 int sql_single_int(const char *stmt);
122 #define SQL_PREPARE(ss,stmt) ((ss)= sql_prepare((stmt),#ss))
123 sqlite3_stmt *sql_prepare(const char *stmt, const char *what);
125 #define SQL_BIND(ss,index,value) (sql_bind((ss),(index),(value),#ss,#value))
126 void sql_bind(sqlite3_stmt *ss, int index, int value,
127 const char *ss_what, const char *val_what);
129 #define MAX_ROUTELEN 20
133 void setup_sql(const char *database);
137 double distance_loss_factor;
138 struct TradesBlock *trades;
139 double route_tail_value;
142 IslandPair *ipair_get_maybe(int si, int di);
144 double value_route(int nislands, const int *islands, int exclude_arbitrage);
145 void setup_value(void);
147 #define AP 2 /* 0=absolute, 1=perleague */
154 int ports[MAX_ROUTELEN];
155 } OnePotentialResult;
158 OnePotentialResult prs[AP];
161 void setup_search(void);
162 void search(int start_isle, int final_isle /* -1 means any */,
163 Bucket ****buckets_base_io[GRANUS]
164 /* bucket_base[granui][finalthing][midthing]-> */);
166 extern double max_mass, max_volu, max_capi;
167 extern double distance_loss_factor_per_league;
168 extern int max_dist, min_trade_maxprofit;
170 #define LOSS_FACTOR_PER_DELAY_SLOT (1-1e-8)
172 extern int islandtablesz;
175 extern char **archnames;
176 extern int *islandid2arch;
178 extern int granusz_fin[GRANUS], granusz_mid[GRANUS];
184 #define NEW(ptr) ((ptr)= mmalloc(sizeof(*ptr)))
186 #define MCALLOC(array, count) ((array)= mcalloc(sizeof(*(array)) * (count)))
188 #define MCALLOC_INITEACH(array, count, init_this) ({ \
189 MCALLOC((array), (count)); \
191 typeof(&(array)[0]) this; \
192 for (initi=0, this=(array); initi<(count); initi++, this++) { \
204 extern int nhighscores[GRANUS][AP];
205 extern HighScoreEntry *highscores[GRANUS][AP];
208 #define ONDEMAND(pointer_lvalue, calloc_size_count) \
209 ((pointer_lvalue) ? : \
210 ((pointer_lvalue) = mcalloc(sizeof(*(pointer_lvalue)) * calloc_size_count)))
213 static inline int isle2arch(int isle) {
214 int arch= islandid2arch[isle];
219 static inline int route2midarch(const int *ports, int nports) {
220 int archs[nports], last_arch=-1, narchs=0, i;
221 for (i=0; i<nports; i++) {
222 int arch= isle2arch(ports[i]);
223 if (arch==last_arch) continue;
224 archs[narchs++]= last_arch= arch;
226 return archs[narchs/2];
230 #endif /*RSCOMMON_H*/