chiark / gitweb /
WIP improve intro etc. - howto page from Naath
[ypp-sc-tools.db-test.git] / yarrg / rsmain.c
1 /*
2  * Route searcher - command line parsing and result printing
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 #include "rscommon.h"
29
30 #include <ctype.h>
31
32 int o_quiet= 0;
33 double max_mass=-1, max_volu=-1, max_capi=-1;
34 double distance_loss_factor_per_league;
35 int max_dist=-1, min_trade_maxprofit=0;
36
37 FILE *debug_file;
38 FILE *output;
39
40 #define tabdebugf printf
41
42
43 #define CTR(x)    int ctr_##x;
44 #define CTRA(x,n) int ctr_##x[n];
45   COUNTER_LIST
46 #undef CTR
47 #undef CTRA
48
49 static Bucket ****results[GRANUS];
50   /* results[GRANUS][start_isle_ix][finalisle][midisle]-> */
51
52 static pid_t debugoutpid;
53
54 int main(int argc, const char **argv) {
55   const char *arg;
56   int i, ap;
57   int granui;
58   const char *database=0;
59   const char *concur_base=0, *concur_rhs=0;
60   int concur_lim=-1;
61
62 #ifndef debug_flags
63   debug_flags= ~( dbg_sql2 );
64 #endif
65
66   for (;;) {
67     arg= *++argv;
68     if (arg[0] != '-') break;
69     if (!strcmp(arg,"-d")) {
70       database= *++argv;
71     } else if (!strcmp(arg,"-C")) {
72       concur_base= *++argv;
73       concur_rhs= *++argv;
74       concur_lim= atoi(*++argv);
75     } else if (!strcmp(arg,"-g")) {
76       granus= atoi(*++argv);
77       assert(granus>=1 && granus<=GRANUS);
78 #ifndef debug_flags
79     } else if (!strcmp(arg,"-DN")) {
80       debug_flags= 0;
81     } else if (!strcmp(arg,"-D1")) {
82       debug_flags= ~(dbg_sql2|dbg_lp|dbg_value2);
83 #endif
84     } else {
85       abort();
86     }
87   }
88
89   if (debug_flags) {
90     /* glpk insists on writing stuff to stdout, and it does buffering,
91      * so we route all our debug through it this too */
92     int realstdout;
93     sysassert( (realstdout= dup(1)) > 2 );
94     sysassert( output= fdopen(realstdout,"w") );
95
96     int pfd[2];
97     sysassert(! pipe(pfd) );
98     sysassert( (debugoutpid= fork()) >=0 );
99     if (!debugoutpid) {
100       sysassert( dup2(pfd[0],0)==0 );
101       sysassert( dup2(2,1)==1 );
102       sysassert(! close(pfd[0]) );
103       sysassert(! close(pfd[1]) );
104       sysassert(! execlp("cat","cat",(char*)0) );
105     }
106     sysassert( dup2(pfd[1],1)==1 );
107     sysassert(! close(pfd[0]) );
108     sysassert(! close(pfd[1]) );
109
110     debug_file= stdout;
111   } else {
112     output= stdout;
113     debug_file= stderr;
114   }
115
116   sysassert( !setvbuf(debug,0,_IOLBF,0) );
117
118   max_mass= atof(*argv++);
119   max_volu= atof(*argv++);
120   max_capi= atof(*argv++);
121   double loss_per_league= atof(*argv++);
122   distance_loss_factor_per_league= 1.0 - loss_per_league;
123
124   min_trade_maxprofit= atoi(*argv++);
125
126   if (concur_base) {
127     for (i=0; i<concur_lim; i++) {
128       char *concfn= masprintf("%s%02d%s", concur_base, i, concur_rhs);
129       int concfd, r;
130       sysassert( (concfd= open(concfn, O_RDWR|O_CREAT|O_TRUNC, 0600)) >= 0);
131       struct flock fl;
132       memset(&fl,0,sizeof(fl));
133       fl.l_type= F_WRLCK;
134       r= fcntl(concfd, F_SETLK, &fl);
135       free(concfn);
136       if (!r) goto concur_ok;
137       sysassert( errno == EWOULDBLOCK );
138       close(concfd);
139     }
140     fprintf(output,"@@@ concurrency limit exceeded (%d)\n", concur_lim);
141     exit(0);
142
143   concur_ok:
144     /* deliberately leak concfd */
145     fprintf(output,"concurrency slot %d\n", i);
146   }
147
148   setup_sql(database);
149   setup_value();
150   setup_search();
151
152   for (i=0; i<narches; i++)
153     fprintf(output,"arch %d %s\n",i,archnames[i]);
154   fprintf(output,"setup complete, starting search\n");
155
156   arg= *argv++;
157   if (!strcmp(arg,"specific")) {
158     int ia[argc], ni=0;
159     while ((arg= *argv++))
160       ia[ni++]= atoi(arg);
161
162     double val= value_route(ni, ia, 0);
163     fprintf(output, "route value is %g\n", val);
164   } else if (!strcmp(arg,"search")) {
165     for (granui=0; granui<GRANUS; granui++)
166       MCALLOC(results[granui], argc);
167
168     max_dist= atoi(*argv++);
169
170     for (ap=0; ap<AP; ap++) {
171       int nhs= atoi(*argv++);
172       for (granui=0; granui<GRANUS; granui++) {
173         nhighscores[granui][ap]= nhs;
174         MCALLOC(highscores[granui][ap], nhs);
175       }
176     }
177     const char *final_isle_spec= *argv++;
178
179     int resultsix= 0;
180     while ((arg= argv[resultsix])) {
181       int init_isle= atoi(arg);
182
183       int final_isle;
184       if (!strcmp(final_isle_spec,"circ")) final_isle= init_isle;
185       else if (!strcmp(final_isle_spec,"any")) final_isle= -1;
186       else final_isle= atoi(final_isle_spec);
187       assert(final_isle);
188
189       Bucket ****buckets_base_io[GRANUS];
190       for (granui=0; granui<GRANUS; granui++)
191         buckets_base_io[granui]= &results[granui][resultsix];
192         
193       search(init_isle, final_isle, buckets_base_io);
194       resultsix++;
195     }
196
197     int mid, fin;
198     for (granui=0; granui<granus; granui++) {
199       fprintf(output,"\n");
200       for (i=0; i<resultsix; i++) {
201         tabdebugf("========== start #%d granui%d %s ==========\n",
202                   i, granui, argv[i]);
203         Bucket ***buckets_resultsix= results[granui][i];
204         if (!buckets_resultsix) continue;
205         tabdebugf("    ");
206         for (mid=0; mid<granusz_mid[granui]; mid++) {
207           tabdebugf("|   m%-3d   ",mid);
208         }
209         tabdebugf("\n");
210         for (fin=0; fin<granusz_fin[granui]; fin++) {
211           Bucket **buckets_fin= buckets_resultsix[fin];
212           if (!buckets_fin) continue;
213           tabdebugf("f%-3d",fin);
214           for (mid=0; mid<granusz_mid[granui]; mid++) {
215             Bucket *result= buckets_fin[mid];
216             if (!result) {
217               tabdebugf("|          ");
218             } else {
219               tabdebugf("|%5d",(int)(result->prs[A].value[A]));
220               tabdebugf(" ");
221               tabdebugf("%4d",(int)(result->prs[P].value[P]));
222             }
223           }
224           tabdebugf("\n");
225         }
226       } /* i */
227
228       for (ap=0; ap<AP; ap++) {
229         int pos;
230         fprintf(output,"============== granui%d ap=%d ==============\n",
231                 granui, ap);
232         for (pos=nhighscores[granui][ap]-1; pos>=0; pos--) {
233           HighScoreEntry *hs= &highscores[granui][ap][pos];
234           Bucket *bucket= hs->bucket;
235           if (!bucket) continue;
236           OnePotentialResult *pr= &bucket->prs[ap];
237           const int *const ports= pr->ports;
238           int nports;
239           for (nports=0; nports<MAX_ROUTELEN && ports[nports]>=0; nports++);
240           int finisle= ports[nports-1];
241           int finarch= isle2arch(finisle);
242           int midisle= ports[nports/2];
243           int midarch= route2midarch(ports,nports);
244           fprintf(output,
245                   " @%2d %c#%2d | start%3d mid%d:%3d f%d:%3d"
246                   " | %3dlg | %5d %5d %4d |",
247                   pos, "ap"[ap], nhighscores[granui][ap] - pos,
248                   ports[0], midarch,midisle, finarch,finisle, pr->length,
249                   (int)hs->value, (int)pr->value[A], (int)pr->value[P]);
250           for (i=0; i<nports; i++) fprintf(output," %d",ports[i]);
251           fprintf(output,"\n");
252         } /* pos */
253       } /* ap */
254     } /* granui */
255     fprintf(output,"\n");
256
257   } else {
258     abort();
259   }
260
261 #define CTR(x) fprintf(output,"  %-30s %10d\n",#x,ctr_##x);
262 #define CTRA(x,n) for (i=0;i<n;i++) \
263   fprintf(output,"  %-27s[%d] %10d\n",#x,i,ctr_##x[i]);
264   COUNTER_LIST
265 #undef CTR
266
267   if (debug_flags) {
268     sysassert(! fclose(debug) );
269     waitpid_check_exitstatus(debugoutpid,"debug cat",1);
270   }
271   sysassert(! fclose(output) );
272
273   return 0;
274 }