Free Hero Mesh

Check-in [3edc879c32]
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.
Overview
Comment:Implement the solution replay
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3edc879c324f49d885daf45a63a6caf334d28065
User & Date: user on 2021-01-09 00:56:28
Other Links: manifest | tags
Context
2021-01-09
01:32
Fix the logic for sending CREATED and DESTROYED messages check-in: 00378e2e14 user: user tags: trunk
00:56
Implement the solution replay check-in: 3edc879c32 user: user tags: trunk
2021-01-08
09:14
Implement defining the CollisionLayers value for a class. check-in: 34870ce7e8 user: user tags: trunk
Changes

Modified game.c from [537835c926] to [a95ee4649a].

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
#include "heromesh.h"
#include "quarks.h"
#include "cursorshapes.h"
#include "names.h"

Uint8*replay_list;
Uint16 replay_size,replay_count,replay_pos,replay_mark;


static volatile Uint8 timerflag;
static int exam_scroll;
static Uint8*inputs;
static int inputs_size,inputs_count;
static Uint8 side_mode=255;

static void setup_game(void) {
  const char*v;
  optionquery[1]=Q_showInventory;
  v=xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"";
  side_mode=boolxrm(v,1);

}

static void redraw_game(void) {
  char buf[32];
  SDL_Rect r;
  int x,y;
  r.x=r.y=0;







>












>







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
#include "heromesh.h"
#include "quarks.h"
#include "cursorshapes.h"
#include "names.h"

Uint8*replay_list;
Uint16 replay_size,replay_count,replay_pos,replay_mark;
Uint8 solution_replay=255;

static volatile Uint8 timerflag;
static int exam_scroll;
static Uint8*inputs;
static int inputs_size,inputs_count;
static Uint8 side_mode=255;

static void setup_game(void) {
  const char*v;
  optionquery[1]=Q_showInventory;
  v=xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"";
  side_mode=boolxrm(v,1);
  solution_replay=0;
}

static void redraw_game(void) {
  char buf[32];
  SDL_Rect r;
  int x,y;
  r.x=r.y=0;
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
      draw_picture(0,y*picture_size+52,classes[inventory[y].class]->images[inventory[y].image]&0x7FFF);
    }
  } else {
    // Move list
    snprintf(buf,8,"%5d",replay_pos);
    draw_text(8,52,buf,0xF0,0xF9);
    snprintf(buf,8,"%5d",replay_count);
    draw_text(8,screen->h-8,buf,0xF0,0xFC);
    for(y=44,x=replay_pos-(screen->h-68)/32;;x++) {
      y+=16;
      if(y+24>screen->h) break;
      if(x>=0 && x<replay_count) draw_key(16,y,replay_list[x],0xF8,0xFB);
      if(x==replay_count) draw_key(16,y,1,0xF0,0xF8);
      if(x==replay_pos) draw_text(0,y,"~~",0xF0,0xFE);
      if(x==replay_mark) draw_text(32,y,"~~",0xF0,0xFD);







|







106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
      draw_picture(0,y*picture_size+52,classes[inventory[y].class]->images[inventory[y].image]&0x7FFF);
    }
  } else {
    // Move list
    snprintf(buf,8,"%5d",replay_pos);
    draw_text(8,52,buf,0xF0,0xF9);
    snprintf(buf,8,"%5d",replay_count);
    draw_text(8,screen->h-8,buf,0xF0,solution_replay?0xFA:0xFC);
    for(y=44,x=replay_pos-(screen->h-68)/32;;x++) {
      y+=16;
      if(y+24>screen->h) break;
      if(x>=0 && x<replay_count) draw_key(16,y,replay_list[x],0xF8,0xFB);
      if(x==replay_count) draw_key(16,y,1,0xF0,0xF8);
      if(x==replay_pos) draw_text(0,y,"~~",0xF0,0xFE);
      if(x==replay_mark) draw_text(32,y,"~~",0xF0,0xFD);
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
  }
  SDL_LockSurface(screen);
  draw_text(0,40,buf,0xF0,0xF1);
  SDL_UnlockSurface(screen);
  SDL_Flip(screen);
}

