-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe.c
265 lines (211 loc) · 7.21 KB
/
recipe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* main body of a program to find the recipe leading to a given reaction */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <my_global.h>
#include <mysql.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "config.h"
#include "pattern.h"
#include "reaction.h"
#include "database.h"
#define SHIP_MAGIC_X 60 // TODO: currently magic value >= -offX for ALL parts
#define SHIP_MAGIC_SZ 9 // TODO: currently magic value ~ max (H(part) / part->cost)
#define PARTWIDTH 1200 // TODO: currently magic value > max (W(part))
#define PARTHEIGHT 1100 // TODO: currently magic value > max (H(part))
#define MAXPARTS 100
#define SELECT "SELECT reaction.rId, initial_tId, %u+gen-result_phase, period, bId, lane, initial_state, rephase, initial_phase, pId, cost, total_cost " \
"FROM transition LEFT JOIN reaction USING (rId) LEFT JOIN target ON (initial_tId = tId) "
// #define FIRST "WHERE rId = %llu ORDER BY cost DESC LIMIT 1"
// #define NEXT "WHERE result_tId = %llu AND (result_state+%u-lane_adj)%%%u = %u AND total_cost = %u ORDER BY cost DESC LIMIT 1"
#define FIRST "WHERE rId = %llu ORDER BY cost ASC LIMIT 1"
#define NEXT "WHERE result_tId = %llu AND (result_state+3*%u-lane_adj)%%%u = %u AND total_cost = %u ORDER BY cost ASC LIMIT 1"
typedef struct
{
ROWID rId, tId;
int phase;
int period;
ROWID bId;
int lane;
int state;
int rephase;
int delay;
part *p;
int cost;
int total_cost;
} trace;
static trace recipe [MAXPARTS];
static int n_recipe = 0;
static object *ships = NULL;
static part *parts = NULL;
static pattern constructor;
uint8_t mod (int value, int divisor)
// C operator % does not what we want, if LHS < 0
{
// Sanity. Avoid Division by zero and negative divisors:
// Plus: x mod 1 == 0 ...
assert (divisor > 0);
if (divisor <= 1)
return 0;
// C operator % does not what we want, if LHS < 0
while (value < 0)
value += divisor;
return value % divisor;
}
part *find_part (ROWID pId)
{
int p;
for (p = 0; parts [p].pId; p++)
if (parts [p].pId == pId)
return &parts [p];
return NULL;
}
trace *follow (const char *q)
{
trace *t;
if (mysql_query (con, q))
finish_with_error (con);
MYSQL_RES *result = mysql_store_result (con);
if (!result)
{
fprintf (stderr, "No result for '%s'\n", q);
exit (2);
}
MYSQL_ROW row = mysql_fetch_row (result);
if (!row)
{
fprintf (stderr, "No result for '%s'\n", q);
exit (2);
}
t = &recipe [n_recipe++];
t->rId = strtoull (row [0], NULL, 0);
t->tId = strtoull (row [1], NULL, 0);
t->phase = atoi (row [2]);
t->period = atoi (row [3]);
t->bId = strtoull (row [4], NULL, 0);
t->lane = atoi (row [5]);
t->state = atoi (row [6]);
t->rephase = atoi (row [7]);
t->delay = atoi (row [8]);
t->p = find_part (strtoull (row [9], NULL, 0));
t->cost = atoi (row [10]);
t->total_cost = atoi (row [11]);
mysql_free_result (result);
return t;
}
main (int argc, char **argv)
{
char query [4096];
int i, n, pos;
unsigned long stampY = 0UL;
trace *t;
part *track = NULL, *rephaser = NULL;
target tgt;
unsigned period;
config_load (argv [1]);
db_init ();
MAXWIDTH = PARTWIDTH;
MAXHEIGHT = PARTHEIGHT;
MAXGEN = MAXPERIOD*2;
MAX_RLE = 65536;
lab_allocate (MAXWIDTH, MAXHEIGHT, MAXGEN, MAX_FIND);
pat_allocate (&constructor, PARTWIDTH, 2*(MAXPARTS+1)*PARTHEIGHT);
// if this fails we need to redefine struct reaction in reaction.h
assert (LANES <= UINT8_MAX);
// load all standard ships so we can search for them ;)
ships = db_load_space_ships ();
// Load THE bullet used by all our parts. Mixing rakes with differnt bullets is NYI.
bullets = db_load_bullets_for (SHIPNAME);
assert (bullets [1].id == 0);
period = MAXPERIOD + mod (-MAXPERIOD,bullets[0].dt);
// load all parts we can use for our ship-to-build.
parts = db_load_parts_for (SHIPNAME, true, period, SHIP_MAGIC_X, abs (DY));
// There must be a part of type pt_track and another one of type pt_rephaser for this ship.
// TO DO: check for multiple versions ... Maybe different tracks would not make to much sense. Multiple rephasers DO.
for (n = 0; parts [n].pId; n++)
if (parts [n].type == pt_track)
track = &parts [n];
else if (parts [n].type == pt_rephaser)
rephaser = &parts [n];
assert (SHIPMODE);
assert (DX == track->dx);
assert (DY == -track->dy);
assert (track);
assert (track->dx == 0);
assert (track->dy > 0); // NYI: only northbound ships please
for (i = 2; i < argc; i++)
{
int delay = 0;
sprintf (query, SELECT FIRST, DT, strtoull (argv [i], NULL, 0));
n_recipe = 0;
t = follow (query);
while (t->total_cost > 0)
{
sprintf (query, SELECT NEXT, DT, t->tId, LANES, LANES, t->state, t->total_cost-t->cost);
t = follow (query);
}
db_target_reload (&tgt, t->tId, 0);
// Write recipe in the header.
printf ("#P 0 %lu\n", stampY);
for (n = n_recipe-1; n >= 0; n--)
{
if (n == n_recipe-1)
delay = mod (recipe [n].delay, recipe [n].period);
else
{
fprintf (stderr, "delay = mod (%d - %d - %d, %d) =", recipe [n+1].phase, delay, recipe [n].delay, recipe [n].period);
delay = mod (recipe [n+1].phase - delay - recipe [n].delay, recipe [n].period);
fprintf (stderr, "%d\n", delay);
}
recipe [n].delay = delay;
printf ("#C [%llu]: ", recipe [n].rId);
if (recipe [n].rephase)
printf ("REPHASE BY %u LANES, ", recipe [n].rephase);
if (delay)
printf ("DELAY %d TICKS, ", delay);
printf ("FIRE %s\n", recipe [n].p->name);
}
// Construct demo.
pat_init (&constructor);
pos = 0;
for (n = 0; n < (recipe [0].total_cost+5)*SHIP_MAGIC_SZ; n++)
{
pat_add (&constructor, track->pats [0].X, pos+track->pats [0].Y, track->pats [0].pat);
pos += track->dy;
}
for (n = n_recipe-1; n >= 0; n--)
{
int phase = recipe [n].rephase;
while (phase != 0)
{
pat_add (&constructor, rephaser->pats [0].X, pos+rephaser->pats [0].Y, rephaser->pats [0].pat);
pos += rephaser->dy;
phase = (LANES + phase - rephaser->lane_adjust) % LANES;
}
pat_add (&constructor, track->pats [0].X, pos+track->pats [0].Y, track->pats [0].pat);
pos += track->dy;
part *p = recipe [n].p;
target *pt = &p->pats [period - recipe [n].delay];
pat_add (&constructor, pt->X, pos+pt->Y, pt->pat);
if (n == n_recipe-1)
{
int tgtX = p->fireX + (period*bullets [0].dx/bullets [0].dt) - bullets [0].base_x - (recipe [n].lane*bullets [0].lane_dx) + tgt.X-tgt.left;
int tgtY = pos+p->fireY + (period*bullets [0].dy/bullets [0].dt) - bullets [0].base_y - (recipe [n].lane*bullets [0].lane_dy) + tgt.Y-tgt.bottom;
for (; tgtY > 0; tgtY += DY)
pat_add (&constructor, tgtX, tgtY, tgt.pat);
}
pos += p->dy;
/*
pat_add (&constructor, track->pats [0].X, pos+track->pats [0].Y, track->pats [0].pat);
pos += track->dy;
pat_add (&constructor, track->pats [0].X, pos+track->pats [0].Y, track->pats [0].pat);
pos += track->dy;
*/
}
// ... and show it.
pat_dump (&constructor, false);
stampY += 1000UL * (2UL + (unsigned long)pos / 1000UL);
}
}