Free Hero Mesh

Check-in [8117ec3cff]
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 level index editor, including divisions. (Currently, the divisions is not used outside of the level index editor; later, this should be fixed)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 8117ec3cff1871c395b593f1bde6ee08625206ce
User & Date: user on 2022-01-04 02:21:52
Other Links: manifest | tags
Context
2022-01-04
06:02
Change documentation about code page numbers (the effectiveness is the same as before), and treat code page 367 the same as 437 (which is a superset of 367 anyways, so it is OK). check-in: 561f982c9c user: user tags: trunk
02:21
Implement level index editor, including divisions. (Currently, the divisions is not used outside of the level index editor; later, this should be fixed) check-in: 8117ec3cff user: user tags: trunk
2022-01-02
21:04
Implement loading the level divisions table; currently it does not do anything with this table, which will be implemented later. check-in: a731b53535 user: user tags: trunk
Changes

Modified bindings.doc from [d9974709a6] to [09ad0658cd].

128
129
130
131
132
133
134



135
136
137
138
139
140
141
'rs' <number>
  Adjust replay speed by the specified number (negative to make faster, or
  positive to make slower; zero leaves it unchanged).


=== Editor commands ===




'^N'
  Add a new level (after the last one). The new level has the same
  dimensions as the current level, but contains no objects or strings,
  and the title is set to "New Level".

'^S'
  Save level.







>
>
>







128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'rs' <number>
  Adjust replay speed by the specified number (negative to make faster, or
  positive to make slower; zero leaves it unchanged).


=== Editor commands ===

'^I'
  Level index editor.

'^N'
  Add a new level (after the last one). The new level has the same
  dimensions as the current level, but contains no objects or strings,
  and the title is set to "New Level".

'^S'
  Save level.

Modified default.heromeshrc from [f54d91daf2] to [7f0656bbc7].

153
154
155
156
157
158
159

160
161
162
163
164
165
166
?.editKey.up: select 'mR',-1;
?.editKey.down: select 'mR',+1;
?.editKey.C: select 'lc',:level_code where :level_code=cast(:level_code as int);
?.editKey.E: ^s
?.editKey.F: with n(n) as (select 65 union all select n+1 h from n where h<=64*pfheight()+65) select '^a',n from n;
?.editKey.R: select 're',substr(:resize,1,instr(:resize,'x')-1),substr(:resize,instr(:resize,'x')+1) where length(:resize);
?.editKey.W: ^w

?.editKey.ctrl.N: ^N
?.editKey.ctrl.P: ^P
?.editKey.ctrl.X: select 're',pfwidth(),pfheight();
?.editKey.space: ^c
?.editKey.return: ^e
?.editKey.f1: select 'im',:Import_Level;
?.editKey.f2: select 'ex',:Export_Level;







>







153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
?.editKey.up: select 'mR',-1;
?.editKey.down: select 'mR',+1;
?.editKey.C: select 'lc',:level_code where :level_code=cast(:level_code as int);
?.editKey.E: ^s
?.editKey.F: with n(n) as (select 65 union all select n+1 h from n where h<=64*pfheight()+65) select '^a',n from n;
?.editKey.R: select 're',substr(:resize,1,instr(:resize,'x')-1),substr(:resize,instr(:resize,'x')+1) where length(:resize);
?.editKey.W: ^w
?.editKey.ctrl.I: ^I
?.editKey.ctrl.N: ^N
?.editKey.ctrl.P: ^P
?.editKey.ctrl.X: select 're',pfwidth(),pfheight();
?.editKey.space: ^c
?.editKey.return: ^e
?.editKey.f1: select 'im',:Import_Level;
?.editKey.f2: select 'ex',:Export_Level;

Modified edit.c from [cd3b37ede3] to [13e928d083].

1529
1530
1531
1532
1533
1534
1535












































































































































































































































































