hyperrogue/racing.cpp

1465 lines
40 KiB
C++
Raw Normal View History

// Hyperbolic Rogue - Racing
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
2018-11-17 18:30:50 +00:00
/** \file racing.cpp
* \brief racing mode
*/
2018-11-17 18:30:50 +00:00
#include "hyper.h"
2018-11-17 18:30:50 +00:00
namespace hr {
2020-03-27 20:47:09 +00:00
/** \brief Racing mode */
2019-08-09 19:00:52 +00:00
EX namespace racing {
2018-11-17 18:30:50 +00:00
2019-09-13 01:10:26 +00:00
#if CAP_RACING
void set_race_configurer();
2019-08-09 19:00:52 +00:00
EX bool guiding = false;
2019-01-07 03:53:09 +00:00
2019-08-09 19:00:52 +00:00
EX bool on;
EX bool player_relative = false;
EX bool standard_centering = false;
EX bool track_ready;
2022-06-16 21:10:44 +00:00
EX bool official_race = false;
2019-01-11 01:19:28 +00:00
int TWIDTH;
2019-08-09 19:00:52 +00:00
EX ld race_advance = 0;
2018-12-23 13:01:07 +00:00
static constexpr int LENGTH = 250;
static constexpr int DROP = 1;
2018-11-17 18:30:50 +00:00
2019-08-09 19:00:52 +00:00
EX int ghosts_to_show = 5;
EX int ghosts_to_save = 10;
struct race_cellinfo {
cell *c;
int from_track;
int completion;
2018-12-23 12:11:30 +00:00
int from_start, from_goal;
};
vector<race_cellinfo> rti;
2019-08-09 19:00:52 +00:00
EX vector<cell*> track;
map<cell*, int> rti_id;
2018-11-17 18:30:50 +00:00
EX int trophy[MAXPLAYER];
2019-01-07 03:55:21 +00:00
transmatrix straight;
2018-11-25 22:39:47 +00:00
int race_try;
2019-08-09 19:00:52 +00:00
EX int race_start_tick, race_finish_tick[MAXPLAYER];
2018-11-24 02:26:07 +00:00
2018-11-26 22:17:43 +00:00
typedef unsigned char uchar;
uchar frac_to_uchar(ld x) { return uchar(x * 256); }
uchar angle_to_uchar(ld x) { return frac_to_uchar(x / TAU); }
2018-11-26 22:17:43 +00:00
ld uchar_to_frac(uchar x) { return x / 256.; }
transmatrix spin_uchar(uchar x) { return spin(uchar_to_frac(x) * TAU); }
2018-11-26 22:17:43 +00:00
static constexpr ld distance_multiplier = 4;
2018-11-26 22:17:43 +00:00
2018-11-24 02:26:07 +00:00
struct ghostmoment {
2022-06-16 21:10:44 +00:00
int step;
cell *where_cell;
2018-11-26 22:17:43 +00:00
uchar alpha, distance, beta, footphase;
2018-11-24 02:26:07 +00:00
};
struct ghost {
charstyle cs;
int result;
int checksum;
long long timestamp;
vector<ghostmoment> history;
};
2022-06-16 21:10:44 +00:00
vector<ghost> ghostset;
2019-01-11 01:19:28 +00:00
array<vector<ghostmoment>, MAXPLAYER> current_history;
2018-11-24 02:26:07 +00:00
2022-06-16 21:10:44 +00:00
EX map<eLand, int> best_scores;
2022-08-07 00:16:40 +00:00
EX map<eLand, int> best_scores_to_save;
2022-06-16 21:10:44 +00:00
2018-11-24 20:24:27 +00:00
string ghost_prefix = "default";
2022-08-14 17:25:10 +00:00
#if CAP_FILES && CAP_EDIT
2018-11-24 20:24:27 +00:00
void hread(hstream& hs, ghostmoment& m) {
2022-06-16 21:10:44 +00:00
int id;
hread(hs, m.step, id, m.alpha, m.distance, m.beta, m.footphase);
if(id < 0 || id >= isize(mapstream::cellbyid)) throw hr_exception("error reading a ghost moment");
2022-06-16 21:10:44 +00:00
m.where_cell = mapstream::cellbyid[id];
2018-11-24 20:24:27 +00:00
}
void hwrite(hstream& hs, const ghostmoment& m) {
2022-06-16 21:10:44 +00:00
int id = mapstream::cellids[m.where_cell];
hwrite(hs, m.step, id, m.alpha, m.distance, m.beta, m.footphase);
2018-11-24 20:24:27 +00:00
}
void hread(hstream& hs, ghost& gh) {
hread(hs, gh.cs, gh.result, gh.timestamp, gh.checksum, gh.history);
2018-11-24 20:24:27 +00:00
}
void hwrite(hstream& hs, const ghost& gh) {
hwrite(hs, gh.cs, gh.result, gh.timestamp, gh.checksum, gh.history);
2018-11-24 20:24:27 +00:00
}
2022-06-16 21:10:44 +00:00
EX void save_ghosts(hstream& f) {
hwrite(f, ghostset);
2018-11-24 20:24:27 +00:00
}
2022-06-16 21:10:44 +00:00
EX void load_ghosts(hstream& f) {
hread(f, ghostset);
2018-11-24 20:24:27 +00:00
}
2022-06-16 21:10:44 +00:00
2019-02-17 17:58:49 +00:00
#endif
2018-11-24 20:24:27 +00:00
shiftmatrix get_ghostmoment_matrix(ghostmoment& p) {
2022-06-16 21:10:44 +00:00
cell *w = p.where_cell;
2019-01-07 03:56:06 +00:00
transmatrix T = spin_uchar(p.alpha) * xpush(uchar_to_frac(p.distance) * distance_multiplier) * spin_uchar(p.beta);
return gmatrix[w] * T;
}
2018-11-24 00:30:05 +00:00
void fix_cave(cell *c) {
int v = 0;
// if(c->wall == waCavewall) v++;
// if(c->wall == waCavefloor) v--;
forCellEx(c2, c) {
if(c2->wall == waCavewall) v++;
if(c2->wall == waCavefloor) v--;
}
else v--;
if(v>0 && c->wall == waCavefloor) c->wall = waCavewall;
if(v<0 && c->wall == waCavewall) c->wall = waCavefloor;
}
2019-10-04 12:22:19 +00:00
bool keep_to_crossroads() {
2022-12-08 18:38:06 +00:00
return specialland == laCrossroads && !(nonisotropic || mhybrid);
2019-10-04 12:22:19 +00:00
}
2018-11-23 01:58:16 +00:00
bool bad(cell *c2, cell *c) {
2018-11-24 00:30:05 +00:00
if(c2->land == laCaves) {
forCellEx(c3, c2) fix_cave(c3);
fix_cave(c2);
}
2018-11-23 01:58:16 +00:00
if(!passable(c2, c, P_ISPLAYER)) return true;
2019-10-04 12:22:19 +00:00
if((c2->land == laCrossroads) ^ (c->land == laCrossroads) && keep_to_crossroads()) return true;
2018-11-23 01:58:16 +00:00
return false;
}
2018-12-04 20:17:57 +00:00
int rcelldist(cell *c) {
2019-02-27 14:53:07 +00:00
#if CAP_CRYSTAL
if(cryst) return crystal::space_distance(c, currentmap->gamestart());
2019-02-27 14:53:07 +00:00
#endif
return celldist(c);
2018-12-04 20:17:57 +00:00
}
int pcelldist(cell *c) {
2019-02-27 14:53:07 +00:00
#if CAP_CRYSTAL
if(cryst) return crystal::precise_distance(c, currentmap->gamestart());
2019-02-27 14:53:07 +00:00
#endif
2019-09-27 16:18:50 +00:00
if(sl2) return PIU(celldist(c));
2019-02-27 14:53:07 +00:00
return celldist(c);
2018-12-04 20:17:57 +00:00
}
2018-11-17 18:30:50 +00:00
int trackval(cell *c) {
2018-12-04 20:17:57 +00:00
int v = rcelldist(c);
2018-11-17 18:30:50 +00:00
int bonus = 0;
2019-10-04 12:22:19 +00:00
if(c->land != laCrossroads || !keep_to_crossroads())
2018-11-17 18:30:50 +00:00
forCellEx(c2, c) {
2018-12-04 20:17:57 +00:00
int d = rcelldist(c2) - v;
2018-11-23 01:58:16 +00:00
if(d < 0 && bad(c2, c))
2018-11-17 18:30:50 +00:00
bonus += 2;
2018-11-23 01:58:16 +00:00
if(d == 0 && bad(c2, c))
2018-11-17 18:30:50 +00:00
bonus ++;
}
return v + bonus;
}
void tie_info(cell *c, int from_track, int comp) {
rti_id[c] = isize(rti);
2018-12-23 12:11:30 +00:00
rti.emplace_back(race_cellinfo{c, from_track, comp, -1, -1});
}
race_cellinfo& get_info(cell *c) {
return rti[rti_id.at(c)];
}
2018-11-25 22:39:47 +00:00
ld start_line_width;
struct hr_track_failure : hr_exception {};
int length;
2019-11-09 17:03:32 +00:00
bool use_exhaustive_distance;
2019-08-03 09:37:22 +00:00
void find_track(cell *start, int sign, int len) {
int dl = 7 - getDistLimit() - genrange_bonus;
2019-12-06 11:15:04 +00:00
dl = (8 + dl) / 2;
if(WDIM == 3 && dl < 6) dl = 6;
2019-08-03 09:37:22 +00:00
cell *goal;
map<cell*, cell*> parent;
map<int, vector<cell*> > cellbydist;
cellbydist[0].push_back(start);
int traversed = 0;
while(true) {
traversed++;
if(cellbydist.empty()) {
println(hlog, "reset after traversing ", traversed, " width = ", TWIDTH, " length = ", length);
throw hr_track_failure();
2019-08-03 09:37:22 +00:00
}
auto it = cellbydist.end();
it--;
// if(hrand(100) < 85 && it != cellbydist.begin()) it--;
auto& v = it->second;
if(v.empty()) { cellbydist.erase(it); continue; }
int id = hrand(isize(v));
cell *c = v[id];
v[id] = v.back(); v.pop_back();
if(it->first >= length) {
2019-08-03 09:37:22 +00:00
goal = c;
break;
}
2019-12-06 11:15:04 +00:00
setdist(c, dl, parent[c]);
2019-08-03 09:37:22 +00:00
forCellEx(c1, c) if(!bad(c1, c) && !parent.count(c1)) {
parent[c1] = c;
int id;
/* if(nil && !nilv::nilperiod[0]) {
switch(sign) {
2019-09-27 16:04:19 +00:00
case 1: id = c1->master->zebraval - start->master->zebraval; break;
case 2: id = start->master->emeraldval - c1->master->emeraldval; break;
case 3: id = start->master->zebraval - c1->master->zebraval; break;
case 4: id = start->master->emeraldval - c1->master->emeraldval; break;
}
} */
2019-11-15 01:36:14 +00:00
if(use_exhaustive_distance)
id = trackval(c1);
else if(nil && !quotient) {
switch(sign) {
case 1: id = c1->master->emeraldval - start->master->emeraldval; break;
case 2: case 3: id = start->master->fieldval - c1->master->fieldval; break;
case 4: id = start->master->emeraldval - c1->master->emeraldval; break;
}
2019-09-27 16:04:19 +00:00
}
#if CAP_SOLV
2019-11-09 17:03:32 +00:00
else if(asonov::in() && asonov::period_z) {
2019-11-15 01:19:53 +00:00
auto co = asonov::get_coord(c->master);
ld x = szgmod(co[0], asonov::period_xy);
ld y = szgmod(co[1], asonov::period_xy);
2019-11-09 17:03:32 +00:00
ld z = hypot(x, y);
2019-11-15 01:19:53 +00:00
ld maxz = asonov::period_xy ? asonov::period_xy/3 : 1000000000;
id = int((log(z) / log(maxz)) * length);
2019-11-09 17:03:32 +00:00
}
else if(sn::in())
2019-08-03 09:37:22 +00:00
id = (start->master->distance - c1->master->distance) * sign;
#endif
2019-08-03 09:37:22 +00:00
else
id = trackval(c1);
cellbydist[id].push_back(c1);
}
}
2019-11-09 17:03:32 +00:00
if(use_exhaustive_distance) permanent_long_distances(goal);
2019-08-03 09:37:22 +00:00
if(nonisotropic) {
2019-08-03 09:37:22 +00:00
vector<cell*> p;
while(goal != start) p.push_back(goal), goal = parent[goal];
while(!p.empty()) track.push_back(p.back()), p.pop_back();
}
else
track = build_shortest_path(start, goal);
}
2019-09-28 12:18:58 +00:00
EX void block_cells(vector<cell*> to_block, function<bool(cell*)> blockbound) {
2021-07-12 03:54:25 +00:00
hrandom_shuffle(to_block);
2019-09-28 12:18:58 +00:00
for(cell *c: to_block) switch(specialland) {
case laIce:
c->wall = waIcewall;
break;
case laHell:
c->wall = waSulphur;
break;
case laJungle: {
vector<int> dirs;
forCellIdEx(c2, i, c) if(among(c2->monst, moIvyRoot, moIvyWait)) dirs.push_back(i);
if(dirs.empty()) c->monst = moIvyRoot;
else c->monst = moIvyWait, c->mondir = dirs[hrand(isize(dirs))];
break;
}
case laDeadCaves:
if(blockbound(c)) c->wall = waDeadwall;
break;
case laRedRock:
if(blockbound(c)) c->wall = waRed3;
break;
case laDragon:
c->wall = waChasm;
break;
case laDryForest:
if(blockbound(c)) c->wall = waBigTree;
break;
case laDesert:
if(blockbound(c)) c->wall = waDune;
break;
case laRuins:
if(blockbound(c)) c->wall = waRuinWall;
break;
case laElementalWall:
if(blockbound(c)) {
if(c->land == laEFire) c->wall = waEternalFire;
else if(c->land == laEWater) c->wall = waSea;
else if(c->land == laEAir) c->wall = waChasm;
else if(c->land == laEEarth) c->wall = waStone;
}
break;
default: break;
}
}
EX void make_bounded_track(cell *s) {
2019-09-28 12:57:57 +00:00
celllister cl(s, 999, 1000000, NULL);
for(cell *c: cl.lst)
setdist(c, 0, NULL);
println(hlog, "cls = ", isize(cl.lst));
for(cell *c: cl.lst) {
2019-09-28 13:06:08 +00:00
c->item = itNone; c->monst = moNone;
if(c->land != laAsteroids) c->wall = waNone;
2019-09-28 12:57:57 +00:00
}
2019-09-28 12:18:58 +00:00
map<cell*, int> mazetype;
track.clear();
track.push_back(s);
while(true) {
cell *last = track.back();
mazetype[last] = 1;
vector<cell*> choices;
forCellCM(c1, last) {
if(mazetype[c1] != 0) continue;
choices.push_back(c1);
}
if(choices.empty()) break;
cell *nxt = choices[hrand(isize(choices))];
for(cell *cc: choices) mazetype[cc] = 2;
track.push_back(nxt);
}
vector<cell*> to_block;
for(auto p: mazetype) {
2019-09-28 12:57:57 +00:00
if(p.second == 2) to_block.push_back(p.first);
2019-09-28 12:18:58 +00:00
}
block_cells(to_block, [] (cell *c) { return true; });
2019-09-28 13:06:08 +00:00
for(cell *c: to_block) if(among(c->wall, waNone, waInvisibleFloor) && !c->monst) c->wall = waBarrier;
2019-09-28 12:18:58 +00:00
}
EX bool bounded_track;
2019-08-09 19:00:52 +00:00
EX void generate_track() {
2018-11-17 18:30:50 +00:00
2022-06-16 21:10:44 +00:00
official_race = false;
ghostset.clear();
TWIDTH = getDistLimit() - 1;
if(TWIDTH == 1) TWIDTH = 2;
2019-12-06 11:15:04 +00:00
if(sl2) TWIDTH = 2;
TWIDTH += race_try / 8;
2019-09-28 12:18:58 +00:00
2018-11-17 18:30:50 +00:00
track.clear();
/*
int t = -1;
bignum full_id;
bool onlychild = true;
*/
cell *s = currentmap->gamestart();
2018-11-25 22:39:47 +00:00
if(specialland == laCrossroads) {
celllister cl(s, TWIDTH, 1000000, NULL);
for(cell *c: cl.lst) c->bardir = NOBARRIERS;
}
2019-02-27 14:53:22 +00:00
int dl = 7 - getDistLimit() - genrange_bonus;
2018-11-25 22:39:47 +00:00
2018-11-17 18:30:50 +00:00
setdist(s, 6, NULL);
makeEmpty(s);
cview(); // needed for some virtualRebases
use_exhaustive_distance = exhaustive_distance_appropriate();
2019-11-15 01:36:14 +00:00
if(use_exhaustive_distance)
permanent_long_distances(s);
2019-09-28 12:18:58 +00:00
bounded_track = false;
length = LENGTH;
if(WDIM == 3 || weirdhyperbolic) length = max(length - 10 * race_try, 10);
2019-11-15 01:36:14 +00:00
if(use_exhaustive_distance) {
int maxsd = max_saved_distance(s);
2019-11-15 01:36:14 +00:00
println(hlog, "max length = ", maxsd);
length = min(length, maxsd * 5/6);
}
2019-11-02 21:31:29 +00:00
try {
2022-12-08 18:38:06 +00:00
if(closed_or_bounded && !mproduct && !(cgflags & qHUGE_BOUNDED)) {
bounded_track = true;
make_bounded_track(s);
}
2019-11-15 01:36:14 +00:00
else if(use_exhaustive_distance) {
find_track(s, 0, length);
}
else if(asonov::in()) {
2019-11-09 17:03:32 +00:00
find_track(s, 0, length);
}
else if(sol && !asonov::in()) {
2019-08-03 09:37:22 +00:00
track.push_back(s);
find_track(s, 1, length/4);
find_track(track.back(), -1, length-2*(length/4));
find_track(track.back(), 1, length/4);
2019-08-03 09:37:22 +00:00
}
2019-10-05 11:59:27 +00:00
else if(nih) {
track.push_back(s);
find_track(s, 1, length/2);
find_track(track.back(), -1, length/2);
2019-10-05 11:59:27 +00:00
}
2019-09-27 16:04:19 +00:00
else if(nil) {
track.push_back(s);
find_track(s, 1, length/4);
find_track(track.back(), 2, length/4);
find_track(track.back(), 3, length/4);
find_track(track.back(), 4, length/4);
2019-09-27 16:04:19 +00:00
}
else find_track(s, 0, length);
}
2021-03-21 21:56:38 +00:00
catch(const hr_track_failure&) {
race_try++;
gamegen_failure = true;
return;
}
2021-03-21 21:56:38 +00:00
catch(const hr_shortest_path_exception&) {
addMessage("error: could not build path");
gamegen_failure = true;
racing::on = false;
return;
}
2018-11-17 18:30:50 +00:00
2019-12-06 11:15:04 +00:00
if(WDIM == 3) dl = 7 - TWIDTH;
2019-02-27 14:53:22 +00:00
for(cell *c:track) setdist(c, dl, NULL);
2018-11-17 18:30:50 +00:00
2022-06-16 21:10:44 +00:00
configure_track(true);
}
EX vector<cell*> reachable_goals;
EX void restore_goals() {
for(auto c: reachable_goals)
c->wall = pick(waCloud, waMirror);
}
EX void configure_track(bool gen) {
cell *s = track[0];
2019-09-28 12:18:58 +00:00
if(true) {
2018-11-18 00:42:17 +00:00
manual_celllister cl;
2022-06-16 21:10:44 +00:00
rti.clear();
rti_id.clear();
2018-11-17 18:30:50 +00:00
for(int i=0; i<isize(track); i++) {
tie_info(track[i], 0, i);
2018-11-18 00:42:17 +00:00
cl.add(track[i]);
}
int win = isize(track) - DROP;
2018-11-18 00:42:17 +00:00
for(int i=0; i<isize(cl.lst); i++) {
cell *c = cl.lst[i];
auto p = get_info(c);
forCellEx(c2, c) if(!rti_id.count(c2)) {
tie_info(c2, p.from_track+1, p.completion);
2018-11-18 00:42:17 +00:00
cl.add(c2);
}
2022-06-16 21:10:44 +00:00
if(bounded_track || !gen) continue;
2018-11-18 00:42:17 +00:00
c->item = itNone;
if(c->wall == waMirror || c->wall == waCloud) c->wall = waNone;
if(!isIvy(c))
c->monst = moNone;
2018-11-24 00:30:05 +00:00
if(c->monst == moIvyHead) c->monst = moIvyWait;
2018-11-18 00:42:17 +00:00
if(inmirror(c->land))
;
else if(p.from_track == TWIDTH) {
2018-11-24 00:30:05 +00:00
killMonster(c, moNone, 0);
c->wall = waBarrier;
2018-11-18 00:42:17 +00:00
c->land = laBarrier;
2018-11-24 00:30:05 +00:00
}
else if(p.from_track > TWIDTH) {
2018-11-24 00:30:05 +00:00
killMonster(c, moNone, 0);
c->land = laMemory;
2018-11-18 00:42:17 +00:00
c->wall = waChasm;
2018-11-24 00:30:05 +00:00
}
if(p.completion >= win && p.from_track < TWIDTH) {
2018-11-24 00:30:05 +00:00
c->wall = hrand(2) ? waMirror : waCloud;
killMonster(c, moNone, 0);
}
2018-11-17 18:30:50 +00:00
}
}
int byat[65536];
2018-11-17 18:30:50 +00:00
for(int a=0; a<16; a++) byat[a] = 0;
for(const auto s: rti) byat[s.from_track]++;
2018-11-17 18:30:50 +00:00
for(int a=0; a<16; a++) printf("%d: %d\n", a, byat[a]);
2022-06-16 21:10:44 +00:00
if(gen) if(s->land == laCaves || (s->land == laCrossroads && !keep_to_crossroads())) {
2018-11-24 00:30:05 +00:00
set<unsigned> hash;
while(true) {
unsigned hashval = 7;
int id = 0;
for(auto s: rti) {
fix_cave(s.c);
if(s.c->wall == waCavewall)
2018-11-24 00:30:05 +00:00
hashval = (3+2*(id++)) * hashval + 1;
if(s.c->wall == waCavefloor)
2018-11-24 00:30:05 +00:00
hashval = (3+2*(id++)) * hashval + 2;
}
printf("hashval = %x id = %d\n", hashval, id);
if(hash.count(hashval)) break;
hash.insert(hashval);
}
}
2018-11-25 22:39:47 +00:00
2019-09-28 12:18:58 +00:00
if(!bounded_track) for(cell *sc: track) {
2018-11-25 22:39:47 +00:00
straight = calc_relative_matrix(sc, track[0], C0);
2019-08-15 13:05:43 +00:00
if(straight[GDIM][GDIM] > 1e8) break;
2018-11-25 22:39:47 +00:00
}
straight = rspintox(straight * C0);
ld& a = start_line_width;
2019-09-28 12:18:58 +00:00
if(WDIM == 2 && !bounded_track) for(a=0; a<10; a += .1) {
2018-11-25 22:39:47 +00:00
hyperpoint h = straight * parabolic1(a) * C0;
cell *at = s;
2019-11-14 18:33:55 +00:00
virtualRebase(at, h);
2022-06-16 21:10:44 +00:00
if(!rti_id.count(at)) break;
if(at->land == laMemory) break;
2018-11-25 22:39:47 +00:00
}
2022-06-16 21:10:44 +00:00
if(gen) if(WDIM == 2 && !bounded_track) for(ld cleaner=0; cleaner<a*.75; cleaner += .2) for(int dir=-1; dir<=1; dir+=2) {
2018-11-25 22:39:47 +00:00
transmatrix T = straight * parabolic1(cleaner * dir);
cell *at = s;
2019-11-14 18:33:55 +00:00
virtualRebase(at, T);
2018-12-23 12:11:30 +00:00
get_info(at).from_start = 0;
2018-11-25 22:39:47 +00:00
for(ld u=0; u<50; u++) {
if(at->wall != waBarrier)
makeEmpty(at);
killMonster(at, moNone, 0);
T = T * xpush(.1);
2019-11-14 18:33:55 +00:00
virtualRebase(at, T);
2018-11-25 22:39:47 +00:00
}
}
2022-06-16 21:10:44 +00:00
if(gen) for(auto s: rti) if(s.c->monst == moIvyDead) s.c->monst = moNone;
2018-11-25 22:39:47 +00:00
for(int i=0; i<motypes; i++) kills[i] = 0;
2019-01-07 03:55:21 +00:00
2022-06-16 21:10:44 +00:00
if(gen && bounded_track) track.back()->wall = waCloud;
2019-09-28 12:18:58 +00:00
2018-11-18 00:42:17 +00:00
if(1) {
manual_celllister cl;
cl.add(s);
for(int i=0; i<isize(cl.lst); i++) {
cell *c = cl.lst[i];
forCellEx(c2, c) {
2022-06-16 21:10:44 +00:00
if(among(c2->wall, waCloud, waMirror)) reachable_goals.push_back(c2);
if(passable(c2, c, P_ISPLAYER))
2018-11-18 00:42:17 +00:00
cl.add(c2);
}
}
2022-06-16 21:10:44 +00:00
if(reachable_goals.empty()) {
2018-11-18 00:42:17 +00:00
printf("error: goal unreachable\n");
2019-09-28 13:06:08 +00:00
gamegen_failure = true;
race_try++;
return;
2018-11-18 00:42:17 +00:00
}
}
2018-12-23 12:11:30 +00:00
if(1) {
map<cell*, pair<int, int> > cdists;
manual_celllister cl;
cl.add(s);
for(auto cc: rti) if(cc.from_start == 0) cl.add(cc.c);
for(int i=0; i<isize(cl.lst); i++) {
cell *c = cl.lst[i];
forCellEx(c2, c) {
if(passable(c2, c, P_ISPLAYER) && !cl.listed(c2))
cl.add(c2), get_info(c2).from_start = get_info(c).from_start + 1;
}
}
}
if(1) {
map<cell*, pair<int, int> > cdists;
manual_celllister cl;
for(auto cc: rti) if(among(cc.c->wall, waCloud, waMirror))
cc.from_goal = 0, cl.add(cc.c);
for(int i=0; i<isize(cl.lst); i++) {
cell *c = cl.lst[i];
forCellEx(c2, c) {
if(passable(c2, c, P_ISPLAYER) && !cl.listed(c2))
cl.add(c2), get_info(c2).from_goal = get_info(c).from_goal + 1;
}
}
}
int total_track = get_info(s).from_goal;
auto blockoff = [&total_track] (race_cellinfo& cc) {
if(cc.from_start < 1 || cc.from_goal < 1) return false;
int dif = cc.from_start + cc.from_goal - 2 * cc.from_track - total_track;
return dif > 3;
};
vector<cell*> to_block;
for(auto cc: rti) if(blockoff(cc)) to_block.push_back(cc.c);
2022-06-16 21:23:29 +00:00
if(gen) block_cells(to_block, [&blockoff] (cell *c) {
2019-09-28 12:18:58 +00:00
forCellEx(c2, c) if(passable(c2, c, P_ISPLAYER) && !blockoff(get_info(c2))) return true;
return false;
});
2018-12-23 12:11:30 +00:00
// for(cell *c: to_block) if(blockbound(c)) c->land = laOvergrown;
2018-11-17 18:30:50 +00:00
/*
for(cell *c: track) {
int i = trackval(c) - celldist(c);
if(i == 0) c->item = itDiamond;
if(i == 1) c->item = itGold;
if(i == 2) c->item = itEmerald;
if(i == 3) c->item = itSapphire;
if(i == 4) c->item = itRuby;
if(i >= 5) c->item = itBone;
}
*/
track_ready = true;
2022-06-16 21:10:44 +00:00
reset_race();
}
EX void reset_race() {
2022-06-16 21:10:44 +00:00
cell *s = track[0];
vector<shiftmatrix> forbidden;
for(auto& ghost: ghostset)
forbidden.push_back(get_ghostmoment_matrix(ghost.history[0]));
2018-11-24 02:26:07 +00:00
race_start_tick = 0;
for(int i=0; i<MAXPLAYER; i++) race_finish_tick[i] = 0;
2019-01-11 01:19:28 +00:00
2022-06-16 21:10:44 +00:00
for(int i=0; i<multi::players; i++) trophy[i] = 0;
for(int i=0; i<multi::players; i++) {
auto who = shmup::pc[i];
if(!who) { println(hlog, "who missing"); continue; }
2022-06-21 08:34:43 +00:00
who->reset();
2022-06-16 21:10:44 +00:00
// this is intentionally not hrand
for(int j=0; j<100; j++) {
if(WDIM == 3 || bounded_track)
who->at = Id; // straight * cspin(0, 2, rand() % 360) * cspin(1, 2, rand() % 360);
else
who->at = straight * parabolic1(start_line_width * (rand() % 20000 - 10000) / 40000) * spin(rand() % 360);
who->base = s;
bool ok = true;
for(const shiftmatrix& t: forbidden) if(hdist(t*C0, shiftless(who->at) * C0) < 10. / (j+10)) ok = false;
if(ok) break;
2022-02-02 02:07:12 +00:00
}
2022-06-16 21:10:44 +00:00
virtualRebase(who);
2019-01-11 01:19:28 +00:00
}
2022-06-16 21:10:44 +00:00
2018-11-17 18:30:50 +00:00
}
bool inrec = false;
EX trans23 race_angle = cspin90(0, 1);
2018-11-17 18:30:50 +00:00
EX bool force_standard_centering() {
2022-12-08 18:38:06 +00:00
return nonisotropic || mhybrid || quotient || closed_or_bounded;
}
EX bool use_standard_centering() {
return standard_centering || force_standard_centering();
}
EX transmatrix track_matrix(int at, int dir) {
transmatrix res = unshift(ggmatrix(racing::track[at]));
while(true) {
if(at+dir < 0 || at+dir >= isize(racing::track)) return res;
2020-11-01 16:37:51 +00:00
for(int x=0; x<MXDIM; x++) for(int y=0; y<MXDIM; y++)
if(abs(res[y][x]) > 10000) return res;
cell *cur = racing::track[at];
at += dir;
cell *next = racing::track[at];
int nei = neighborId(cur, next);
if(nei == -1) return res;
res = res * currentmap->adj(cur, nei);
}
}
EX bool set_view() {
2018-11-17 18:30:50 +00:00
2019-03-09 15:20:06 +00:00
multi::cpid = subscreens::in ? subscreens::current_player : 0;
2018-11-24 02:26:07 +00:00
2019-03-09 15:20:06 +00:00
if(race_start_tick == 0) race_start_tick = ticks + 5000;
2018-11-17 18:30:50 +00:00
2019-03-09 15:20:06 +00:00
shmup::monster *who = shmup::pc[multi::cpid];
2018-11-17 18:30:50 +00:00
2018-11-26 22:17:43 +00:00
if(!inrec) {
const transmatrix T = who->at;
ld alpha = -atan2(T * C0);
ld distance = hdist0(T * C0);
ld beta = -atan2(xpush(-distance) * spin(-alpha) * T * Cx1);
2022-06-16 21:10:44 +00:00
current_history[multi::cpid].emplace_back(ghostmoment{ticks - race_start_tick, who->base,
2018-11-26 22:17:43 +00:00
angle_to_uchar(alpha),
frac_to_uchar(distance / distance_multiplier),
angle_to_uchar(beta),
frac_to_uchar(who->footphase)
});
}
2018-11-17 18:30:50 +00:00
if(use_standard_centering()) return false;
2019-05-21 23:13:54 +00:00
if(player_relative && specialland == laAsteroids) return false;
2019-02-27 17:00:50 +00:00
hyperpoint at;
transmatrix atm;
if(multi::players == 1 || multi::split_screen) {
atm = ypush(-vid.yshift) * unshift(ggmatrix(who->base)) * who->at;
at = atm * C0;
}
else {
at = Hypc;
hyperpoint first;
for(int i=0; i<multi::players; i++) {
hyperpoint h = ypush(-vid.yshift) * unshift(ggmatrix(shmup::pc[i]->base)) * tC0(shmup::pc[i]->at);;
if(i == 0) first = h;
at += h;
}
at /= multi::players;
at = normalize(at);
atm = rgpushxto0(at) * spin270() * rspintox(gpushxto0(at) * first);
}
2018-11-17 18:30:50 +00:00
if(racing::player_relative || quotient || (kite::in() && GDIM == 3)) {
View = iso_inverse(atm) * View;
2019-05-11 21:55:02 +00:00
}
2018-11-17 18:30:50 +00:00
else {
/* this works only in isotropic geometries, but we don't really care about this in 3D */
int z = get_info(who->base).completion;
transmatrix T1 = track_matrix(z, -1);
transmatrix T2 = track_matrix(z, +1);
2020-09-16 03:57:05 +00:00
transmatrix iT1 = iso_inverse(T1);
2020-09-16 03:57:05 +00:00
transmatrix T = spintox(iT1 * T2 * C0);
hyperpoint h = T * iT1 * ypush(vid.yshift) * at;
2019-05-11 21:55:02 +00:00
ld y = GDIM == 2 ? asin_auto(h[1]) : asin_auto(hypot(h[1], h[2]));
2018-11-17 18:30:50 +00:00
ld x = asin_auto(h[0] / cos_auto(y));
2018-12-23 13:01:07 +00:00
x += race_advance;
2019-05-13 15:16:54 +00:00
if(GDIM == 3 && race_advance == 0 && pmodel == mdPerspective) race_advance = -1;
View = xpush(-x) * T * iT1 * ypush(vid.yshift) * View;
if(GDIM == 3) View = cspin(2, 0, 90._deg) * View;
fixmatrix(View);
2018-11-17 18:30:50 +00:00
}
2019-05-11 21:55:02 +00:00
if(GDIM == 3 && WDIM == 2)
View = spin180() * cspin(2, 1, 90._deg + shmup::playerturny[multi::cpid]) * spin270() * View;
else if(GDIM == 2) View = race_angle.v2 * View;
2019-05-11 21:55:02 +00:00
return true;
2018-11-17 18:30:50 +00:00
}
#if CAP_COMMANDLINE
void show();
int readArgs() {
using namespace arg;
if(0) ;
else if(argis("-racing")) {
PHASEFROM(2);
stop_game();
switch_game_mode(rg::racing);
2019-10-05 13:34:26 +00:00
}
2022-06-16 21:10:44 +00:00
else if(argis("-racing-official")) {
PHASEFROM(2);
load_official_track();
}
2019-10-05 13:34:26 +00:00
else if(argis("-rsc")) {
standard_centering = true;
}
else if(argis("-rfast")) {
PHASEFROM(3);
start_game();
race_start_tick = 1;
2018-11-17 18:30:50 +00:00
}
2022-02-02 02:07:12 +00:00
else if(argis("-rtry")) {
shift(); race_try = argi();
}
2018-11-17 18:30:50 +00:00
else return 1;
return 0;
}
#endif
int tstart, tstop;
heptspin sview;
auto hook =
#if CAP_COMMANDLINE
2018-11-17 18:30:50 +00:00
addHook(hooks_args, 100, readArgs)
#endif
+ addHook(hooks_clearmemory, 0, []() {
track_ready = false;
track.clear();
rti.clear();
rti_id.clear();
2022-06-16 21:10:44 +00:00
reachable_goals.clear();
for(auto &ch: current_history) ch.clear();
})
+ addHook(hooks_configfile, 100, [] {
2022-03-27 07:53:02 +00:00
param_f(racing::race_advance, "race_advance");
param_matrix(racing::race_angle.v2, "race_angle", 2);
param_matrix(racing::race_angle.v3, "race_angle3", 3);
2022-03-27 07:53:02 +00:00
param_i(racing::ghosts_to_show, "race_ghosts_to_show");
param_i(racing::ghosts_to_save, "race_ghosts_to_save");
param_b(racing::guiding, "race_guiding");
param_b(racing::player_relative, "race_player_relative");
param_b(racing::standard_centering, "race_standard_centering");
})
2018-11-17 18:30:50 +00:00
// + addHook(hooks_handleKey, 120, akh);
;
2019-08-09 19:00:52 +00:00
EX vector<eLand> race_lands = {
2018-11-23 01:58:16 +00:00
laHunting,
2018-11-24 00:30:05 +00:00
laCrossroads,
laJungle,
laDesert,
2018-11-17 18:30:50 +00:00
laRedRock,
laDragon,
laMirror,
2018-11-17 18:30:50 +00:00
laRuins,
laCaves,
laWildWest,
laIce,
laHell,
laTerracotta,
laElementalWall,
laDryForest,
laDeadCaves,
2019-05-21 23:13:54 +00:00
laAsteroids
2018-11-17 18:30:50 +00:00
};
2018-11-18 02:44:23 +00:00
vector<string> playercmds_race = {
"forward", "backward", "turn left", "turn right",
"forward", "backward", "turn left", "turn right",
"", "", "",
"", "change camera", "", ""
};
2019-08-09 19:00:52 +00:00
EX string racetimeformat(int t) {
string times = "";
int digits = 0;
bool minus = (t < 0);
if(t < 0) t = -t;
while(t || digits < 6) {
int mby = (digits == 4 ? 6 : 10);
times = char('0'+(t%mby)) + times;
t /= mby; digits++;
if(digits == 3) times = "." + times;
if(digits == 5) times = ":" + times;
}
if(minus) times = "-" + times;
return times;
}
extern int playercfg;
2022-06-16 21:10:44 +00:00
EX void load_official_track() {
fhstream f("officials.data", "rb");
hread(f, f.vernum);
map<eLand, string> tracks;
hread(f, tracks);
eLand l = specialland;
if(!tracks.count(l)) {
println(hlog, "ERROR: no official track in the database");
throw hstream_exception();
}
string s = decompress_string(tracks[l]);
shstream sf(s);
2022-08-14 17:25:10 +00:00
#if CAP_EDIT
2022-06-16 21:10:44 +00:00
mapstream::loadMap(sf);
2022-08-14 17:25:10 +00:00
#endif
2022-06-16 21:10:44 +00:00
cheater = autocheat = 0;
official_race = true;
}
void track_chooser(bool official) {
cmode = sm::NOSCR;
gamescreen();
2023-12-03 13:29:02 +00:00
dialog::init(official ? XLAT("play on an official track") : XLAT("generate a random track"));
2019-01-11 01:19:28 +00:00
map<char, eLand> landmap;
dynamicval<bool> so(shmup::on, true);
dynamicval<bool> ro(racing::on, true);
char let = 'a';
for(eLand l: race_lands) {
2019-01-11 01:19:28 +00:00
landmap[let] = l;
2022-06-16 21:10:44 +00:00
string s = "";
if(best_scores.count(l))
s = racetimeformat(best_scores[l]);
dialog::addSelItem(XLAT1(linf[l].name), s, let++);
2022-06-16 21:10:44 +00:00
dialog::add_action([l, official] () {
stop_game();
multi::players = playercfg;
specialland = l;
if(!racing::on) switch_game_mode(rg::racing);
2022-06-16 21:10:44 +00:00
if(official) {
racing::on = false;
load_official_track();
}
else {
start_game();
}
popScreenAll();
});
}
dialog::addBack();
dialog::display();
2022-06-16 21:10:44 +00:00
if(landmap.count(getcstat) && official)
displayScore(landmap[getcstat]);
}
void race_projection() {
cmode = sm::SIDE | sm::MAYDARK;
gamescreen();
dialog::init(XLAT("racing projections"));
2023-08-14 16:08:28 +00:00
dialog::addBoolItem(XLAT("Poincaré disk model"), pmodel == mdDisk && models::camera_straight, '1');
dialog::add_action([] () {
pmodel = mdDisk;
race_advance = 0;
vid.yshift = 0;
2023-08-14 16:08:28 +00:00
pconf.cam() = Id;
pconf.xposition = 0;
pconf.yposition = 0;
pconf.scale = 1;
2019-01-11 14:01:36 +00:00
vid.use_smart_range = 0;
vid.smart_range_detail = 3;
});
dialog::addBoolItem(XLAT("band"), pmodel == mdBand, '2');
dialog::add_action([] () {
pmodel = mdBand;
pconf.mori() = race_angle;
race_advance = 1;
vid.yshift = 0;
2023-08-14 16:08:28 +00:00
pconf.cam() = Id;
pconf.xposition = 0;
pconf.yposition = 0;
pconf.scale = 1;
2019-01-11 14:01:36 +00:00
vid.use_smart_range = 1;
vid.smart_range_detail = 3;
});
dialog::addBoolItem(XLAT("half-plane"), pmodel == mdHalfplane, '3');
dialog::add_action([] () {
pmodel = mdHalfplane;
pconf.mori() = race_angle * spin90();
race_advance = 0.5;
vid.yshift = 0;
2023-08-14 16:08:28 +00:00
pconf.cam() = Id;
pconf.xposition = 0;
pconf.yposition = 0;
pconf.scale = 1;
2019-01-11 14:01:36 +00:00
vid.use_smart_range = 1;
vid.smart_range_detail = 3;
});
2023-08-14 16:08:28 +00:00
dialog::addBoolItem(XLAT("third-person perspective"), pmodel == mdDisk && !models::camera_straight, '4');
dialog::add_action([] () {
pmodel = mdDisk;
race_advance = 0;
vid.yshift = -0.3;
2023-08-14 16:08:28 +00:00
pconf.cam() = cspin(1, 2, -45._deg);
pconf.scale = 18/16. * vid.xres / vid.yres / multi::players;
pconf.xposition = 0;
pconf.yposition = -0.9;
2019-01-11 14:01:36 +00:00
vid.use_smart_range = 1;
vid.smart_range_detail = 3;
});
2019-05-11 21:55:02 +00:00
if(true) {
2021-05-23 12:33:25 +00:00
dialog::addSelItem(XLAT("point of view"), player_relative ? XLAT("player") : XLAT("track"), 'p');
if(racing::use_standard_centering())
dialog::lastItem().value = XLAT("N/A");
dialog::add_action([] () {
player_relative = !player_relative;
if(pmodel == mdBand || pmodel == mdHalfplane)
pmodel = mdDisk;
if(racing::on)
set_view();
});
}
else dialog::addBreak(100);
2019-08-15 13:05:43 +00:00
if(GDIM == 2) {
dialog::addMatrixItem(XLAT("race angle"), race_angle.get(), 'a');
2023-08-08 15:20:39 +00:00
dialog::add_action([] () {
dialog::editMatrix(race_angle.get(), XLAT("model orientation"), "", GDIM);
auto q = rot_inverse(race_angle) * pconf.mori();
2023-08-09 12:01:24 +00:00
auto last = dialog::get_ne().reaction;
dialog::get_ne().reaction = [q, last] () { last(); pconf.mori() = race_angle * q; };
2019-05-11 21:55:02 +00:00
});
}
dialog::addSelItem(XLAT("show more in front"), fts(race_advance), 'A');
dialog::add_action([] () {
dialog::editNumber(race_advance, 0, 360, 0.1, 1, XLAT("show more in front"), "");
});
2019-01-17 00:15:28 +00:00
dialog::addBoolItem(XLAT("do not use special centering for racing"), standard_centering, 'C');
if(force_standard_centering()) dialog::lastItem().value = XLAT("N/A");
2019-01-17 00:15:28 +00:00
dialog::add_action([] () {
standard_centering = !standard_centering;
});
dialog::addBack();
dialog::display();
}
2018-11-18 02:44:23 +00:00
int playercfg;
2018-11-24 21:50:35 +00:00
/* struct race_configurer { */
2022-06-16 21:10:44 +00:00
void set_race_configurer() { playercfg = multi::players; }
2018-11-18 02:44:23 +00:00
2019-11-02 21:31:37 +00:00
bool alternate = false;
2020-04-17 13:35:48 +00:00
#if MAXMDIM >= 4
EX void thurston_racing() {
cmode = sm::NOSCR;
gamescreen();
dialog::init(XLAT("racing in Thurston geometries"));
dialog::addBreak(100);
char ch = '1';
auto add_thurston_race = [&ch] (string caption, reaction_t launcher) {
dialog::addBigItem(caption, ch++);
dialog::add_action([=] { stop_game();
if(!racing::on) switch_game_mode(rg::racing);
racing::standard_centering = true;
launcher();
start_game();
popScreenAll();
});
dialog::addBreak(50);
};
2019-11-02 21:31:37 +00:00
if(!alternate) {
2019-12-08 10:01:28 +00:00
add_thurston_race(XLAT("Euclidean"), [] { stop_game(); euc::clear_torus3(); set_geometry(gBitrunc3); });
2019-11-02 21:31:37 +00:00
add_thurston_race(XLAT("hyperbolic"), [] { set_geometry(gBinary3); vid.texture_step = 4; });
add_thurston_race(XLAT("spherical"), [] { set_geometry(gCell120); });
#if CAP_SOLV
add_thurston_race(XLAT("Solv geometry"), [] { sn::solrange_xy = 10; sn::solrange_z = 3; set_geometry(gSol); });
#endif
2019-11-02 21:31:37 +00:00
add_thurston_race(XLAT("S2xE"), [] { set_geometry(gSphere); set_variation(eVariation::bitruncated); set_geometry(gProduct); });
add_thurston_race(XLAT("H2xE"), [] { set_geometry(gNormal); set_variation(eVariation::bitruncated); set_geometry(gProduct); });
add_thurston_race(XLAT("Nil"), [] { stop_game(); nilv::nilperiod[0] = 0; set_geometry(gNil); });
add_thurston_race(XLAT("PSL(2,R)"), [] { set_geometry(gNormal); set_variation(eVariation::pure); set_geometry(gRotSpace); });
}
else {
#if CAP_SOLV
2019-11-02 21:31:37 +00:00
add_thurston_race(XLAT("stretched hyperbolic"), [] { set_geometry(gNIH); vid.texture_step = 4; });
add_thurston_race(XLAT("stretched Solv"), [] { set_geometry(gSolN); sn::solrange_xy = 10; sn::solrange_z = 3; vid.texture_step = 4; });
add_thurston_race(XLAT("periodic Solv"), [] { stop_game(); sn::solrange_xy = 5; sn::solrange_z = 2; asonov::period_xy = 8; asonov::period_z = 0; asonov::set_flags(); set_geometry(gArnoldCat); });
#endif
2019-11-02 21:31:37 +00:00
add_thurston_race(XLAT("hyperbolic crystal"), [] { set_geometry(gCrystal344); vid.texture_step = 4; });
2019-12-08 09:59:09 +00:00
add_thurston_race(XLAT("torus x E"), [] { stop_game(); euc::eu_input = euc::torus3(4, 4, 0); set_geometry(gCubeTiling); });
2019-11-02 21:31:37 +00:00
add_thurston_race(XLAT("hyperbolic regular"), [] { set_geometry(gSpace534); });
add_thurston_race(XLAT("S2xE regular"), [] { set_geometry(gSphere); set_variation(eVariation::pure); set_geometry(gProduct); });
add_thurston_race(XLAT("H2xE regular"), [] { set_geometry(gNormal); set_variation(eVariation::pure); set_geometry(gProduct); });
add_thurston_race(XLAT("periodic Nil"), [] { stop_game(); nilv::nilperiod[0] = 3; set_geometry(gNil); });
}
dialog::addBoolItem_action(XLAT("alternate versions"), alternate, 'x');
dialog::addBreak(100);
dialog::addBack();
dialog::display();
}
2020-04-17 13:35:48 +00:00
#endif
void raceconfigurer() {
2018-11-18 02:44:23 +00:00
cmode = sm::SIDE | sm::MAYDARK;
gamescreen();
2018-11-18 02:44:23 +00:00
dialog::init(XLAT("Racing"));
2022-06-16 21:10:44 +00:00
dialog::addBoolItem(XLAT("play on an official track"), racing::on && official_race, 'O');
2022-06-16 21:10:44 +00:00
dialog::add_action_push([/*this*/] () { track_chooser(true); });
dialog::addItem(XLAT("generate a random track"), 's');
dialog::add_action_push([/*this*/] () { track_chooser(false); });
2018-11-18 02:44:23 +00:00
dialog::addBreak(100);
if(WDIM == 3) {
dialog::addItem(XLAT("3D configuration"), '9');
dialog::add_action_push(show3D);
}
else {
dialog::addItem(XLAT("configure the projection"), 'p');
dialog::add_action_push(race_projection);
}
2018-11-18 02:44:23 +00:00
dialog::addBoolItem_action(XLAT("guiding line"), guiding, 'g');
2018-11-18 02:44:23 +00:00
dialog::addItem(multi::player_count_name(playercfg), 'n');
dialog::add_action([/*this*/] () {
2018-11-18 02:44:23 +00:00
playercfg = playercfg == 1 ? 2 : 1;
});
dialog::addItem(XLAT("configure player 1"), '1');
dialog::add_action([] () {
pushScreen(multi::get_key_configurer(1, playercmds_race));
2018-11-18 02:44:23 +00:00
});
if(playercfg >= 2) {
dialog::addItem(XLAT("configure player 2"), '2');
dialog::add_action([] () {
pushScreen(multi::get_key_configurer(2, playercmds_race));
2018-11-18 02:44:23 +00:00
});
2018-11-17 18:30:50 +00:00
}
2018-11-18 02:44:23 +00:00
else dialog::addBreak(100);
2018-11-24 21:50:35 +00:00
2019-01-03 18:58:53 +00:00
dialog::addBreak(100);
dialog::addSelItem(XLAT("best scores to show as ghosts"), its(ghosts_to_show), 'c');
dialog::add_action([]() { dialog::editNumber(ghosts_to_show, 0, 100, 1, 5, "best scores to show as ghosts", ""); });
dialog::addSelItem(XLAT("best scores to save"), its(ghosts_to_save), 'b');
dialog::add_action([]() { dialog::editNumber(ghosts_to_save, 0, 100, 1, 10, "best scores to save", ""); });
if(racing::on) {
dialog::addItem(XLAT("disable the racing mode"), 'x');
dialog::add_action([] () {
stop_game();
switch_game_mode(rg::racing);
2018-11-25 22:39:47 +00:00
race_try = 0;
if(geometry == gNormal) specialland = firstland = laIce;
start_game();
});
}
2020-04-17 13:35:48 +00:00
#if MAXMDIM >= 4
dialog::addItem(XLAT("racing in Thurston geometries"), 'T');
dialog::add_action_push(thurston_racing);
2020-04-17 13:35:48 +00:00
#endif
2018-11-18 02:44:23 +00:00
dialog::addBack();
dialog::display();
2018-11-24 21:50:35 +00:00
keyhandler = [/*this*/] (int sym, int uni) {
2018-11-24 21:50:35 +00:00
dialog::handleNavigation(sym, uni);
2022-06-16 21:10:44 +00:00
if(doexiton(sym, uni)) popScreen();
2018-11-24 21:50:35 +00:00
};
2018-11-18 02:44:23 +00:00
}
/* }; */
2018-11-17 18:30:50 +00:00
2019-08-09 19:00:52 +00:00
EX void configure_race() {
set_race_configurer();
pushScreen(raceconfigurer);
}
2018-11-18 02:44:23 +00:00
auto hooks1 =
addHook(hooks_o_key, 90, [] (o_funcs& v) {
if(racing::on) { set_race_configurer(); v.push_back(named_dialog(XLAT("racing menu"), raceconfigurer)); }
2018-11-17 18:30:50 +00:00
});
2019-01-11 01:19:28 +00:00
map<string, map<eLand, int> > scoreboard;
void uploadScore() {
int tscore = 0;
for(eLand l: race_lands) {
2022-06-16 21:13:21 +00:00
if(!best_scores.count(l)) continue;
int i = best_scores[l];
2019-01-11 01:19:28 +00:00
if(!i) continue;
2019-01-17 13:08:10 +00:00
scoreboard[myname()][l] = i;
2019-01-11 01:19:28 +00:00
int score = 60000000 / i; // 1000 points for minute, 2000 points for 30 sec
tscore += score;
}
achievement_score(LB_RACING, tscore);
}
2019-08-09 19:00:52 +00:00
EX void displayScore(eLand l) {
2019-01-11 01:19:28 +00:00
int vf = min((vid.yres-64) / 70, vid.xres/80);
int x = vid.xres / 4;
2019-01-14 22:00:17 +00:00
set_priority_board(LB_RACING);
2019-01-11 01:19:28 +00:00
if(get_sync_status() == 1) {
displayfr(x, 56, 1, vf, "(syncing)", 0xC0C0C0, 0);
}
else {
vector<pair<int, string> > scores;
for(auto p: scoreboard) if(p.second.count(l)) scores.emplace_back(p.second[l], p.first);
sort(scores.begin(), scores.end());
int i = 0;
for(auto& sc: scores) {
int i0 = 56 + (i++) * vf;
displayfr(x, i0, 1, vf, racetimeformat(sc.first), 0xC0C0C0, 16);
displayfr(x+8, i0, 1, vf, sc.second, 0xC0C0C0, 0);
}
}
}
2022-10-27 10:27:37 +00:00
EX int pause_limit = 3;
EX bool started() {
return race_start_tick && ticks >= race_start_tick;
}
EX bool finished() {
for(int& i: racing::race_finish_tick) if(!i) return false;
return true;
}
2019-08-09 19:00:52 +00:00
EX void race_won() {
2019-03-09 15:20:06 +00:00
if(!race_finish_tick[multi::cpid]) {
2022-10-27 10:27:37 +00:00
if(shmup::count_pauses >= pause_limit) {
addMessage(XLAT("Too many pauses!"));
race_finish_tick[multi::cpid] = ticks;
return;
}
2019-01-07 03:55:21 +00:00
int result = ticks - race_start_tick;
int losers = 0;
int place = 1;
for(int i=0; i<multi::players; i++) if(race_finish_tick[i]) place++;
2022-06-16 21:10:44 +00:00
for(auto& ghost: ghostset) if(ghost.result < result) place++; else losers++;
2019-01-07 03:55:21 +00:00
2019-03-09 15:20:06 +00:00
if(place == 1 && losers) trophy[multi::cpid] = 0xFFD500FF;
if(place == 2) trophy[multi::cpid] = 0xFFFFC0FF;
if(place == 3) trophy[multi::cpid] = 0x967444FF;
2019-01-17 13:12:26 +00:00
if(place + losers > 1)
addMessage(XLAT("Finished the race! Time: %1, place: %2 out of %3", racetimeformat(result), its(place), its(place+losers)));
else
addMessage(XLAT("Finished the race in time %1!", racetimeformat(result)));
2019-01-07 03:55:21 +00:00
if(place == 1 && official_race)
2019-01-11 01:19:28 +00:00
achievement_gain("RACEWON", rg::racing);
2019-03-09 15:20:06 +00:00
race_finish_tick[multi::cpid] = ticks;
charstyle gcs = getcs();
2018-11-24 20:24:27 +00:00
for(color_t *x: {&gcs.skincolor, &gcs.haircolor, &gcs.dresscolor, &gcs.swordcolor, &gcs.dresscolor2}) {
for(int a=1; a<4; a++)
part(*x, a) = (part(*x, a) + part(gcs.uicolor, a)) / 2;
part(*x, 0) >>= 2;
}
2022-06-16 21:10:44 +00:00
if(true) {
auto &subtrack = ghostset;
subtrack.emplace_back(ghost{gcs, result, VERNUM_HEX, time(NULL), current_history[multi::cpid]});
sort(subtrack.begin(), subtrack.end(), [] (const ghost &g1, const ghost &g2) { return g1.result < g2.result; });
if(isize(subtrack) > ghosts_to_save && ghosts_to_save > 0)
subtrack.resize(ghosts_to_save);
}
2019-01-11 01:19:28 +00:00
2022-06-16 21:10:44 +00:00
if(official_race && !cheater) {
if(!best_scores.count(specialland))
best_scores[specialland] = result;
best_scores[specialland] = min(best_scores[specialland], result);
2022-08-07 00:16:40 +00:00
best_scores_to_save[specialland] = result;
2022-08-14 17:25:10 +00:00
#if CAP_SAVE
2022-06-16 21:10:44 +00:00
saveStats();
2022-08-14 17:25:10 +00:00
#endif
2022-06-16 21:10:44 +00:00
if(official_race) uploadScore();
}
}
}
void draw_ghost_at(ghost& ghost, cell *w, const shiftmatrix& V, ghostmoment& p) {
dynamicval<charstyle> x(getcs(), ghost.cs);
if(ghost.cs.charid == -1) {
dynamicval<bool> pc(peace::on, true);
drawMonsterType(eMonster(ghost.cs.uicolor), w, V, ghost.cs.dresscolor, uchar_to_frac(p.footphase), NOCOLOR);
return;
}
drawMonsterType(moPlayer, w, V, 0, uchar_to_frac(p.footphase), NOCOLOR);
2019-01-17 13:11:12 +00:00
}
bool ghost_finished(ghost& ghost) {
auto p = std::find_if(ghost.history.begin(), ghost.history.end(), [] (const ghostmoment gm) { return gm.step > ticks - race_start_tick;} );
return p == ghost.history.end();
}
ghostmoment& get_ghostmoment(ghost& ghost) {
auto p = std::find_if(ghost.history.begin(), ghost.history.end(), [] (const ghostmoment gm) { return gm.step > ticks - race_start_tick;} );
if(p == ghost.history.end()) p--, p->footphase = 0;
return *p;
}
void draw_ghost(ghost& ghost) {
auto& p = get_ghostmoment(ghost);
2022-06-16 21:10:44 +00:00
cell *w = p.where_cell;
2019-01-17 13:11:12 +00:00
if(!gmatrix.count(w)) return;
draw_ghost_at(ghost, w, get_ghostmoment_matrix(p), p);
}
shiftmatrix racerel(ld rel) {
2019-01-17 13:11:12 +00:00
int bsize = vid.fsize * 2;
return shiftless(atscreenpos(bsize, vid.yres - bsize - rel * (vid.yres - bsize*2) / 100, bsize) * spin90());
2019-01-17 13:11:12 +00:00
}
EX int get_percentage(cell *c) {
2019-01-17 13:11:12 +00:00
return min(get_info(c).completion * 100 / (isize(track) - DROP), 100);
}
EX int get_percentage(int i) {
2019-01-17 13:11:12 +00:00
return get_percentage(shmup::pc[i]->base);
}
void draw_ghost_state(ghost& ghost) {
auto& p = get_ghostmoment(ghost);
2022-06-16 21:10:44 +00:00
cell *w = p.where_cell;
2019-01-17 13:11:12 +00:00
ld result = ghost_finished(ghost) ? 100 : get_percentage(w);
draw_ghost_at(ghost, w, racerel(result), p);
}
EX void drawStats() {
2019-01-17 13:11:12 +00:00
if(!racing::on) return;
2020-04-17 13:54:48 +00:00
flat_model_enabler fme;
2019-01-17 13:11:12 +00:00
initquickqueue();
int bsize = vid.fsize * 2;
for(int y: {bsize, vid.yres - bsize}) {
curvepoint(atscreenpos(bsize, y, bsize) * C0);
curvepoint(atscreenpos(bsize/2, y, bsize) * C0);
curvepoint(atscreenpos(bsize*3/2, y, bsize) * C0);
curvepoint(atscreenpos(bsize, y, bsize) * C0);
}
queuecurve(shiftless(Id), 0xFFFFFFFF, 0, PPR::ZERO);
2019-01-17 13:11:12 +00:00
2022-06-16 21:10:44 +00:00
for(auto& ghost: ghostset) draw_ghost_state(ghost);
2019-01-17 13:11:12 +00:00
for(int i=0; i<multi::players; i++) {
dynamicval<int> d(multi::cpid, i);
drawMonsterType(moPlayer, shmup::pc[i]->base, racerel(race_finish_tick[i] ? 100 : get_percentage(i)), 0xFFFFFFC0, shmup::pc[i]->footphase, NOCOLOR);
2019-01-17 13:11:12 +00:00
}
quickqueue();
}
EX void markers() {
if(!racing::on) return;
2019-05-11 21:55:37 +00:00
if(guiding && WDIM == 2) for(int i=0; i<multi::players; i++) {
2019-01-07 03:53:09 +00:00
shmup::monster *m = shmup::pc[i];
if(!m) continue;
for(int j=0; j<5; j++)
2019-05-11 21:55:37 +00:00
gridline(m->pat, xpush0(j), xpush0(j+1), multi::scs[i].uicolor, 2);
2019-01-07 03:53:09 +00:00
}
if(racing::player_relative || use_standard_centering() || WDIM == 3) {
using namespace racing;
cell *goal = NULL;
for(cell *c: track) if(inscreenrange(c)) goal = c;
if(!goal) return;
shiftpoint H = tC0(ggmatrix(goal));
if(invalid_point(H.h)) return;
queuestr(H, 2*vid.fsize, "X", 0x10100 * int(128 + 100 * sintick(150)));
int cd = celldistance(track.back(), cwt.at);
if(cd != DISTANCE_UNKNOWN)
2018-12-04 20:17:57 +00:00
queuestr(H, vid.fsize,
2019-02-27 14:53:07 +00:00
#if CAP_CRYSTAL
(cryst && !crystal::pure()) ? fts(crystal::space_distance(cwt.at, track.back())) :
2019-02-27 14:53:07 +00:00
#endif
its(cd), 0x10101 * int(128 - 100 * sintick(150)));
2018-12-23 13:30:18 +00:00
addauraspecial(H, 0xFFD500, 0);
}
2018-11-25 22:39:47 +00:00
2022-06-16 21:10:44 +00:00
for(auto& ghost: ghostset)
draw_ghost(ghost);
2018-11-25 22:39:47 +00:00
if(gmatrix.count(track[0])) {
hyperpoint h = WDIM == 2 && GDIM == 3 ? zpush(cgi.FLOOR - cgi.human_height/80) * C0 : C0;
2018-11-25 22:39:47 +00:00
for(ld z=-start_line_width; z<=start_line_width; z+=0.1)
curvepoint(parabolic1(z) * h);
queuecurve(ggmatrix(track[0]) * straight, 0xFFFFFFFF, 0, PPR::BFLOOR);
2018-11-25 22:39:47 +00:00
}
}
2019-08-09 19:00:52 +00:00
EX void add_debug(cell *c) {
2018-12-23 12:10:46 +00:00
if(racing::on && racing::rti_id[c]) {
auto& r = racing::get_info(c);
dialog::addSelItem("from_track", its(r.from_track), 0);
dialog::addSelItem("from_start", its(r.from_start), 0);
dialog::addSelItem("from_goal", its(r.from_goal), 0);
dialog::addSelItem("completion", its(r.completion), 0);
}
}
2021-02-07 17:29:49 +00:00
#if MAXMDIM >= 4
2021-02-06 01:55:51 +00:00
EX void start_thurston() {
stop_game();
resetModes();
start_game();
pushScreen(showStartMenu);
pushScreen(racing::thurston_racing);
}
2021-02-07 17:29:49 +00:00
#endif
#endif
2021-02-06 01:55:51 +00:00
2019-09-13 01:10:26 +00:00
#if !CAP_RACING
EX always_false on;
2019-09-06 07:17:45 +00:00
#endif
2019-09-13 01:10:26 +00:00
EX }
}