Free Hero Mesh

Check-in [04cceae6dc]
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 importing levels
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 04cceae6dcf4ae62bdd3bd6738bbfda9e0b2f0a5
User & Date: user on 2021-03-11 05:38:29
Other Links: manifest | tags
Context
2021-03-11
07:38
Implement the "chain" instruction check-in: 83764e9bf8 user: user tags: trunk
05:38
Implement importing levels check-in: 04cceae6dc user: user tags: trunk
2021-03-09
23:15
Implement the %C substitution, and allow %s substitutions to display message names as well. check-in: 5cdca70402 user: user tags: trunk
Changes

Modified bindings.doc from [d258a8579c] to [ee76aafc91].

120
121
122
123
124
125
126




127
128
129
130
131
132
133
  Add an object with the current MRU values to that location, even if
  there is already another object of the same class at that location.

'ex' <command>
  Export a level (given the operating system command to receive the
  exported data). See export.doc for details.





'lc' <number>
  Set the level code number. This is a 16-bit number which can be used in
  the class codes of your puzzle set for whatever purpose you wish; the
  game engine does not use this value itself.

'lv' <number>
  Set the level version number. This number is used to detect the validity







>
>
>
>







120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  Add an object with the current MRU values to that location, even if
  there is already another object of the same class at that location.

'ex' <command>
  Export a level (given the operating system command to receive the
  exported data). See export.doc for details.

'im' <command>
  Import a level (given the operating system command to produce the data
  to be imported). See export.doc for details.

'lc' <number>
  Set the level code number. This is a 16-bit number which can be used in
  the class codes of your puzzle set for whatever purpose you wish; the
  game engine does not use this value itself.

'lv' <number>
  Set the level version number. This number is used to detect the validity

Modified default.heromeshrc from [ed90c9d16e] to [75fb89c738].

143
144
145
146
147
148
149

150
151
152
153
154
155
156
157
158
159
160
161
?.editKey.R: select 're',substr(:resize,1,instr(:resize,'x')-1),substr(:resize,instr(:resize,'x')+1) where length(:resize);
?.editKey.ctrl.X: select 're',pfwidth(),pfheight();
?.editKey.ctrl.P: ^P
?.editKey.ctrl.Q: ^Q
?.editKey.ctrl.S: ^S
?.editKey.space: ^c
?.editKey.return: ^e

?.editKey.f2: select 'ex',:Export_Level;
?.editClick.left: ^a
?.editClick.alt.left: ^u
?.editClick.right: delete from objects where x=$X and y=$Y and up is null;

! Global key bindings
?.?.kp_minus: select 'go',-(level()-1);
?.?.kp_plus: select 'go',-(level()+1);
?.?.ctrl.G: select 'go',-:Go_To_Level where :Go_To_Level=cast(:Go_To_Level as int) and cast(:Go_To_Level as int)>0;
?.?.shift.ctrl.M: select ':s';
?.?.f10: select ':x';








>












143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
?.editKey.R: select 're',substr(:resize,1,instr(:resize,'x')-1),substr(:resize,instr(:resize,'x')+1) where length(:resize);
?.editKey.ctrl.X: select 're',pfwidth(),pfheight();
?.editKey.ctrl.P: ^P
?.editKey.ctrl.Q: ^Q
?.editKey.ctrl.S: ^S
?.editKey.space: ^c
?.editKey.return: ^e
?.editKey.f1: select 'im',:Import_Level;
?.editKey.f2: select 'ex',:Export_Level;
?.editClick.left: ^a
?.editClick.alt.left: ^u
?.editClick.right: delete from objects where x=$X and y=$Y and up is null;

! Global key bindings
?.?.kp_minus: select 'go',-(level()-1);
?.?.kp_plus: select 'go',-(level()+1);
?.?.ctrl.G: select 'go',-:Go_To_Level where :Go_To_Level=cast(:Go_To_Level as int) and cast(:Go_To_Level as int)>0;
?.?.shift.ctrl.M: select ':s';
?.?.f10: select ':x';

Modified edit.c from [d19625c559] to [2aa028c0b1].

734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
}

static void export_level(const char*cmd) {
  int i;
  Uint32 n;
  Object*o;
  FILE*fp;
  if(!cmd) return;
  fp=popen(cmd,"w");
  if(!fp) {
    screen_message("Cannot open pipe");
    return;
  }
  fprintf(fp,"; Free Hero Mesh exported level ID=%d ORD=%d\n",level_id,level_ord);
  fprint_esc(fp,'@',level_title);







|







734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
}

static void export_level(const char*cmd) {
  int i;
  Uint32 n;
  Object*o;
  FILE*fp;
  if(!cmd || !*cmd) return;
  fp=popen(cmd,"w");
  if(!fp) {
    screen_message("Cannot open pipe");
    return;
  }
  fprintf(fp,"; Free Hero Mesh exported level ID=%d ORD=%d\n",level_id,level_ord);
  fprint_esc(fp,'@',level_title);
758
759
760
761
762
763
764




















































































































































































































765
766
767
768
769
770
771
      fprintf(fp," %d\n",o->dir);
      n=o->up;
    }
  }
  for(i=0;i<nlevelstrings;i++) fprint_esc(fp,'%',levelstrings[i]);
  pclose(fp);
}





















































































































































































