1536
1537
1538
1539
1540
1541
1542
        goto redraw;
      case SDL_QUIT:
        exit(0);
        return -1;
    }
  }
}













































































































































































































































































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);







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







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
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
        goto redraw;
      case SDL_QUIT:
        exit(0);
        return -1;
    }
  }
}

typedef struct {
  char*title;
  Uint16 id,kind,mark;
} IndexItem;

static void level_index_editor(void) {
  SDL_Rect r;
  SDL_Event ev;
  char had_divisions=0;
  FILE*out;
  char*buf=0;
  char b[32];
  size_t bufsize=0;
  IndexItem*items;
  IndexItem*items2;
  IndexItem citem;
  int nitems;
  int nmark=0;
  sqlite3_stmt*st=0;
  int i,j,x,y;
  int sel=0;
  int scroll=0;
  char rescroll=1;
  const char*q;
  if(sqlite3_prepare_v2(userdb,"SELECT COUNT() FROM `DIVISIONS`;",-1,&st,0) || sqlite3_step(st)!=SQLITE_ROW) {
    screen_message(sqlite3_errmsg(userdb));
    sqlite3_finalize(st);
    return;
  }
  had_divisions=sqlite3_column_int(st,0)?1:0;
  nitems=level_nindex+sqlite3_column_int(st,0);
  sqlite3_finalize(st);
  if(sqlite3_prepare_v2(userdb,"SELECT 0,`FIRST`,0,`HEADING`,_ROWID_ FROM `DIVISIONS` UNION ALL SELECT 1,`ORD`,`ID`,`TITLE`,0 FROM `LEVELS` ORDER BY 2,1,5;",-1,&st,0)) {
    screen_message(sqlite3_errmsg(userdb));
    return;
  }
  items=calloc(nitems,sizeof(IndexItem));
  if(!items) fatal("Allocation failed\n");
  for(i=0;i<nitems;i++) {
    j=sqlite3_step(st);
    if(j!=SQLITE_ROW) {
      if(j!=SQLITE_DONE) screen_message(sqlite3_errmsg(userdb));
      nitems=i;
      break;
    }
    items[i].kind=sqlite3_column_int(st,0);
    items[i].id=sqlite3_column_int(st,2);
    items[i].title=strdup(sqlite3_column_text(st,3)?:(const unsigned char*)"<?>");
    if(!items[i].title) fatal("Allocation failed\n");
  }
  sqlite3_finalize(st);
  // Main editor loop (GUI)
  set_cursor(XC_arrow);
  redraw:
  if(sel<0) sel=0;
  if(sel>nitems) sel=nitems;
  if(rescroll) {
    if(scroll<0) scroll=0;
    if(sel<scroll) scroll=sel;
    if(sel>=scroll+screen->h/8-2) scroll=sel+3-screen->h/8;
    rescroll=0;
  }
  SDL_FillRect(screen,0,0x02);
  r.x=r.y=0;
  r.w=screen->w;
  r.h=16;
  SDL_FillRect(screen,&r,0xF7);
  SDL_LockSurface(screen);
  draw_text(0,0,"<\x18\x19> Cursor  <SHIFT+\x18\x19> Exchange  <INS> Add Division  <DEL> Delete Division  <ESC> Save",0xF7,0xF0);
  draw_text(0,8,"<F1/E> Edit Title  <F2/M> Mark  <F3/P> Paste  <F4/T> Show Title  <F5/U> Unmark All  <F6/F> Find  <F7> Current",0xF7,0xF0);
  for(y=16,i=scroll;y<screen->h-7 && i<=nitems;y+=8,i++) {
    if(i==sel) {
      r.x=16; r.y=y;
      r.w=screen->w-16; r.h=8;
      SDL_FillRect(screen,&r,0xF8);
      draw_text(16,y,"\x10",x=0xF8,nmark?0xFD:0xFE);
    } else {
      draw_text(16,y,"\xB3",x=0x02,0xF8);
    }
    draw_text(6*8,y,"\xB3",x,x^0xFA);
    draw_text(12*8,y,"\xB3",x,x^0xFA);
    if(i==nitems) {
      draw_text(4*8,y,"\x04 END \x04",x,0xFC);
    } else if(items[i].kind) {
      snprintf(b,8,"%5u",items[i].id);
      draw_text(7*8,y,b,x,0xFA);
      draw_text(13*8,y,items[i].title,x,0xFF);
    } else {
      draw_text(7*8,y,"\x07\x07\x07\x07\x07",x,0xF9);
      draw_text(13*8,y,items[i].title,x,0xFE);
    }
    if(i<nitems && items[i].mark) {
      snprintf(b,8,"%3u",items[i].mark);
      draw_text(3*8,y,b,0xF1,0xFB);
    }
  }
  SDL_UnlockSurface(screen);
  r.x=r.w=0; r.y=16; r.h=screen->h-16;
  scrollbar(&scroll,screen->h/8-2,nitems+1,0,&r);
  SDL_Flip(screen);
  while(SDL_WaitEvent(&ev)) {
    if(ev.type!=SDL_VIDEOEXPOSE && scrollbar(&scroll,screen->h/8-2,nitems+1,&ev,&r)) goto redraw;
    switch(ev.type) {
      case SDL_MOUSEMOTION:
        set_cursor(XC_arrow);
        break;
      case SDL_MOUSEBUTTONDOWN:
        if(ev.button.x>=15) sel=(ev.button.y-16)/8+scroll;
        if(ev.button.button==2) goto paste;
        if(ev.button.button==3) goto mark;
        goto redraw;
      case SDL_KEYDOWN:
        switch(ev.key.keysym.sym) {
          case SDLK_UP: case SDLK_k: case SDLK_KP8:
            if(!sel) break;
            sel--;
            if(ev.key.keysym.mod&(KMOD_ALT|KMOD_META)) {
              while(sel>0 && items[sel].kind) sel--;
            } else if((ev.key.keysym.mod&KMOD_SHIFT) && sel+1<nitems) {
              citem=items[sel+1];
              items[sel+1]=items[sel];
              items[sel]=citem;
            }
            rescroll=1; goto redraw;
          case SDLK_DOWN: case SDLK_j: case SDLK_KP2:
            if(sel>=nitems) break;
            sel++;
            if(ev.key.keysym.mod&(KMOD_ALT|KMOD_META)) {
              while(sel<nitems && items[sel].kind) sel++;
            } else if((ev.key.keysym.mod&KMOD_SHIFT) && sel!=nitems) {
              citem=items[sel-1];
              items[sel-1]=items[sel];
              items[sel]=citem;
            }
            rescroll=1; goto redraw;
          case SDLK_PAGEUP: case SDLK_KP9:
            sel-=screen->h/8-2;
            scroll-=screen->h/8-2;
            rescroll=1; goto redraw;
          case SDLK_PAGEDOWN: case SDLK_KP3:
            sel+=screen->h/8-2;
            scroll+=screen->h/8-2;
            rescroll=1; goto redraw;
          case SDLK_HOME: case SDLK_KP7: scroll=sel=0; goto redraw;
          case SDLK_END: case SDLK_KP1: sel=nitems; rescroll=1; goto redraw;
          case SDLK_INSERT: case SDLK_KP0:
            if(nitems>=65535) {
              screen_message("Too many items");
              goto redraw;
            }
            q=screen_prompt("Add division?");
            if(!q || !*q) goto redraw;
            items=realloc(items,(nitems+1)*sizeof(IndexItem));
            if(!items) fatal("Allocation failed\n");
            memmove(items+sel+1,items+sel,(nitems-sel)*sizeof(IndexItem));
            items[sel].kind=items[sel].mark=0;
            items[sel].title=strdup(q);
            if(!items[sel].title) fatal("Allocation failed\n");
            ++nitems;
            rescroll=1; goto redraw;
          case SDLK_DELETE: case SDLK_KP_PERIOD:
            if(sel==nitems || items[sel].kind) break;
            if(items[sel].mark) {
              --nmark;
              for(i=0;i<nitems;i++) if(items[i].mark>items[sel].mark) --items[i].mark;
            }
            free(items[sel].title);
            memmove(items+sel,items+sel+1,(nitems-sel-1)*sizeof(IndexItem));
            --nitems;
            goto redraw;
          case SDLK_F1: case SDLK_e:
            if(sel==nitems || items[sel].kind) break;
            q=screen_prompt("Edit division?");
            if(!q || !*q) goto redraw;
            free(items[sel].title);
            items[sel].title=strdup(q);
            if(!items[sel].title) fatal("Allocation failed");
            goto redraw;
          case SDLK_F2: case SDLK_m: mark:
            if(sel==nitems || items[sel].mark || nmark==999) goto redraw;
            items[sel].mark=++nmark;
            goto redraw;
          case SDLK_F3: case SDLK_p: paste:
            if(!nmark) goto redraw;
            x=sel;
            if(x<nitems) while(x && items[x].mark && items[x-1].mark) --x;
            items2=items;
            items=malloc(nitems*sizeof(IndexItem));
            if(!items) fatal("Allocation failed\n");
            for(i=j=0;i<x;i++) if(!items2[i].mark) items[j++]=items2[i];
            sel=j; j+=nmark;
            for(i=0;i<nitems;i++) {
              if(y=items2[i].mark) items2[i].mark=0,items[sel+y-1]=items2[i];
              else if(i>=x) items[j++]=items2[i];
            }
            free(items2);
            nmark=0;
            rescroll=1; goto redraw;
          case SDLK_F4: case SDLK_t:
            if(sel==nitems) break;
            modal_draw_popup(items[sel].title);
            goto redraw;
          case SDLK_F5: case SDLK_u:
            for(i=0;i<nitems;i++) items[i].mark=0;
            nmark=0;
            goto redraw;
          case SDLK_F6: case SDLK_f:
            q=screen_prompt("Find?");
            if(!q || !*q) goto redraw;
            if(*q>='0' && *q<='9') {
              j=strtol(q,0,10);
              for(i=0;i<nitems;i++) if(items[i].kind && items[i].id==j) sel=i;
            }
            rescroll=1; goto redraw;
          case SDLK_F7:
            for(i=0;i<nitems;i++) if(items[i].kind && items[i].id==level_id) {
              sel=i;
              rescroll=1; goto redraw;
            }
            break;
          case SDLK_ESCAPE:
            if(ev.key.keysym.mod&KMOD_SHIFT) goto final;
            goto save;
        }
        break;
      case SDL_VIDEOEXPOSE: goto redraw;
      case SDL_QUIT: SDL_PushEvent(&ev); goto save;
    }
  }
  save:
  // Saving the changes, including SQL table and DIVISION.IDX lump
  sqlite3_exec(userdb,"DELETE FROM `DIVISIONS`;",0,0,0);
  if(sqlite3_prepare_v2(userdb,"INSERT INTO `DIVISIONS`(`HEADING`,`FIRST`) VALUES(?1,?2);",-1,&st,0)) {
    screen_message(sqlite3_errmsg(userdb));
    goto final;
  }
  out=open_memstream(&buf,&bufsize);
  if(!out) fatal("Allocation failed");
  for(i=j=0;i<nitems;i++) {
    if(items[i].kind) {
      if(j<level_nindex) level_index[j++]=items[i].id;
    } else {
      sqlite3_bind_text(st,1,items[i].title,-1,0);
      sqlite3_bind_int(st,2,j+1);
      while(sqlite3_step(st)==SQLITE_ROW);
      sqlite3_reset(st);
      fputc((j+1)&255,out); fputc((j+1)>>8,out);
      fwrite(items[i].title,1,strlen(items[i].title)+1,out);
    }
  }
  sqlite3_finalize(st);
  if(!ftell(out)) {
    if(!had_divisions) goto nodiv;
    fputc(0,out);
  }
  fclose(out);
  if(!buf) fatal("Allocation failed");
  write_lump(FIL_LEVEL,LUMP_DIVISION_IDX,bufsize,buf);
  nodiv:
  free(buf);
  // LEVEL.IDX lump
  update_level_index();
  // Done
  final:
  for(i=0;i<nitems;i++) free(items[i].title);
  free(items);
}

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);
1554
1555
1556
1557
1558
1559
1560







