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 [620e67e86b]:

To Artifact [2ba6e49ee7]:


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
Uint16 get_message_ptr(int c,int m) {
  if(!classes[c]) return 0xFFFF;
  if(classes[c]->nmsg<=m) return 0xFFFF;
  return classes[c]->messages[m];
}

static void set_message_ptr(int c,int m,int p) {
  if(m>classes[c]->nmsg) {
    classes[c]->messages=realloc(classes[c]->messages,(m+1)*sizeof(Uint16));
    if(!classes[c]->messages) fatal("Allocation failed\n");
    while(m>classes[c]->nmsg) classes[c]->messages[classes[c]->nmsg++]=0xFFFF;
  }
  classes[c]->messages[m]=p;
}

static int look_class_name(void) {
  int i;
  int u=undef_class;







|


|







245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
Uint16 get_message_ptr(int c,int m) {
  if(!classes[c]) return 0xFFFF;
  if(classes[c]->nmsg<=m) return 0xFFFF;
  return classes[c]->messages[m];
}

static void set_message_ptr(int c,int m,int p) {
  if(m>=classes[c]->nmsg) {
    classes[c]->messages=realloc(classes[c]->messages,(m+1)*sizeof(Uint16));
    if(!classes[c]->messages) fatal("Allocation failed\n");
    while(m>=classes[c]->nmsg) classes[c]->messages[classes[c]->nmsg++]=0xFFFF;
  }
  classes[c]->messages[m]=p;
}

static int look_class_name(void) {
  int i;
  int u=undef_class;
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
  cl->codes=realloc(cl->codes,0x10000*sizeof(Uint16));
  if(!cl->codes) fatal("Allocation failed\n");
  for(;;) {
    nxttok();
    if(Tokenf(TF_MACRO)) ParseError("Unexpected macro\n");
    if(Tokenf(TF_INT)) {
      numeric:
      if(!(tokenv&~0xFF)) AddInst(tokenv);
      else if(!((tokenv-1)&tokenv)) AddInst(0x87E0+__builtin_ctz(tokenv));
      else if(!(tokenv&~0xFFFF)) AddInst2(OP_INT16,tokenv);
      else if(-256<(Sint32)tokenv) AddInst(tokenv&0x01FF);
      else AddInst(OP_INT32),AddInst2(tokenv>>16,tokenv);
    } else if(Tokenf(TF_NAME)) {
      switch(tokenv) {
        AbbrevOp(OP_ADD,0x00);
        AbbrevOp(OP_SUB,0x08);
        AbbrevOp(OP_MUL,0x10);
        AbbrevOp(OP_DIV,0x18);







|

|
|







900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
  cl->codes=realloc(cl->codes,0x10000*sizeof(Uint16));
  if(!cl->codes) fatal("Allocation failed\n");
  for(;;) {
    nxttok();
    if(Tokenf(TF_MACRO)) ParseError("Unexpected macro\n");
    if(Tokenf(TF_INT)) {
      numeric:
      if(!(tokenv&~0xFFL)) AddInst(tokenv);
      else if(!((tokenv-1)&tokenv)) AddInst(0x87E0+__builtin_ctz(tokenv));
      else if(!(tokenv&~0xFFFFL)) AddInst2(OP_INT16,tokenv);
      else if((tokenv&0x80000000L) && -256<(Sint32)tokenv) AddInst(tokenv&0x01FF);
      else AddInst(OP_INT32),AddInst2(tokenv>>16,tokenv);
    } else if(Tokenf(TF_NAME)) {
      switch(tokenv) {
        AbbrevOp(OP_ADD,0x00);
        AbbrevOp(OP_SUB,0x08);
        AbbrevOp(OP_MUL,0x10);
        AbbrevOp(OP_DIV,0x18);
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
          break;
        case OP_NEXT:
          FlowPop(OP_FOR);
          AddInst(OP_NEXT);
          cl->codes[flowptr[flowdepth]]=ptr;
          break;
        case OP_STRING:
          
          break;
        default:
          if(Tokenf(TF_ABNORMAL)) ParseError("Invalid instruction token\n");
          if(compat && Tokenf(TF_COMPAT)) ++tokenv;
          AddInstF(tokenv,tokent);
      }
    } else if(Tokenf(TF_FUNCTION)) {







|







1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
          break;
        case OP_NEXT:
          FlowPop(OP_FOR);
          AddInst(OP_NEXT);
          cl->codes[flowptr[flowdepth]]=ptr;
          break;
        case OP_STRING:
          AddInst2(OP_STRING,pool_string(tokenstr));
          break;
        default:
          if(Tokenf(TF_ABNORMAL)) ParseError("Invalid instruction token\n");
          if(compat && Tokenf(TF_COMPAT)) ++tokenv;
          AddInstF(tokenv,tokent);
      }
    } else if(Tokenf(TF_FUNCTION)) {
1070
1071
1072
1073
1074
1075
1076

1077
1078
1079
1080
1081
1082
1083
          tokenv=x;
          goto numeric;
        default:
          ParseError("Invalid parenthesized instruction\n");
      }
    } else if(tokent==TF_CLOSE) {
      if(peep<ptr && cl->codes[ptr-1]==OP_RET) break;

      if(Inst8bit()) ChangeInst(+=0x1E00);
      else AddInst(OP_RET);
      break;
    } else if(Tokenf(TF_EOF)) {
      ParseError("Unexpected end of file\n");
    } else {
      ParseError("Invalid instruction token\n");







>







1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
          tokenv=x;
          goto numeric;
        default:
          ParseError("Invalid parenthesized instruction\n");
      }
    } else if(tokent==TF_CLOSE) {
      if(peep<ptr && cl->codes[ptr-1]==OP_RET) break;
      if(peep<ptr && (cl->codes[ptr-1]&0xFF00)==0x1E00) break;
      if(Inst8bit()) ChangeInst(+=0x1E00);
      else AddInst(OP_RET);
      break;
    } else if(Tokenf(TF_EOF)) {
      ParseError("Unexpected end of file\n");
    } else {
      ParseError("Invalid instruction token\n");
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
  if(hash && cl->uservars) {
    printf("  Variables:\n");
    for(i=0;i<LOCAL_HASH_SIZE;i++) {
      if(hash[i].id>=0x2000 && hash[i].id<0x3000) printf("    %%%s = 0x%04X\n",hash[i].txt,hash[i].id);
    }
  }
  if(endptr && cl->codes) {
    printf("  Codes:\n");
    for(i=0;i<endptr;i++) {
      if(!(i&16)) printf("    [%04X]",i);
      printf(" %04X",cl->codes[i]);
    }
    putchar('\n');
  }
  printf("---\n\n");
}








|

|







1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
  if(hash && cl->uservars) {
    printf("  Variables:\n");
    for(i=0;i<LOCAL_HASH_SIZE;i++) {
      if(hash[i].id>=0x2000 && hash[i].id<0x3000) printf("    %%%s = 0x%04X\n",hash[i].txt,hash[i].id);
    }
  }
  if(endptr && cl->codes) {
    printf("  Codes:");
    for(i=0;i<endptr;i++) {
      if(!(i&15)) printf("\n    [%04X]",i);
      printf(" %04X",cl->codes[i]);
    }
    putchar('\n');
  }
  printf("---\n\n");
}

1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175

1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204







1205





1206
1207

1208





















1209





















































































1210
1211
1212
1213
1214
1215
1216
  nxttok();
  if(Tokenf(TF_NAME) && tokenv==OP_INPLACE) {
    nxttok();
    if(tokent!=TF_CLOSE) ParseError("Close parentheses expected\n");
    return 1<<12;
  }
  for(i=0;i<25;i++) {
    nxttok();
    if(!Tokenf(TF_INT) || (tokenv&~1)) ParseError("Expected 0 or 1\n");
    if(tokenv) n|=1<<(i+4-2*(i%5));
  }
  nxttok();

  if(tokent!=TF_CLOSE) ParseError("Close parentheses expected\n");
  return n;
}

static void class_def_hard(Uint16*data) {
  int i;
  for(;;) {
    nxttok();
    if(tokent==TF_CLOSE) {
      return;
    } else if(tokent==TF_OPEN) {
      nxttok();
      if(!Tokenf(TF_DIR) || tokenv>7 || (tokenv&1)) ParseError("Expected even absolute direction\n");
      i=tokenv>>1;
      nxttok();
      if(tokent!=TF_INT || (tokenv&~0xFFFF)) ParseError("Sixteen-bit number expected\n");
      data[i]=tokenv;
      nxttok();
      if(tokent!=TF_CLOSE) ParseError("Close parentheses expected\n");
    } else if(tokent==TF_INT) {
      if(tokenv&~0xFFFF) ParseError("Hardness/sharpness must be a 16-bit number\n");
      data[0]=data[1]=data[2]=data[3]=tokenv;
    } else {
      ParseError("Expected ( or ) or number\n");
    }
  }
}

static inline Uint8 class_def_shovable(void) {







  





}


static inline Uint8 class_def_shape(void) {





















  





















































































}

static void class_definition(int cla) {
  Hash*hash=calloc(LOCAL_HASH_SIZE,sizeof(Hash));
  Class*cl=classes[cla];
  int ptr=0;
  int compat=0;







<


<
|
>















|













>
>
>
>
>
>
>
|
>
>
>
>
>
|
|
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1165
1166
1167
1168
1169
1170
1171

1172
1173

1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
  nxttok();
  if(Tokenf(TF_NAME) && tokenv==OP_INPLACE) {
    nxttok();
    if(tokent!=TF_CLOSE) ParseError("Close parentheses expected\n");
    return 1<<12;
  }
  for(i=0;i<25;i++) {

    if(!Tokenf(TF_INT) || (tokenv&~1)) ParseError("Expected 0 or 1\n");
    if(tokenv) n|=1<<(i+4-2*(i%5));

    nxttok();
  }
  if(tokent!=TF_CLOSE) ParseError("Close parentheses expected\n");
  return n;
}

static void class_def_hard(Uint16*data) {
  int i;
  for(;;) {
    nxttok();
    if(tokent==TF_CLOSE) {
      return;
    } else if(tokent==TF_OPEN) {
      nxttok();
      if(!Tokenf(TF_DIR) || tokenv>7 || (tokenv&1)) ParseError("Expected even absolute direction\n");
      i=tokenv>>1;
      nxttok();
      if(tokent!=TF_INT || (tokenv&~0xFFFF)) ParseError("Hardness/sharpness must be a 16-bit number\n");
      data[i]=tokenv;
      nxttok();
      if(tokent!=TF_CLOSE) ParseError("Close parentheses expected\n");
    } else if(tokent==TF_INT) {
      if(tokenv&~0xFFFF) ParseError("Hardness/sharpness must be a 16-bit number\n");
      data[0]=data[1]=data[2]=data[3]=tokenv;
    } else {
      ParseError("Expected ( or ) or number\n");
    }
  }
}

static inline Uint8 class_def_shovable(void) {
  Uint8 n=0;
  nxttok();
  if(tokent==TF_INT) {
    n=tokenv;
    nxttok();
    if(tokent!=TF_CLOSE) ParseError("Close parentheses expected\n");
    return n;
  }
  for(;;) {
    if(tokent==TF_CLOSE) return n;
    if(!Tokenf(TF_DIR) || tokenv>7 || (tokenv&1)) ParseError("Expected even absolute direction\n");
    n|=1<<tokenv;
    nxttok();
  }
}

static inline Uint8 class_def_shape(void) {
  Uint8 n=0;
  int i;
  for(;;) {
    nxttok();
    if(tokent==TF_CLOSE) {
      return n;
    } else if(tokent==TF_OPEN) {
      nxttok();
      if(!Tokenf(TF_DIR) || tokenv>7 || (tokenv&1)) ParseError("Expected even absolute direction\n");
      i=tokenv;
      n&=~(3<<i);
      nxttok();
      if(tokent!=TF_INT || (tokenv&~3)) ParseError("Shape must be 0, 1, 2, or 3\n");
      n|=tokenv<<i;
      nxttok();
      if(tokent!=TF_CLOSE) ParseError("Close parentheses expected\n");
    } else if(tokent==TF_INT) {
      if(tokenv&~3) ParseError("Shape must be 0, 1, 2, or 3\n");
      n=tokenv*0x55;
    } else {
      ParseError("Expected ( or ) or number\n");
    }
  }
}

static char*class_def_help(void) {
  char*txt=malloc(0x3000);
  int n=0;
  int i;
  for(;;) {
    nxttok();
    if(tokent==TF_CLOSE) break;
    if(!Tokenf(TF_NAME) || tokenv!=OP_STRING) ParseError("String expected\n");
    i=strlen(tokenstr);
    if(i+n>=0x2FFD) ParseError("Help text is too long\n");
    strcpy(txt+n,tokenstr);
    n+=i;
  }
  if(!n) {
    free(txt);
    return 0;
  }
  txt[n]=0;
  return realloc(txt,n+1)?:txt;
}

static void class_def_image(int cla) {
  Class*cl=classes[cla];
  Uint16*img=malloc(256*sizeof(Uint16));
  sqlite3_stmt*st;
  if(cl->nimages || cl->images) ParseError("Duplicate (Image) block\n");
  if(!img) fatal("Allocation failed\n");
  if(userdb && sqlite3_prepare_v2(userdb,"SELECT `ID` FROM `PICTURES` WHERE `NAME` = ?1;",-1,&st,0)) fatal("SQL error: %s\n",sqlite3_errmsg(userdb));
  for(;;) {
    nxttok();
    if(tokent==TF_CLOSE) break;
    if(cl->nimages==255) ParseError("Too many images in class\n");
    if(!Tokenf(TF_NAME) || tokenv!=OP_STRING) ParseError("String expected\n");
    if(userdb) {
      sqlite3_bind_text(st,1,tokenstr,-1,0);
      if(sqlite3_step(st)==SQLITE_ROW) {
        img[cl->nimages++]=sqlite3_column_int(st,0)|0x8000;
      } else {
        ParseError("A picture named \"%s\" does not exist\n",tokenstr);
      }
      sqlite3_reset(st);
      sqlite3_clear_bindings(st);
    } else {
      img[cl->nimages++]=0;
    }
  }
  if(userdb) sqlite3_finalize(st);
  if(!cl->nimages) {
    free(img);
    return;
  }
  cl->images=realloc(img,cl->nimages*sizeof(Uint16))?:img;
}

static void class_def_defaultimage(int cla) {
  Class*cl=classes[cla];
  int i;
  for(i=0;i<cl->nimages;i++) cl->images[i]&=0x7FFF;
  for(;;) {
    nxttok();
    if(tokent==TF_OPEN) {
      nxttok();
      if(tokent==TF_INT) {
        if(tokenv<0 || tokenv>=cl->nimages) ParseError("Image number out of range\n");
        i=tokenv;
        nxttok();
        if(tokent!=TF_INT) ParseError("Number expected\n");
        if(tokenv<i) ParseError("Backward range in (DefaultImage) block\n");
        if(tokenv>=cl->nimages) ParseError("Image number out of range\n");
        while(i<=tokenv) cl->images[i++]|=0x8000;
      } else if(tokent!=TF_CLOSE) {
        ParseError("Number expected\n");
      }
    } else if(tokent==TF_CLOSE) {
      break;
    } else if(tokent==TF_INT) {
      if(tokenv<0 || tokenv>=cl->nimages) ParseError("Image number out of range\n");
      cl->images[tokenv]|=0x8000;
    } else {
      ParseError("Expected ( or ) or number\n");
    }
  }
}

static void class_definition(int cla) {
  Hash*hash=calloc(LOCAL_HASH_SIZE,sizeof(Hash));
  Class*cl=classes[cla];
  int ptr=0;
  int compat=0;
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243

1244
1245
1246

1247
1248
1249
1250
1251
1252
1253
    } else if(Tokenf(TF_MACRO)) {
      ParseError("Unexpected macro token\n");
    } else if(Tokenf(TF_OPEN)) {
      nxttok();
      if(Tokenf(TF_NAME)) {
        switch(tokenv) {
          case OP_IMAGE:
            
            break;
          case OP_DEFAULTIMAGE:
            
            break;
          case OP_HELP:
            

            break;
          case OP_EDITORHELP:
            

            break;
          case OP_HEIGHT:
            cl->height=class_def_number();
            break;
          case OP_WEIGHT:
            cl->weight=class_def_number();
            break;







|


|


|
>


|
>







1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
    } else if(Tokenf(TF_MACRO)) {
      ParseError("Unexpected macro token\n");
    } else if(Tokenf(TF_OPEN)) {
      nxttok();
      if(Tokenf(TF_NAME)) {
        switch(tokenv) {
          case OP_IMAGE:
            class_def_image(cla);
            break;
          case OP_DEFAULTIMAGE:
            class_def_defaultimage(cla);
            break;
          case OP_HELP:
            if(cl->gamehelp) ParseError("Duplicate (Help) block\n");
            cl->gamehelp=class_def_help();
            break;
          case OP_EDITORHELP:
            if(cl->edithelp) ParseError("Duplicate (EditorHelp) block\n");
            cl->edithelp=class_def_help();
            break;
          case OP_HEIGHT:
            cl->height=class_def_number();
            break;
          case OP_WEIGHT:
            cl->weight=class_def_number();
            break;
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
      }
    } else if(Tokenf(TF_CLOSE)) {
      break;
    } else {
      ParseError("Invalid directly inside of a class definition\n");
    }
  }
  end_label_stack(classes[0]->codes,hash);
  if(!cl->nimages) cl->oflags|=OF_INVISIBLE;
  if(main_options['C']) dump_class(cla,ptr,hash);
  if(main_options['H']) {
    for(i=0;i<LOCAL_HASH_SIZE;i++) if(hash[i].id) printf(" \"%s\": %04X\n",hash[i].txt,hash[i].id);
  }
  for(i=0;i<LOCAL_HASH_SIZE;i++) free(hash[i].txt);
  free(hash);







|







1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
      }
    } else if(Tokenf(TF_CLOSE)) {
      break;
    } else {
      ParseError("Invalid directly inside of a class definition\n");
    }
  }
  end_label_stack(cl->codes,hash);
  if(!cl->nimages) cl->oflags|=OF_INVISIBLE;
  if(main_options['C']) dump_class(cla,ptr,hash);
  if(main_options['H']) {
    for(i=0;i<LOCAL_HASH_SIZE;i++) if(hash[i].id) printf(" \"%s\": %04X\n",hash[i].txt,hash[i].id);
  }
  for(i=0;i<LOCAL_HASH_SIZE;i++) free(hash[i].txt);
  free(hash);