static void end_level(void) {
  long sz=replay_size;
  if(!replay_list) return;
  if(sz<replay_count+4) {
    replay_list=realloc(replay_list,sz=replay_count+4);
    if(!replay_list) fatal("Allocation failed\n");
    replay_size=(sz>0xFFFF?0xFFFF:sz);
  }
  sz=replay_count+4;
  replay_list[sz-4]=replay_mark>>8;
  replay_list[sz-3]=replay_mark;
  replay_list[sz-2]=replay_count>>8;
  replay_list[sz-1]=replay_count;
  write_userstate(FIL_LEVEL,level_id,sz,replay_list);
}







































static void begin_level(int id) {
  const char*t;
  long sz;
  if(replay_count) end_level();
  inputs_count=0;
  replay_pos=0;
  t=load_level(id)?:init_level();
  free(replay_list);
  replay_list=read_userstate(FIL_LEVEL,level_id,&sz);
  if(sz>=2) {
    replay_size=(sz>0xFFFF?0xFFFF:sz);
    replay_count=(replay_list[sz-2]<<8)|replay_list[sz-1];
    if(sz-replay_count>=4) replay_mark=(replay_list[replay_count]<<8)|replay_list[replay_count+1]; else replay_mark=0;
  } else {
    replay_count=replay_mark=replay_size=0;
    free(replay_list);
    replay_list=0;
  }
  if(t) {
    gameover=-1;
    screen_message(t);
  } else {
    gameover=0;
  }
  timerflag=0;







|

|













>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


<
|



<
<
<
<
<
<
<
<
<
|
<







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
266
267
268
  }
  SDL_LockSurface(screen);
  draw_text(0,40,buf,0xF0,0xF1);
  SDL_UnlockSurface(screen);
  SDL_Flip(screen);
}

static void save_replay(void) {
  long sz=replay_size;
  if(solution_replay || !replay_list) return;
  if(sz<replay_count+4) {
    replay_list=realloc(replay_list,sz=replay_count+4);
    if(!replay_list) fatal("Allocation failed\n");
    replay_size=(sz>0xFFFF?0xFFFF:sz);
  }
  sz=replay_count+4;
  replay_list[sz-4]=replay_mark>>8;
  replay_list[sz-3]=replay_mark;
  replay_list[sz-2]=replay_count>>8;
  replay_list[sz-1]=replay_count;
  write_userstate(FIL_LEVEL,level_id,sz,replay_list);
}

static void load_replay(void) {
  long sz;
  int i;
  free(replay_list);
  if(solution_replay) {
    replay_list=read_lump(FIL_SOLUTION,level_id,&sz);
    // Solution format: version (16-bits), flag (8-bits), user name (null-terminated), timestamp (64-bits), move list
    if(sz>3) {
      i=replay_list[0]|(replay_list[1]<<8);
      if(i!=level_version) goto notfound;
      i=3;
      if(replay_list[2]&1) {
        while(i<sz && replay_list[i]) i++;
        i++;
      }
      if(replay_list[2]&2) i+=8;
      if(i>=sz || sz-i>0xFFFF) goto notfound;
      replay_size=(sz>0xFFFF?0xFFFF:sz);
      memmove(replay_list,replay_list+i,replay_count=sz-i);
      replay_mark=0;
    } else {
      goto notfound;
    }
  } else {
    replay_list=read_userstate(FIL_LEVEL,level_id,&sz);
    if(sz>=2) {
      replay_size=(sz>0xFFFF?0xFFFF:sz);
      replay_count=(replay_list[sz-2]<<8)|replay_list[sz-1];
      if(sz-replay_count>=4) replay_mark=(replay_list[replay_count]<<8)|replay_list[replay_count+1]; else replay_mark=0;
    } else {
      notfound:
      replay_count=replay_mark=replay_size=0;
      free(replay_list);
      replay_list=0;
    }
  }
}

