Free Hero Mesh

Diff
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.

Differences From Artifact [d19625c559]:

To Artifact [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;