Hi,<br><br>I have been working on a roguelike game in scheme. Due to being unable to find an implementation of scheme with support for ncurses, and after a somewhat disappointing (but informative) attempt using chicken scheme, I eventually decided that extending mzscheme was the way forward.
<br><br>The extension has gone fairly well and provides most of the functionality I desire (including much of the number crunching for line of sight). However the following problems are unresolved.<br><br>I am using mzscheme v370, and emacs.
<br><br>1. the scheme_malloc_* family of functions segfault if the requested memory is unavailable.<br><br>2. Is it possible to register a function which is called when a object is garbage collected?<br><br>3. I have found it desirable to create scheme objects which act as a wrapper for the actual game data. It is undesirable to have to use (map-width * map-height * sizeof(Scheme_Object)) bytes when only a few map elements are accessed at any one time. Secondly when handling map elements it is often desirable to know its coordinates, which can be stored in the wrapper.
<br>This would be simple, except that i would prefer for there to never be two objects which are a wrapper for the same map element. Not only is this unintuitive, but it prevents the possibility of testing for equality using the&nbsp; eqv?.
<br>The best idea I have had is to use a hash map to keep a record of currently available wrapper objects and return the appropriate wrapper. However, question 2 would have to be possible in order to keep the table updated. Also there is the issue of how long to keep unreferenced wrappers around (a round robbin system similar to that used in the caching of some operating systems could be profitable).
<br><br><br><br>As the source is around 2000 lines, I will not post it all here. However the functions involving memory allocation follow. Please note that I have not yet started altering the code to use a wrapper for the scheme type map-element.
<br><br>/* A structure representing indevidual tiles (map elements) of a dungeon.<br>&nbsp;&nbsp; Notice the data field. This allows game specific data to be stored on the tile<br>&nbsp;&nbsp; while keeping all the engine relevent data visible to the engine.*/
<br>typedef struct {<br>&nbsp; Scheme_Object so ; <br>&nbsp; unsigned char seen : 1 ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* has this map element ever been seen? */<br>&nbsp; unsigned char visible : 1 ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* can this map element be seen right now? */<br>
&nbsp; unsigned char opaque_to_visible : 1 ; /* is this map element opaque to normal light? */<br>&nbsp; unsigned char opaque_to_uv : 1 ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* is this map element opaque to uv light? */<br>&nbsp; unsigned char opaque_to_ir : 1 ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* is this map element opaque to ir light? */
<br>&nbsp; unsigned char solid : 1 ;<br>&nbsp; unsigned char changed : 1 ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Not really shure about the advisibility of this, however it avoids<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; repeatedly inserting the same coordinates into the changed list. */
<br>&nbsp; unsigned char residual_heat ;<br>&nbsp; unsigned char lit_visible ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* is this map element lit by normal light?*/<br>&nbsp; unsigned char lit_uv ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* is this map element lit by uv light? */<br>&nbsp; char print_char ;
<br>&nbsp; char display_print_char ;<br>&nbsp; Scheme_Object *data ;<br>}&nbsp; Map_element ;<br><br>/* The structure representing the actual dungeon. */<br>typedef struct {<br>&nbsp; Scheme_Object so ; <br>&nbsp; unsigned int w ;<br>&nbsp; unsigned int h ;
<br>&nbsp; unsigned int l ;<br>&nbsp; unsigned char ambient_visible_light ;<br>&nbsp; unsigned char ambient_uv_light ;<br>&nbsp; unsigned char ambient_heat ;<br>&nbsp; Map_element *map;<br>&nbsp; struct changed *changed_list ;<br>} Dungeon ;<br><br><br>
Scheme_Object *make_dungeon(int argc, Scheme_Object **argv) {<br>&nbsp; unsigned long w, h, l, i ;<br>&nbsp; Map_element *map ;<br>&nbsp; Dungeon *dn ;<br><br>&nbsp; if (!SCHEME_INTP(argv[0]) || negative(argv[0]))<br>&nbsp;&nbsp;&nbsp; scheme_wrong_type(&quot;make-dungeon&quot;, &quot;positive integer&quot;, 0, argc, argv);
<br>&nbsp; if (!SCHEME_INTP(argv[1]) || (negative(argv[1])))<br>&nbsp;&nbsp;&nbsp; scheme_wrong_type(&quot;make-dungeon&quot;, &quot;positive integer&quot;, 1, argc, argv);<br><br>&nbsp; w = SCHEME_INT_VAL(argv[0]);<br>&nbsp; h = SCHEME_INT_VAL(argv[1]);
<br>&nbsp; l = w * h ;<br><br>&nbsp; /* Malloc the dungeon */<br>&nbsp; dn = (Dungeon *)scheme_malloc_tagged(sizeof(Dungeon)) ;<br>&nbsp; dn-&gt;so.type = dungeon_type;<br><br>&nbsp; /*map = (Map_element *)scheme_malloc_fail_ok(scheme_malloc, sizeof(Map_element) * l);*/
<br>&nbsp; map = (Map_element *)scheme_malloc_tagged(sizeof(Map_element) * l) ;<br>&nbsp; if (!map) {<br>&nbsp;&nbsp;&nbsp; scheme_raise_exn(MZEXN_FAIL, &quot;make-dungeon: out of memory&quot;) ;<br>&nbsp; }<br><br>&nbsp; dn-&gt;w&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = w ;<br>&nbsp; dn-&gt;h&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = h ;
<br>&nbsp; dn-&gt;l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = l ;<br>&nbsp; dn-&gt;map&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = map ;<br>&nbsp; dn-&gt;changed_list = NULL ;<br><br>&nbsp; /* Init dungeon to safe vals : */<br>&nbsp; for (i = 0 ; i &lt; l ; i++) {<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].so.type = map_element_type ;
<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].seen = 0 ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].visible = 0 ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].changed = 0 ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].residual_heat = 0 ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].lit_visible = 0 ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].lit_uv = 0 ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].opaque_to_visible = 0 ;
<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].opaque_to_uv = 0 ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].opaque_to_ir = 0 ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].print_char = &#39; &#39; ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].display_print_char = &#39; &#39; ;<br>&nbsp;&nbsp;&nbsp; dn-&gt;map[i].data = scheme_false ;
<br>&nbsp; }<br><br>&nbsp; return (Scheme_Object *)dn ;<br>}<br><br><br><br><br>Thanks<br><br><br>Dragal<br>