1561
1562
1563
1564
1565
1566
1567
      if(prev) return prev;
      add_object_at(number&63?:64,number/64?:64,mru+curmru,0);
      return 0;
    case '^w': // Swap world
      swap_world();
      level_changed=1;
      return 0;







    case '^N': // New level
      if(level_nindex>0xFFFE) return 0;
      new_level();
      return 1;
    case '^P': // Play
      discard_solution();
      return -2;







>
>
>
>
>
>
>







1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
      if(prev) return prev;
      add_object_at(number&63?:64,number/64?:64,mru+curmru,0);
      return 0;
    case '^w': // Swap world
      swap_world();
      level_changed=1;
      return 0;
    case '^I': // Level index editor
      sqlite3_exec(userdb,"BEGIN;",0,0,0);
      level_index_editor();
      sqlite3_exec(userdb,"DELETE FROM LEVELS; DROP TABLE TEMP.LEVELS;",0,0,0);
      sqlite3_exec(userdb,"COMMIT;",0,0,0);
      for(x=0;x<level_nindex;x++) if(level_id==level_index[x]) level_ord=x+1;
      return 0;
    case '^N': // New level
      if(level_nindex>0xFFFE) return 0;
      new_level();
      return 1;
    case '^P': // Play
      discard_solution();
      return -2;

