1 /*
2  * XLife Copyright 1989 Jon Bennett jb7m+@andrew.cmu.edu, jcrb@cs.cmu.edu
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software without
10  * specific, written prior permission.  The copyright holders make no
11  * representations about the suitability of this software for any purpose.  It
12  * is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20  * PERFORMANCE OF THIS SOFTWARE.
21  */
22 
23 /*
24 A lot of modifications were added at 2001, 2011-13 by Vladimir Lidovski vol.litwr@gmail.com
25 (C) This version of XLife may be used under the same conditions as mentioned above
26 $Id: help.c 239 2013-07-31 17:35:09Z micro $
27 */
28 
29 #include "defs.h"
30 #include "file.h"
31 #include "history.h"
32 #include "tile.h"
33 
34 static char *strs[] = {
35 #include "help.h"
36 0
37 };
38 
redraw_help(void)39 void redraw_help(void) {
40    int i;
41    XClearWindow(disp, helpw);
42    for (i = 0; strs[i]; i++)
43       XDrawString(disp, helpw, itextgc, 10, i*(FONTHEIGHT - 1) + 16, strs[i],
44                                                               strlen(strs[i]));
45 }
46 
help(void)47 void help(void) {
48    helpw_mode = -1;
49 LOOP:
50    XRaiseWindow(disp, helpw);
51    redraw_help();
52    wait_activity(0);
53    XLowerWindow(disp, helpw);
54    if (event.type == KeyPress) {
55       XLookupString(&event.xkey, keybuf, 16, &ks, 0);
56       if (ks == XK_Shift_L || ks == XK_Shift_R || *keybuf == '?')
57          goto LOOP;
58       helpw_mode = 0;
59       if (!DoKeySymIn(ks))
60          if (*keybuf && strchr(
61             "Cl!%^#bMZr*+=-hpc$/Egfms<>ojPWDNAVKGUIduLYXnRTFta@vSBiQ12346789H",
62                                                                       *keybuf))
63             DoKeyIn(keybuf); /* k.Ox()[]{}J05 -- excluded */
64    }
65    showstates();
66 }
67 
yesno(int d)68 static char* yesno(int d) {
69    static char yes[] = "yes", no[] = "no";
70    if (d)
71       return yes;
72    else
73       return no;
74 }
75 
pseudocolor_msg(void)76 void pseudocolor_msg(void) {
77    switch (pseudocolor) {
78       case 0:
79          strcat(inpbuf, "no");
80          break;
81       case 1:
82          strcat(inpbuf, "Mark out new cells");
83          break;
84       case 2:
85          strcat(inpbuf, "Mark out new and removed cells");
86          break;
87       case 3:
88          strcat(inpbuf, "Mark out removed cells");
89          break;
90       case 4:
91          strcat(inpbuf, "Show only changed cells");
92    }
93 }
94 
redraw_viewvars(void)95 void redraw_viewvars(void) {
96    double ul_max = (1UL << 31)*2.;
97    int k = 0, i;
98    XClearWindow(disp, helpw);
99    sprintf(inpbuf, "Window size: %dx%d", width, height);
100    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
101                                                                strlen(inpbuf));
102    if (stashed[0]) {
103       sprintf(inpbuf, "Last loaded filename: %s", stashed);
104       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
105                                                                strlen(inpbuf));
106       sprintf(inpbuf, "Number of comment lines: %d", numcomments);
107       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
108                                                                strlen(inpbuf));
109       if (outcome[0]) {
110          sprintf(inpbuf, "#K line: %s", outcome);
111          XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
112                                                                strlen(inpbuf));
113       }
114       sprintf(inpbuf, "Quantity of scripts to save: %d", i = loadscriptstat());
115       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
116                                                                strlen(inpbuf));
117       if (i--) {
118          sprintf(inpbuf, "%s", loadscriptfn(1));
119          while (i--)
120             sprintf(inpbuf + strlen(inpbuf), " %s", loadscriptfn(0));
121          XDrawString(disp, helpw, itextgc, 25, ++k*FONTHEIGHT, inpbuf,
122                                                                strlen(inpbuf));
123       }
124    }
125    if (colorfile[0]) {
126       sprintf(inpbuf, "Palette filename: %s", colorfile);
127       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
128                                                                strlen(inpbuf));
129    }
130    if (fname[0]) {
131       sprintf(inpbuf, "Internal filename: %s", fname);
132       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
133                                                                strlen(inpbuf));
134    }
135    sprintf(inpbuf, "Topology: %c ", topology);
136    if (limits) {
137       if (limits&1)
138          sprintf(inpbuf + strlen(inpbuf), "%ux", x_max_limit - x_min_limit);
139       else
140          sprintf(inpbuf + strlen(inpbuf), "%.0fx", ul_max);
141       if (limits&2)
142          sprintf(inpbuf + strlen(inpbuf), "%u", y_max_limit - y_min_limit);
143       else
144          sprintf(inpbuf + strlen(inpbuf), "%.0f", ul_max);
145    }
146    else
147       sprintf(inpbuf + 12, "%.0fx%.0f", ul_max, ul_max);
148    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
149                                                                strlen(inpbuf));
150    sprintf(inpbuf, "Origin: (%u, %u)", xorigin, yorigin);
151    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
152                                                                strlen(inpbuf));
153    if (bounding_box(&active)) {
154       sprintf(inpbuf, "Active pattern size: %dx%d",
155                  active.xmax - active.xmin + 1, active.ymax - active.ymin + 1);
156       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
157                                                                strlen(inpbuf));
158    }
159    if (*pathtorules) {
160       sprintf(inpbuf, "Rules filename: %s", pathtorules);
161       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
162                                                                strlen(inpbuf));
163    }
164    sprintf(inpbuf, "Delay for the evolution (msec): %u", delay);
165    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
166                                                                strlen(inpbuf));
167    sprintf(inpbuf, "Jump length for `go' command: %d", hideperiod);
168    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
169                                                                strlen(inpbuf));
170    sprintf(inpbuf, "Wireframe tentative pattern mode: %s", yesno(wireframe));
171    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
172                                                                strlen(inpbuf));
173    sprintf(inpbuf, "Oscillator check mode: %s", yesno(oscillators));
174    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
175                                                                strlen(inpbuf));
176    if (ev_mode == VALENCE_DRIVEN) {
177       strcpy(inpbuf, "Pseudocolor mode: ");
178       pseudocolor_msg();
179       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
180                                                                strlen(inpbuf));
181    }
182    sprintf(inpbuf, "History record mode: %s", yesno(truehistory));
183    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
184                                                                strlen(inpbuf));
185    sprintf(inpbuf, "Density for the random filled area: %.2f%%",
186                                                               rnd_density*100);
187    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
188                                                                strlen(inpbuf));
189    if (randomseed) {
190       sprintf(inpbuf, "Random generator seed: %d", randomseed);
191       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
192                                                                strlen(inpbuf));
193    }
194    hashstat(&active);
195    XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
196                                                                strlen(inpbuf));
197    if (tentative.tiles) {
198       hashstat(&tentative);
199       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
200                                                                strlen(inpbuf));
201    }
202    if (truehistory) {
203       historyhashstat(0);
204       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
205                                                                strlen(inpbuf));
206       historyhashstat(1);
207       XDrawString(disp, helpw, itextgc, 10, ++k*FONTHEIGHT, inpbuf,
208                                                                strlen(inpbuf));
209    }
210    XDrawString(disp, helpw, itextgc, 5, (k + 2)*FONTHEIGHT,
211                                                 "Press a key to continue", 23);
212 }
213 
viewvars(void)214 void viewvars(void) {
215    helpw_mode = -2;
216    XRaiseWindow(disp, helpw);
217    redraw_viewvars();
218    wait_activity(0);
219    XLowerWindow(disp, helpw);
220    helpw_mode = 0;
221    displaystats();
222 }
223 
redraw_comments(void)224 int redraw_comments(void) {
225    int i = 0;
226    XClearWindow(disp, helpw);
227    while (height > (i + 3)*FONTHEIGHT && helpw_mode + i < numcomments) {
228       XDrawString(disp, helpw, itextgc, 10, (i + 1)*FONTHEIGHT,
229                    comments[helpw_mode + i], strlen(comments[helpw_mode + i]));
230       i++;
231    }
232    XDrawString(disp, helpw, itextgc, 5, (i + 2)*FONTHEIGHT,
233                                                 "Press a key to continue", 23);
234    return i;
235 }
236 
view_comments(void)237 void view_comments(void) {
238    if (numcomments) {
239       XRaiseWindow(disp, helpw);
240       do {
241          eo_comments = redraw_comments();
242          wait_activity(0);
243          helpw_mode += eo_comments;
244       } while (helpw_mode < numcomments);
245       XLowerWindow(disp, helpw);
246    }
247    eo_comments = helpw_mode = 0;
248 }
249 
redraw_slashinfo(void)250 void redraw_slashinfo(void) {
251    int i, q = 0, h, l;
252    char s[80];
253    long double t = 0, z = (1UL << 31)*2.;
254    XClearWindow(disp, helpw);
255    h = width/14/FONTWIDTH;
256    if ((l = bounding_box(&active)) == 0)
257       XDrawString(disp, helpw, itextgc, 10, FONTHEIGHT, "Life is extinct", 15);
258    else {
259       for (i = 1; i < maxstates; i++)
260          if (active.cellcount[i]) {
261             t += active.cellcount[i];
262             sprintf(s, "[%d] %d", i, active.cellcount[i]);
263             XDrawString(disp, helpw, itextgc, 10 + (q%h)*14*FONTWIDTH,
264                                            (q/h + 1)*FONTHEIGHT, s, strlen(s));
265             q++;
266          }
267       if (maxstates > 2) {
268          sprintf(s, "[*] %.0Lf", t);
269          XDrawString(disp, helpw, itextgc, 10 + (q%h)*14*FONTWIDTH,
270                                            (q/h + 1)*FONTHEIGHT, s, strlen(s));
271          q++;
272       }
273       if (limits) {
274          if (limits&1)
275             z = x_max_limit - x_min_limit;
276          if (limits&2)
277             z *= y_max_limit - y_min_limit;
278          else
279             z *= (1UL << 31)*2.;
280       }
281       else
282          z *= z;
283       sprintf(s, "[0] %.0Lf", z - t);
284       XDrawString(disp, helpw, itextgc, 10, (q/h + 2)*FONTHEIGHT, s,
285                                                                     strlen(s));
286    }
287    XDrawString(disp, helpw, itextgc, 5, (q/h + 5)*FONTHEIGHT,
288                                                 "Press a key to continue", 23);
289 }
290 
view_slashinfo(void)291 void view_slashinfo(void) {
292    helpw_mode = -3;
293    XRaiseWindow(disp, helpw);
294    redraw_slashinfo();
295    wait_activity(0);
296    XLowerWindow(disp, helpw);
297    helpw_mode = 0;
298 }
299