chiark / gitweb /
237cab1347d8006ed63587529fd61f442a34acb9
[ypp-sc-tools.main.git] / yarrg / rscommon.h
1 /*
2  * Route searcher - common definitions
3  */
4 /*
5  *  This is part of the YARRG website, a tool for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  *
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.
14  *  
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.
19  *  
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/>.
22  *  
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.
26  */
27
28 #ifndef RSCOMMON_H
29 #define RSCOMMON_H
30
31 #include <sqlite3.h>
32
33 #define DEBUG_FLAG_LIST                         \
34    DF(sql)                                      \
35    DF(sql2)                                     \
36    DF(value)                                    \
37    DF(value2)                                   \
38    DF(search)                                   \
39    DF(filter)                                   \
40    DF(check)                                    \
41    DF(tableau)                                  \
42    DF(lp)
43
44 //#define debug_flags 0
45
46 #define debug debug_file
47
48 #include "common.h"
49
50 extern FILE *debug_file;
51 #define DEBUG_DEV "/dev/stdout" /* just for glpk */
52
53
54 #define GRANUS 3
55
56 #define COUNTER_LIST                            \
57    CTR(commodities_loaded)                      \
58    CTR(trades_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)                       \
66    CTR(routes_valued)                           \
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];
74   COUNTER_LIST
75 #undef CTR
76 #undef CTRA
77
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;                             \
82     int sqr;                                                             \
83     if (DEBUGP(sql2)) fprintf(debug,"SQL %s", sql_must_call_string);     \
84     sqr= (call);                                                         \
85     if (DEBUGP(sql2)) fprintf(debug," = %d\n", sqr);                     \
86     if (sqr) sql_fatal("(unknown)", sqr, sql_must_call_string);          \
87   })                                                                     \
88
89 void sql_fatal(const char *stmt_what, int sqr, const char *act_what) NORET;
90
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);
94
95 #define SQL_DISTINCT_DECL(cols, nintcols)       \
96   int cols[nintcols];                           \
97   cols[0]= -1;
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
108     *
109     * Call SQL_DISTINCT_DECL, passing intcols = the total number of keys and
110     * consequences; it will declare  int cols[intfields];
111     *
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
117     * cols.
118     */
119
120 int sql_single_int(const char *stmt);
121
122 #define SQL_PREPARE(ss,stmt) ((ss)= sql_prepare((stmt),#ss))
123 sqlite3_stmt *sql_prepare(const char *stmt, const char *what);
124
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);
128
129 #define MAX_ROUTELEN 20
130
131 extern sqlite3 *db;
132
133 void setup_sql(const char *database);
134
135
136 typedef struct {
137   double distance_loss_factor;
138   struct TradesBlock *trades;
139   double route_tail_value;
140 } IslandPair;
141
142 IslandPair *ipair_get_maybe(int si, int di);
143
144 double value_route(int nislands, const int *islands, int exclude_arbitrage);
145 void setup_value(void);
146
147 #define AP 2 /* 0=absolute, 1=perleague */
148 #define A 0
149 #define P 1
150
151 typedef struct {
152   double value[AP];
153   int length;
154   int ports[MAX_ROUTELEN];
155 } OnePotentialResult;
156
157 typedef struct {
158   OnePotentialResult prs[AP];
159 } Bucket;
160
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]-> */);
165
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;
169
170 #define LOSS_FACTOR_PER_DELAY_SLOT (1-1e-8)
171
172 extern int islandtablesz;
173
174 extern int narches;
175 extern char **archnames;
176 extern int *islandid2arch;
177
178 extern int granusz_fin[GRANUS], granusz_mid[GRANUS];
179
180
181 extern FILE *output;
182
183
184 #define NEW(ptr) ((ptr)= mmalloc(sizeof(*ptr)))
185
186 #define MCALLOC(array, count) ((array)= mcalloc(sizeof(*(array)) * (count)))
187
188 #define MCALLOC_INITEACH(array, count, init_this) ({                   \
189     MCALLOC((array), (count));                                         \
190     int initi;                                                         \
191     typeof(&(array)[0]) this;                                          \
192     for (initi=0, this=(array); initi<(count); initi++, this++) {      \
193       init_this;                                                       \
194     }                                                                  \
195   })
196
197
198 typedef struct {
199   double value;
200   Bucket *bucket;
201 } HighScoreEntry;
202
203 extern int granus;
204 extern int nhighscores[GRANUS][AP];
205 extern HighScoreEntry *highscores[GRANUS][AP];
206
207
208 #define ONDEMAND(pointer_lvalue, calloc_size_count)                          \
209   ((pointer_lvalue) ? :                                                      \
210    ((pointer_lvalue) = mcalloc(sizeof(*(pointer_lvalue)) * calloc_size_count)))
211
212
213 static inline int isle2arch(int isle) {
214   int arch= islandid2arch[isle];
215   assert(arch>=0);
216   return arch;
217 }
218
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;
225   }
226   return archs[narchs/2];
227 }
228
229
230 #endif /*RSCOMMON_H*/