Modified edit.doc from [df66040602] to [a922353611].

187
188
189
190
191
192
193























194
195
196
197
198
199

200
201
202
203
204
205
206
Push F2 to export, and then at the prompt, type a operating system shell
command which will accept the level text as input (in X window system,
type "xclip" to export to the primary selection; you will need to install
xclip to use this).

See export.doc for the file format.

























=== Summary of default key/mouse bindings ===

Keyboard:

  CTRL+G    Go to level (won't auto save)

  CTRL+N    Add new level (after last one)
  CTRL+P    Switch to play game (won't auto save)
  CTRL+Q    Quit (won't auto save)
  CTRL+S    Save level
  CTRL+T    Edit level title
  CTRL+X    Clear level (delete all objects)
  C         Set level code number







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






>







187
188
189
190
191
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
Push F2 to export, and then at the prompt, type a operating system shell
command which will accept the level text as input (in X window system,
type "xclip" to export to the primary selection; you will need to install
xclip to use this).

See export.doc for the file format.


=== Level index editor ===

You can push CTRL+I to activate the level index editor, which can be used
for reordering levels and for inserting divisions into the list of levels.

The display shows the level ID number followed by the title of that level;
if it is a division then there is no ID number. There may also be another
number to the left of the ID number which is the mark number, if there is
any mark (see below for information).

You can move cursor by arrows, and you can then exchange an item with the
one above or below by shift and arrows. If you push alt and arrows, will
move the cursor to the next division in that direction.

Push insert to add a division (just above the cursor), or delete to delete
a division (only divisions can be deleted in this way, not levels).

Another way to reorder the items is to add marks by pushing F2 (or click
by right mouse button) on each one, and tghen push F3 (or click by middle
mouse button) at the point to be moved to; they are removed from where
they were before and put here in the order in which they were marked.


=== Summary of default key/mouse bindings ===

Keyboard:

  CTRL+G    Go to level (won't auto save)
  CTRL+I    Level index editor
  CTRL+N    Add new level (after last one)
  CTRL+P    Switch to play game (won't auto save)
  CTRL+Q    Quit (won't auto save)
  CTRL+S    Save level
  CTRL+T    Edit level title
  CTRL+X    Clear level (delete all objects)
  C         Set level code number