static int editor_command(int prev,int cmd,int number,int argc,sqlite3_stmt*args,void*aux) {
  int x,y;
  switch(cmd) {
    case '^a': // Add object (no duplicates)
      if(prev) return prev;
      add_object_at(number&63?:64,number/64?:64,mru+curmru,1);







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







758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
      fprintf(fp," %d\n",o->dir);
      n=o->up;
    }
  }
  for(i=0;i<nlevelstrings;i++) fprint_esc(fp,'%',levelstrings[i]);
  pclose(fp);
}

static char*import_numbers(char*p,int*x,int*y) {
  if(!p) return 0;
  while(*p==' ' || *p=='\t') p++;
  if(*p<'0' || *p>'9') return 0;
  *x=strtol(p,&p,10);
  if(*p && *p!=' ' && *p!='\t') return 0;
  while(*p==' ' || *p=='\t') p++;
  if(!y) return p;
  if(*p<'0' || *p>'9') return 0;
  *y=strtol(p,&p,10);
  if(*p && *p!=' ' && *p!='\t') return 0;
  while(*p==' ' || *p=='\t') p++;
  return p;
}

static char*import_value(char*p,Value*v) {
  int i;
  int n=strcspn(p," \t");
  if(!p) return 0;
  while(*p==' ' || *p=='\t') p++;
  if(*p>='0' && *p<='9') {
    v->t=TY_NUMBER;
    v->u=strtol(p,&p,10)&0xFFFF;
  } else if(*p=='$') {
    v->t=TY_CLASS;
    p++; n--;
    for(i=1;i<0x4000;i++) {
      if(classes[i] && !(classes[i]->cflags&CF_NOCLASS2) && strlen(classes[i]->name)==n && !memcmp(p,classes[i]->name,n)) {
        v->u=i;
        p+=n;
        goto ok;
      }
    }
    return 0;
  } else if(*p=='%') {
    v->t=TY_LEVELSTRING;
    v->u=strtol(p+1,&p,10)&0xFFFF;
  } else if(*p=='#') {
    v->t=TY_MESSAGE;
    p++; n--;
    for(i=0;i<0x4000;i++) {
      if(messages[i] && strlen(messages[i])==n && !memcmp(p,messages[i],n)) {
        v->u=i+256;
        p+=n;
        goto ok;
      }
    }
    return 0;
  } else if(*p>='A' && *p<='Z') {
    v->t=TY_MESSAGE;
    for(i=0;i<N_MESSAGES;i++) {
      if(strlen(standard_message_names[i])==n && !memcmp(p,standard_message_names[i],n)) {
        v->u=i;
        p+=n;
        goto ok;
      }
    }
    return 0;
  } else {
    return 0;
  }
  ok: if(*p==' ' || *p=='\t') return p+1; else return 0;
}

static char*import_string(char*p) {
  char isimg=0;
  char*s=malloc(strlen(p)+3);
  char*q=s;
  if(!s) fatal("Allocation failed\n");
  while(*p) {
    if(*p=='\\') {
      p++;
      if(isimg) {
        isimg=0;
        *q++='\\';
      } else {
        switch(*p++) {
          case '0' ... '7': *q++=*p+1-'0'; break;
          case 'b': *q++=15; break;
          case 'c': *q++=12; break;
          case 'i': *q++=14; isimg=1; break;
          case 'l': *q++=11; break;
          case 'n': *q++=10; break;
          case 'q': *q++=16; break;
          case 'x':
            *q++=31;
            *q=0;
            if(p[1]>='0' && p[1]<='9') *q=(p[1]-'0')<<4;
            else if(p[1]>='A' && p[1]<='F') *q=(p[1]+10-'A')<<4;
            else if(p[1]>='a' && p[1]<='f') *q=(p[1]+10-'a')<<4;
            else goto bad;
            if(p[2]>='0' && p[2]<='9') *q|=(p[2]-'0');
            else if(p[2]>='A' && p[2]<='F') *q|=(p[2]+10-'A');
            else if(p[2]>='a' && p[2]<='f') *q|=(p[2]+10-'a');
            else goto bad;
            p+=2; q++; break;
          default: *q++=*p;
        }
        p++;
      }
    } else {
      *q++=*p++;
    }
  }
  if(0) {
    bad:
    screen_message("Bad \\x escape");
  }
  if(isimg) *q++='\\';
  *q=0;
  return s;
}

