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 [b606dc980d]:

To Artifact [bcf1e42af2]:


611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
  Uint32 m;
  int i,xx,yy;
  Object*o;
  Object*p;
  Value v;
  if(n==VOIDLINK || (objects[n]->oflags&OF_DESTROYED)) return 0;
  o=objects[n];
  if(lastimage_processing && (classes[o->class]->cflags&CF_COMPATIBLE)) Throw("Can't move during animation processing");
  if(x<1 || y<1 || x>pfwidth || y>pfheight) return 0;
  if(v_bool(send_message(from,n,MSG_MOVING,NVALUE(x),NVALUE(y),NVALUE(0)))) return 0;
  if(classes[o->class]->cflags&CF_PLAYER) {
    m=lastobj;
    while(m!=VOIDLINK) {
      if(v_bool(send_message(n,m,MSG_PLAYERMOVING,NVALUE(x),NVALUE(y),OVALUE(from)))) return 0;
      m=objects[m]->prev;







|







611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
  Uint32 m;
  int i,xx,yy;
  Object*o;
  Object*p;
  Value v;
  if(n==VOIDLINK || (objects[n]->oflags&OF_DESTROYED)) return 0;
  o=objects[n];
  if(lastimage_processing) Throw("Can't move during animation processing");
  if(x<1 || y<1 || x>pfwidth || y>pfheight) return 0;
  if(v_bool(send_message(from,n,MSG_MOVING,NVALUE(x),NVALUE(y),NVALUE(0)))) return 0;
  if(classes[o->class]->cflags&CF_PLAYER) {
    m=lastobj;
    while(m!=VOIDLINK) {
      if(v_bool(send_message(n,m,MSG_PLAYERMOVING,NVALUE(x),NVALUE(y),OVALUE(from)))) return 0;
      m=objects[m]->prev;
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
  o=objects[obj];
  printf(" : %u %u [$%s %d %d]",o->generation,obj,classes[o->class]->name,o->x,o->y);
  printf(" : %u %u : %u %u\n",t1.t,t1.u,t2.t,t2.u);
}

static void flush_object(Uint32 n) {
  Object*o=objects[n];
  o->arrived=o->arrived2=o->departed=o->departed2=0;
  o->oflags&=~(OF_MOVED|OF_BUSY|OF_USERSIGNAL);
  o->inertia=0;
}

static void flush_class(Uint16 c) {
  Uint32 n,p;
  Object*o;
  if(lastobj==VOIDLINK) return;
  n=lastobj;
  while(o=objects[n]) {
    p=o->prev;
    if(!c || o->class==c) {
      o->arrived=o->arrived2=o->departed=o->departed2=0;
      o->oflags&=~(OF_MOVED|OF_BUSY|OF_USERSIGNAL);
      o->inertia=0;
    }
    if(p==VOIDLINK) break;
    n=p;
  }
}







|












|







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
  o=objects[obj];
  printf(" : %u %u [$%s %d %d]",o->generation,obj,classes[o->class]->name,o->x,o->y);
  printf(" : %u %u : %u %u\n",t1.t,t1.u,t2.t,t2.u);
}

static void flush_object(Uint32 n) {
  Object*o=objects[n];
  o->arrived=o->departed=0;
  o->oflags&=~(OF_MOVED|OF_BUSY|OF_USERSIGNAL);
  o->inertia=0;
}

static void flush_class(Uint16 c) {
  Uint32 n,p;
  Object*o;
  if(lastobj==VOIDLINK) return;
  n=lastobj;
  while(o=objects[n]) {
    p=o->prev;
    if(!c || o->class==c) {
      o->arrived=o->departed=0;
      o->oflags&=~(OF_MOVED|OF_BUSY|OF_USERSIGNAL);
      o->inertia=0;
    }
    if(p==VOIDLINK) break;
    n=p;
  }
}
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
  Uint32 i,j;
  Object*o=objects[obj];
  Value t1,t2;
  static Value t3,t4,t5;
  if(StackProtection()) Throw("Call stack overflow");
  // Note about bit shifting: At least when running Hero Mesh in DOSBOX, out of range bit shifts produce zero.
  // I don't know if this is true on all computers that Hero Mesh runs on, though. (Some documents suggest that x86 doesn't work this way)
  // The below code assumes that signed right shifting is available.
  for(;;) switch(code[ptr++]) {
    case 0x0000 ... 0x00FF: StackReq(0,1); Push(NVALUE(code[ptr-1])); break;
    case 0x0100 ... 0x01FF: StackReq(0,1); Push(NVALUE(code[ptr-1]-0x200)); break;
    case 0x0200 ... 0x02FF: StackReq(0,1); Push(MVALUE(code[ptr-1]&255)); break;
    case 0x0300 ... 0x03FF: StackReq(0,1); Push(UVALUE(code[ptr-1]&255,TY_SOUND)); break;
    case 0x0400 ... 0x04FF: StackReq(0,1); Push(UVALUE(code[ptr-1]&255,TY_USOUND)); break;
    case 0x1000 ... 0x107F: StackReq(1,1); t1=Pop(); Numeric(t1); t1.u+=code[ptr-1]&0x7F; Push(t1); break; // +







|







1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
  Uint32 i,j;
  Object*o=objects[obj];
  Value t1,t2;
  static Value t3,t4,t5;
  if(StackProtection()) Throw("Call stack overflow");
  // Note about bit shifting: At least when running Hero Mesh in DOSBOX, out of range bit shifts produce zero.
  // I don't know if this is true on all computers that Hero Mesh runs on, though. (Some documents suggest that x86 doesn't work this way)
  // The below code assumes that signed right shifting is available on the computer that Free Hero Mesh runs on.
  for(;;) switch(code[ptr++]) {
    case 0x0000 ... 0x00FF: StackReq(0,1); Push(NVALUE(code[ptr-1])); break;
    case 0x0100 ... 0x01FF: StackReq(0,1); Push(NVALUE(code[ptr-1]-0x200)); break;
    case 0x0200 ... 0x02FF: StackReq(0,1); Push(MVALUE(code[ptr-1]&255)); break;
    case 0x0300 ... 0x03FF: StackReq(0,1); Push(UVALUE(code[ptr-1]&255,TY_SOUND)); break;
    case 0x0400 ... 0x04FF: StackReq(0,1); Push(UVALUE(code[ptr-1]&255,TY_USOUND)); break;
    case 0x1000 ... 0x107F: StackReq(1,1); t1=Pop(); Numeric(t1); t1.u+=code[ptr-1]&0x7F; Push(t1); break; // +
1490
1491
1492
1493
1494
1495
1496
1497




















1498

1499

1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550

1551















1552




































1553







1554



1555











1556




1557
1558
1559

1560
1561
1562
1563
1564
1565
1566
    free(objects[i]);
  }
  nobjects=0;
  free(objects);
  objects=0;
  gameover=0;
}





















const char*execute_turn(int key) {

  Uint32 m,n;

  Value v;
  int i;
  if(!key) return 0;
  if(setjmp(my_env)) return my_error;
  if(quiz_text) {
    sqlite3_free(quiz_text);
    quiz_text=0;
    if(key_ignored) {
      if(quiz_obj.t) quiz_obj=NVALUE(0); else return 0;
    } else if(!quiz_obj.t) {
      move_number++;
      return 0;
    }
  }
  changed=0;
  key_ignored=0;
  all_flushed=0;
  lastimage_processing=0;
  vstackptr=0;
  current_key=key;
  for(n=0;n<nobjects;n++) if(objects[n]) {
    objects[n]->distance=0;
    objects[n]->oflags&=~(OF_KEYCLEARED|OF_DONE);
    if(objects[n]->anim) objects[n]->anim->count=0;
  }
  // Input phase
  m=VOIDLINK;
  v=NVALUE(0);
  if(!quiz_obj.t) {
    n=lastobj;
    while(n!=VOIDLINK) {
      i=classes[objects[n]->class]->cflags;
      if(i&CF_INPUT) v=send_message(VOIDLINK,n,MSG_KEY,NVALUE(key),v,NVALUE(0));
      if(i&CF_PLAYER) m=n;
      n=objects[n]->prev;
    }
  } else {
    n=quiz_obj.u;
    if(objects[n]->generation!=quiz_obj.t) n=VOIDLINK;
    quiz_obj=NVALUE(0);
    //TODO
    v=send_message(VOIDLINK,n,MSG_KEY,NVALUE(key),NVALUE(0),NVALUE(1));
  }
  current_key=0;
  if(key_ignored) {
    quiz_obj=NVALUE(0);
    return changed?"Invalid use of IgnoreKey":0;
  }
  move_number++;
  // Beginning phase
  if(!all_flushed) broadcast(m,0,MSG_BEGIN_TURN,m==VOIDLINK?NVALUE(objects[m]->x):NVALUE(0),m==VOIDLINK?NVALUE(objects[m]->y):NVALUE(0),v,0);

  // Trigger phase















  




































  // Ending phase







  



  // Animation phase











  




  // Cleanup phase
  for(n=0;n<nobjects;n++) if(objects[n] && (objects[n]->oflags&OF_DESTROYED)) objtrash(n);
  if(generation_number<=TY_MAXTYPE) return "Too many generations of objects";

  return 0;
}

const char*init_level(void) {
  if(setjmp(my_env)) return my_error;
  if(main_options['t']) printf("[Level %d restarted]\n",level_id);
  memcpy(globals,initglobals,sizeof(globals));








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

>
|
>




















|
|
|
|
















|










>

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

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



>







1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
    free(objects[i]);
  }
  nobjects=0;
  free(objects);
  objects=0;
  gameover=0;
}

static Uint8 execute_animation(Uint8 clock,Uint32 obj) {
  Object*o=objects[obj];
  Animation*a=o->anim;
  if(!(a->step[a->lstep].flag&ANI_ONCE)) return clock;
  if(a->ltime>=a->step[a->lstep].speed) {
    a->ltime=0;
    if(o->image==a->step[a->lstep].end) {
      a->status&=~ANISTAT_LOGICAL;
      if(classes[o->class]->cflags&CF_COMPATIBLE) lastimage_processing=1;
      send_message(VOIDLINK,obj,MSG_LASTIMAGE,NVALUE(0),NVALUE(0),NVALUE(0));
      lastimage_processing=0;
    } else {
      if(a->step[a->lstep].start>a->step[a->lstep].end) --o->image; else ++o->image;
    }
  }
  if(!(a->status&ANISTAT_LOGICAL) || !(a->step[a->lstep].flag&ANI_ONCE)) return clock;
  if(clock>a->step[a->lstep].speed-a->ltime) return a->step[a->lstep].speed-a->ltime;
  return clock;
}

const char*execute_turn(int key) {
  Uint8 busy,clock;
  Uint32 m,n,turn;
  Object*o;
  Value v;
  int i;
  if(!key) return 0;
  if(setjmp(my_env)) return my_error;
  if(quiz_text) {
    sqlite3_free(quiz_text);
    quiz_text=0;
    if(key_ignored) {
      if(quiz_obj.t) quiz_obj=NVALUE(0); else return 0;
    } else if(!quiz_obj.t) {
      move_number++;
      return 0;
    }
  }
  changed=0;
  key_ignored=0;
  all_flushed=0;
  lastimage_processing=0;
  vstackptr=0;
  current_key=key;
  for(n=0;n<nobjects;n++) if(o=objects[n]) {
    o->distance=0;
    o->oflags&=~(OF_KEYCLEARED|OF_DONE);
    if(o->anim) o->anim->count=0;
  }
  // Input phase
  m=VOIDLINK;
  v=NVALUE(0);
  if(!quiz_obj.t) {
    n=lastobj;
    while(n!=VOIDLINK) {
      i=classes[objects[n]->class]->cflags;
      if(i&CF_INPUT) v=send_message(VOIDLINK,n,MSG_KEY,NVALUE(key),v,NVALUE(0));
      if(i&CF_PLAYER) m=n;
      n=objects[n]->prev;
    }
  } else {
    n=quiz_obj.u;
    if(objects[n]->generation!=quiz_obj.t) n=VOIDLINK;
    quiz_obj=NVALUE(0);
    if(classes[objects[n]->class]->cflags&CF_COMPATIBLE) all_flushed=1;
    v=send_message(VOIDLINK,n,MSG_KEY,NVALUE(key),NVALUE(0),NVALUE(1));
  }
  current_key=0;
  if(key_ignored) {
    quiz_obj=NVALUE(0);
    return changed?"Invalid use of IgnoreKey":0;
  }
  move_number++;
  // Beginning phase
  if(!all_flushed) broadcast(m,0,MSG_BEGIN_TURN,m==VOIDLINK?NVALUE(objects[m]->x):NVALUE(0),m==VOIDLINK?NVALUE(objects[m]->y):NVALUE(0),v,0);
  turn=0;
  // Trigger phase
  trig:
  busy=0;
  clock=255;
  for(i=0;i<64*pfheight;i++) {
    n=playfield[i];
    while(n!=VOIDLINK) {
      o=objects[n];
      m=o->up;
      if(o->oflags&OF_DESTROYED) {
        objtrash(n);
      } else if(classes[o->class]->cflags&CF_COMPATIBLE) {
        if(o->oflags&OF_MOVED) {
          o->oflags&=~OF_MOVED;
          send_message(VOIDLINK,n,MSG_MOVED,NVALUE(0),NVALUE(0),NVALUE(turn));
          busy=1;
        }
        if(o->departed) {
          send_message(VOIDLINK,n,MSG_DEPARTED,NVALUE(0),NVALUE(0),NVALUE(turn));
          o->departed=0;
          busy=1;
        }
        if(o->arrived) {
          send_message(VOIDLINK,n,MSG_ARRIVED,NVALUE(0),NVALUE(0),NVALUE(turn));
          o->arrived=0;
          busy=1;
        }
        if(o->anim && (o->anim->status&ANISTAT_LOGICAL)) clock=execute_animation(clock,n);
        if(o->oflags&(OF_BUSY|OF_USERSIGNAL)) busy=1;
      } else {
        o->departed2=o->departed;
        o->departed=0;
        o->arrived2=o->arrived;
        o->arrived=0;
        if(o->oflags&OF_MOVED) o->oflags=(o->oflags|OF_MOVED2)&~OF_MOVED;
      }
      n=m;
    }
  }
  n=lastobj;
  while(n!=VOIDLINK) {
    o=objects[n];
    if(!(classes[o->class]->cflags&CF_COMPATIBLE)) {
      if(o->oflags&OF_MOVED2) send_message(VOIDLINK,n,MSG_MOVED,NVALUE(0),NVALUE(0),NVALUE(turn)),busy=1;
      if(o->departed2) send_message(VOIDLINK,n,MSG_DEPARTED,NVALUE(o->departed2),NVALUE(0),NVALUE(turn)),busy=1;
      if(o->arrived2) send_message(VOIDLINK,n,MSG_ARRIVED,NVALUE(o->arrived2),NVALUE(0),NVALUE(turn)),busy=1;
      o->oflags&=~OF_MOVED2;
      o->arrived2=o->departed2=0;
      if(o->anim && (o->anim->status&ANISTAT_LOGICAL)) clock=execute_animation(clock,n);
      if(o->oflags&(OF_BUSY|OF_USERSIGNAL)) busy=1;
    }
    n=o->prev;
  }
  // Ending phase
  if(!busy && !all_flushed) {
    n=lastobj;
    while(n!=VOIDLINK) {
      v=send_message(VOIDLINK,n,MSG_END_TURN,NVALUE(turn),NVALUE(0),NVALUE(0));
      if(v_bool(v) || objects[n]->arrived || objects[n]->departed) busy=1;
      if(objects[n]->oflags&(OF_BUSY|OF_USERSIGNAL|OF_MOVED)) busy=1;
      n=objects[n]->prev;
    }
    turn++;
    if(!busy) all_flushed=1;
  }
  // Clock phase
  if(!clock) clock=1;
  n=lastobj;
  while(n!=VOIDLINK) {
    o=objects[n];
    if(o->oflags&(OF_BUSY|OF_USERSIGNAL|OF_MOVED)) busy=1;
    if(o->arrived || o->departed) busy=1;
    if(o->anim && (o->anim->status&ANISTAT_LOGICAL)) {
      if(o->anim->step[o->anim->lstep].flag&ANI_ONCE) {
        i=o->anim->ltime+clock;
        o->anim->ltime=i>255?255:i;
        busy=1;
      }
    }
    n=o->prev;
  }
  if(busy) goto trig;
  // Cleanup phase
  for(n=0;n<nobjects;n++) if(objects[n] && (objects[n]->oflags&OF_DESTROYED)) objtrash(n);
  if(generation_number<=TY_MAXTYPE) return "Too many generations of objects";
  // Finished
  return 0;
}

const char*init_level(void) {
  if(setjmp(my_env)) return my_error;
  if(main_options['t']) printf("[Level %d restarted]\n",level_id);
  memcpy(globals,initglobals,sizeof(globals));