static void begin_level(int id) {
  const char*t;

  if(replay_count) save_replay();
  inputs_count=0;
  replay_pos=0;
  t=load_level(id)?:init_level();









  load_replay();

  if(t) {
    gameover=-1;
    screen_message(t);
  } else {
    gameover=0;
  }
  timerflag=0;
484
485
486
487
488
489
490




491
492
493
494
495
496
497
  modal_draw_popup(s);
  sqlite3_free(s);
}

static int game_command(int prev,int cmd,int number,int argc,sqlite3_stmt*args,void*aux) {
  switch(cmd) {
    case '\' ': // Play a move




      if(inputs_count>=inputs_size) {
        inputs=realloc(inputs,inputs_size+=32);
        if(!inputs) fatal("Allocation failed\n");
      }
      inputs[inputs_count++]=number;
      return 0;
    case '+ ': replay: // Replay







>
>
>
>







513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  modal_draw_popup(s);
  sqlite3_free(s);
}

static int game_command(int prev,int cmd,int number,int argc,sqlite3_stmt*args,void*aux) {
  switch(cmd) {
    case '\' ': // Play a move
      if(solution_replay) {
        screen_message("You cannot play your own moves during the solution replay");
        return -3;
      }
      if(inputs_count>=inputs_size) {
        inputs=realloc(inputs,inputs_size+=32);
        if(!inputs) fatal("Allocation failed\n");
      }
      inputs[inputs_count++]=number;
      return 0;
    case '+ ': replay: // Replay
540
541
542
543
544
545
546





547
548
549
550
551
552
553
      return prev;
    case '^d': // Describe object
      describe_at(number-65);
      return prev;
    case '^o': // List objects
      list_objects_at(number-65);
      return prev;





    case 'go': // Select level
      begin_level(number);
      return 1;
    default:
      return prev;
  }
}







>
>
>
>
>







573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
      return prev;
    case '^d': // Describe object
      describe_at(number-65);
      return prev;
    case '^o': // List objects
      list_objects_at(number-65);
      return prev;
    case '^s': // Toggle solution replay
      solution_replay^=1;
      replay_count=0;
      if(replay_count) begin_level(level_id); else load_replay();
      return 1;
    case 'go': // Select level
      begin_level(number);
      return 1;
    default:
      return prev;
  }
}
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
        }
        redraw_game();
        timerflag=0; // ensure we have not missed a timer event
        break;
    }
  }
  quit:
  end_level();
  exit(0);
}

void locate_me(int x,int y) {
  Uint8 c=7;
  SDL_Rect r,rh,rv;
  SDL_Event ev;







|







696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
        }
        redraw_game();
        timerflag=0; // ensure we have not missed a timer event
        break;
    }
  }
  quit:
  save_replay();
  exit(0);
}

void locate_me(int x,int y) {
  Uint8 c=7;
  SDL_Rect r,rh,rv;
  SDL_Event ev;

Modified heromesh.h from [c906f5298c] to [f7c90cf74d].

254
255
256
257
258
259
260

261
262
263
264
265
266
267
268
269
const char*execute_turn(int key);
const char*init_level(void);

// == game ==

extern Uint8*replay_list;
extern Uint16 replay_size,replay_count,replay_pos,replay_mark;


void run_game(void);
void locate_me(int x,int y);

// == edit ==

void run_editor(void);
void write_empty_level_set(FILE*);








>









254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const char*execute_turn(int key);
const char*init_level(void);

// == game ==

extern Uint8*replay_list;
extern Uint16 replay_size,replay_count,replay_pos,replay_mark;
extern Uint8 solution_replay;

void run_game(void);
void locate_me(int x,int y);

// == edit ==

void run_editor(void);
void write_empty_level_set(FILE*);