static void import_level(const char*cmd) {
  char d=0;
  int x,y;
  Object*o;
  FILE*fp;
  size_t size=0;
  char*buf=0;
  char*p;
  Value v;
  if(!cmd || !*cmd) return;
  fp=popen(cmd,"r");
  if(!fp) {
    screen_message("Cannot open pipe");
    return;
  }
  level_changed=1;
  while(getline(&buf,&size,fp)>0) {
    p=buf;
    p[strcspn(p,"\r\n\f")]=0;
    p+=strspn(p,"\t ");
    switch(*p) {
      case '0' ... '9':
        if(!d) {
          missd:
          screen_message("Missing D line");
          goto done;
        }
        p=import_numbers(p,&x,&y);
        if(!p) goto bad;
        if(x<1 || x>pfwidth || y<1 || y>pfheight) {
          range:
          fprintf(stderr,"Imported level coordinates out of range: %d %d\n",x,y);
          screen_message("Coordinates out of range");
          goto done;
        }
        p=import_value(p,&v);
        if(!p || v.t!=TY_CLASS) goto bad;
        v.u=objalloc(v.u);
        if(v.u==VOIDLINK) goto bad;
        o=objects[v.u];
        o->x=x;
        o->y=y;
        p=import_value(p,&o->misc1);
        p=import_value(p,&o->misc2);
        p=import_value(p,&o->misc3);
        p=import_numbers(p,&x,0);
        o->dir=x;
        if((x&~7) || !p) {
          objtrash(v.u);
          goto bad;
        }
        pflink(v.u);
        break;
      case 'C':
        p=import_numbers(p+1,&x,0);
        if(!p || *p) goto bad;
        level_code=x;
        break;
      case 'D':
        p=import_numbers(p+1,&x,&y);
        if(!p || *p) goto bad;
        if(x<1 || x>64 || y<1 || y>64) goto range;
        if(d) {
          screen_message("Duplicate D line");
          goto done;
        }
        d=1;
        annihilate();
        pfwidth=x;
        pfheight=y;
        break;
      case '@':
        free(level_title);
        level_title=import_string(p+1);
        break;
      case '%':
        if(nlevelstrings>0x2000) {
          screen_message("Too many level strings");
        } else {
          levelstrings=realloc(levelstrings,(nlevelstrings+1)*sizeof(char*));
          if(!levelstrings) fatal("Allocation failed\n");
          levelstrings[nlevelstrings++]=import_string(p+1);
        }
        break;
      default:
        if(*p && *p!=';') {
          bad:
          fprintf(stderr,"Invalid record in imported data:  %s\n",buf);
          screen_message("Invalid record");
          goto done;
        }
    }
  }
  done:
  free(buf);
  pclose(fp);
  generation_number_inc=0;
}

static int editor_command(int prev,int cmd,int number,int argc,sqlite3_stmt*args,void*aux) {
  int x,y;
  switch(cmd) {
    case '^a': // Add object (no duplicates)
      if(prev) return prev;
      add_object_at(number&63?:64,number/64?:64,mru+curmru,1);
790
791
792
793
794
795
796




797
798
799
800
801
802
803
    case 'ex': // Export level
      if(argc<2) return prev;
      export_level(sqlite3_column_text(args,1));
      return prev;
    case 'go': // Select level
      load_level(number);
      return 1;




    case 'lc': // Set level code
      level_code=number;
      level_changed=1;
      return 0;
    case 'lv': // Set level version
      level_version=number;
      level_changed=0;







>
>
>
>







1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
    case 'ex': // Export level
      if(argc<2) return prev;
      export_level(sqlite3_column_text(args,1));
      return prev;
    case 'go': // Select level
      load_level(number);
      return 1;
    case 'im': // Import level
      if(argc<2) return prev;
      import_level(sqlite3_column_text(args,1));
      return 0;
    case 'lc': // Set level code
      level_code=number;
      level_changed=1;
      return 0;
    case 'lv': // Set level version
      level_version=number;
      level_changed=0;

Modified export.doc from [30a247908a] to [ec871d7ba2].

33
34
35
36
37
38
39
40


41
42
43
44
45
46
47
  line must come before any objects are listed, and is mandatory.

x y $class image misc1 misc2 misc3 dir
  Specify an object. Objects are listed starting from the top left corner,
  going right, and within each cell the objects are listed from bottom to
  top. Coordinates are one-based. The image is a number (0 for the first
  image), misc1 to misc3 are misc values (see below), and dir is also a
  number (0 for east, up to 7 for southeast).



%string
  A level string. The level strings are numbered in order, starting from
  string 0. The format is like that of the title.


=== Misc values ===







|
>
>







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  line must come before any objects are listed, and is mandatory.

x y $class image misc1 misc2 misc3 dir
  Specify an object. Objects are listed starting from the top left corner,
  going right, and within each cell the objects are listed from bottom to
  top. Coordinates are one-based. The image is a number (0 for the first
  image), misc1 to misc3 are misc values (see below), and dir is also a
  number (0 for east, up to 7 for southeast). When the data is being
  imported, it will accept objects listed in any order, but when being
  exported from Free Hero Mesh, they are ordered as described above.

%string
  A level string. The level strings are numbered in order, starting from
  string 0. The format is like that of the title.


=== Misc values ===