1 /* ************************************************************************* *
2    bugsx - (C) Copyright 1990-1997 Joshua R. Smith (jrs@media.mit.edu)
3 	   http://physics.www.media.mit.edu/~jrs
4 
5 	   (C) Copyright 1995-1997  Robert Gasch (Robert_Gasch@peoplesoft.com)
6 	   http://www.peoplesoft.com/peoplepages/g/robert_gasch/index.htm
7 
8    Permission to use, copy, modify and distribute this software for any
9    purpose and without fee is hereby granted, provided that this copyright
10    notice appear in all copies as well as supporting documentation. All
11    work developed as a consequence of the use of this program should duly
12    acknowledge such use.
13 
14    No representations are made about the suitability of this software for
15    any purpose. This software is provided "as is" without express or implied
16    warranty.
17 
18    See the GNU General Public Licence for more information.
19  * ************************************************************************* */
20 
21 
22 
23 #include "bugsx.h"
24 
25 
26 extern Display			*mydisplay;
27 extern WinType			main_win,
28 				menu[],
29 				draw_win[];
30 extern char*			myname;
31 extern XrmDatabase		resourceDB;
32 extern unsigned long		fg, bg;
33 extern XSizeHints		szhint;
34 extern int 			G_size_pop,
35 				G_show_genes,
36 				verbose,
37 				do_print_pop,
38 				extend_print,
39 				Draw_Wins,
40 				Draw_Rows,
41 				Draw_Columns,
42 				segments,
43 				batch,
44 				batch_breed,
45 				cycle,
46 				show_breed,
47 				upd_interval,
48 				selected[],
49 				menu_border;
50 extern long			seed;
51 
52 
53 
54 /* ***************************************************************** */
55 /* **************** select the breeding subpopulation ************** */
56 /* ***************************************************************** */
select_breeding_sub_pop()57 void select_breeding_sub_pop ()
58 {
59 	int	i, count=0;
60 
61 	if (!batch)
62 		{
63 		printf ("Sanity check error! select_breeding_sub_pop() called \
64 without -batch ... Exiting\n");
65 		exit (1);
66 		}
67 	for (i=0; i<Draw_Wins; i++)
68 		if (rnd (0,1))
69 			{
70 			if (show_breed)
71 				highlight_org_window(i);
72 			count++;
73 			}
74 
75 	while (count < 2)
76 		{
77 		i=rnd (0, Draw_Wins-1);
78 		if (!selected[i])
79 			{
80 			highlight_org_window(i);
81 			count++;
82 			}
83 		}
84 }
85 
86 
87 
88 /* ***************************************************************** */
89 /* ****************** Highlight an organism window ***************** */
90 /* ***************************************************************** */
highlight_org_window(i)91 void highlight_org_window (i)
92 int 	i;
93 {
94 	if (selected[i])
95 		{
96 		XFillRectangle (mydisplay,
97 			draw_win[i].win, draw_win[i].gc,
98 			0,0, draw_win[i].width,
99 			draw_win[i].height);
100 		XSetForeground (mydisplay, draw_win[i].gc, fg);
101 		selected[i]=FALSE;
102 		}
103 	else
104 		{
105 		XFillRectangle (mydisplay,
106 			draw_win[i].win, draw_win[i].gc,
107 			0,0, draw_win[i].width,
108 			draw_win[i].height);
109 		XSetForeground (mydisplay, draw_win[i].gc, bg);
110 		selected[i]=TRUE;
111 		}
112 	grow (i);
113 	if (G_show_genes)
114 		display_genes(i);
115 }
116 
117 
118 
119 /* ***************************************************************** */
120 /* ****************** Process the program arguments **************** */
121 /* ***************************************************************** */
handle_resize(x,y)122 void handle_resize (x, y)
123 int 	x, y;
124 {
125 	int 	i;
126 	main_win.width=x;
127 	main_win.height=y;
128 	for (i=0; i<MENU_ITEMS; i++)
129 		{
130 		menu[i].x=((main_win.width - menu[i].width) / 2);
131 		menu[i].y=MENU_Y + MENU_HEIGHT(menu[i]) * i;
132 		XResizeWindow (mydisplay, menu[i].win, menu[i].width,
133 			menu[i].height);
134 		XMoveWindow (mydisplay, menu[i].win, menu[i].x,
135 			menu[i].y);
136 		}
137 	for (i=0; i<Draw_Wins; i++)
138 		{
139 		draw_win[i].width = DRAW_WIDTH;
140 		draw_win[i].height = DRAW_HEIGHT;
141 		draw_win[i].x = (i%Draw_Columns)*DRAW_WIDTH+MAIN_BORDER;
142                 draw_win[i].y = (i/Draw_Rows)*DRAW_HEIGHT+MENU_SPACE(main_win)+
143                                 MAIN_BORDER;
144 		XResizeWindow (mydisplay, draw_win[i].win, draw_win[i].width,
145 			draw_win[i].height);
146 		XMoveWindow (mydisplay, draw_win[i].win, draw_win[i].x,
147 			draw_win[i].y);
148 		}
149 	grow_pop ();
150 }
151 
152 
153 /* ***************************************************************** */
154 /* ****************** Process the program arguments **************** */
155 /* ***************************************************************** */
process_databases(argc,argv,commandlineDB)156 void process_databases (argc, argv, commandlineDB)
157 int argc;
158 char **argv;
159 XrmDatabase commandlineDB;
160 {
161 	char 	name[255];
162 	char 	msg[255];
163 	int	tsi;
164 	double	tsd;
165 
166 	strcpy (msg, "");
167 	/* *** Load the local app-defaults file. *** */
168 	if (getenv ("XAPPLRESDIR"))
169 		{
170 		strcpy(name, getenv ("XAPPLRESDIR"));
171 		strcat(name, "/");
172 		strcat(name, CLASS_NAME);
173 		resourceDB = XrmGetFileDatabase(name);
174 		if (resourceDB)
175 			sprintf (msg,
176 				"Read Resource definitions in %s ...", name);
177 		}
178 
179 
180 	/* *** check if we really found something *** */
181 	/* *** if not, check in APP_DEFAULTS_DIR *** */
182 	if (!resourceDB)
183 		{
184 		strcpy(name, APP_DEFAULTS_DIR);
185 		strcat(name, "/");
186 		strcat(name, CLASS_NAME);
187 		resourceDB = XrmGetFileDatabase(name);
188 		if (resourceDB)
189 			sprintf(msg, "Read Resource definitions in %s ...",
190 				name);
191 		}
192 
193 	/* *** Load in any definitions supplied in the X server. *** */
194 	if (XResourceManagerString(mydisplay) != NULL)
195 		XrmMergeDatabases(XrmGetStringDatabase(XResourceManagerString
196 			(mydisplay)), &resourceDB);
197 
198 	/* *** Merge in the command line arguments. *** */
199 	XrmMergeDatabases(commandlineDB, &resourceDB);
200 
201 	/* *** See if the help flag was set. *** */
202 	if (getBoolResource(resourceDB, catlist(myname, ".help", (char*)NULL),
203 		"Bugsx.Help", False))
204 			do_help();
205 
206 	/* *** See if the verbose flag was set. *** */
207 	if ((verbose = getBoolResource(resourceDB,
208 		catlist(myname, ".verbose", (char*) NULL),
209 		"Bugsx.Verbose", False)))
210 		{
211 		PRINT_COPYRIGHT;
212 		puts ("-v flag caught - verbose mode enabled...");
213 		if (resourceDB && strlen (msg))
214 			puts (msg);
215 		}
216 
217 	/* *** assign foreground and backgound colors *** */
218  	fg = getColorResource(resourceDB, catlist(myname,
219 		".foreground", (char*) NULL), "Bugsx.Foreground",
220 		BlackPixel (mydisplay, DefaultScreen (mydisplay)));
221 	bg = getColorResource(resourceDB, catlist(myname,
222 		".background", (char*) NULL), "Bugsx.Background",
223 		WhitePixel (mydisplay, DefaultScreen (mydisplay)));
224 	if (bg != WhitePixel (mydisplay, DefaultScreen (mydisplay)))
225 		if (verbose)
226 			printf ("-bg flag caught - background set\n");
227 
228 	if ((menu_border = getBoolResource(resourceDB,
229 		catlist(myname, ".menuborder", (char*)NULL),
230 		"Bugsx.Menuborder", False)))
231 		if (verbose)
232 			puts("-mb flag caught - will draw menu border.");
233 
234 	if ((do_print_pop = getBoolResource(resourceDB,
235 		catlist(myname, ".printpop", (char*)NULL),
236 		"Bugsx.Printpop", False)))
237 		if (verbose)
238 			puts("-printpop flag caught - will print population.");
239 
240 	if ((extend_print = getBoolResource(resourceDB,
241 		catlist(myname, ".extend_print", (char*)NULL),
242 		"Bugsx.ExtendPrint", False)))
243 		if (verbose)
244 			puts("-extend_print flag caught - will print \
245 breeding output");
246 
247 	if ((batch = getBoolResource(resourceDB,
248 		catlist(myname, ".batch", (char*)NULL),
249 		"Bugsx.Batch", False)))
250 		{
251 		randomize_pop();
252 		if (verbose)
253 			puts("-batch flag caught - will run programm in batch \
254 mode");
255 		}
256 
257 	if (!(batch_breed = getBoolResource(resourceDB,
258 		catlist(myname, ".batchbreed", (char*)NULL),
259 		"Bugsx.Batchbread", True)))
260 		{
261 		if (!batch)
262 			{
263 			puts ("-nobreed does not make sense without -batch ...\
264  Exiting");
265 			exit (1);
266 			}
267 		if (verbose)
268 			puts("-nobreed flag caught - will not breed in batch \
269 mode");
270 		}
271 
272 	if ((cycle = getIntResource(resourceDB,
273 		catlist(myname, ".cycle", (char*)NULL),
274 		"Bugsx.Cycle", 100)) != 100)
275 		{
276 		if (!batch)
277 			{
278 			puts ("-cycle does not make sense without -batch ...\
279  Exiting");
280 			exit (1);
281 			}
282 		if (verbose)
283 			printf ("-cycle flag caught - will re-initialize population \
284 after %d turns ...\n", cycle);
285 		}
286 
287 
288 	if ((segments = getIntResource(resourceDB,
289 		catlist(myname, ".segments", (char*)NULL),
290 		"Bugsx.Segments", 150)) != ORG_SEGMENTS)
291 		{
292 		if (segments < 5)
293 			{
294 			printf ("Invalid -segments flag caught (%d) - must be \
295 between >= 5 Exiting ...\n", segments);
296 			exit (1);
297 			}
298 		if (verbose)
299 			printf ("-segments flag caught - will use %d \
300 segments to draw org\n", segments);
301 		}
302 
303 	if ((upd_interval = getIntResource(resourceDB,
304 		catlist(myname, ".interval", (char*)NULL),
305 		"Bugsx.Interval", UPDINTERVAL)) != UPDINTERVAL)
306 		{
307 		if (!batch)
308 			{
309 			puts ("-interval does not make sense without -batch ...\
310  Exiting");
311 			exit (1);
312 			}
313 		if (upd_interval < 0)
314 			{
315 			printf ("Invalid -interval flag caught (%d) - \
316 must be >= 1 ... Exiting\n", upd_interval);
317 			exit (1);
318 			}
319 		if (verbose)
320 		    printf
321 		   	 ("-interval flag caught - setting interval to %d\n",
322 			upd_interval);
323 		}
324 
325 	if ((Draw_Wins = getIntResource(resourceDB,
326 		catlist(myname, ".number", (char*)NULL),
327 		"Bugsx.Number", 16)) != 16)
328 		{
329 		if (Draw_Wins < 1)
330 			{
331 			printf ("Invalid -number flag caught (%d) - must be \
332 >= 1\n", Draw_Wins);
333 			exit (1);
334 			}
335 		if (Draw_Wins == 1 && batch && batch_breed)
336 			{
337 			puts ("'-number 1' doesn't make sense if '-batch' is \
338 specified without -nobreed ... \nExiting");
339 			exit (1);
340 			}
341 		tsi=(int)sqrt(Draw_Wins);
342 		tsd=sqrt(Draw_Wins);
343 		if ((double)tsi == tsd)
344 		    {
345 		    if (Draw_Wins > 100)
346 			{
347 			printf ("Invalid -number flag caught (%d) - must be \
348 smaller than 100\n", Draw_Wins);
349 			exit (1);
350 			}
351 		    G_size_pop=Draw_Wins;
352 		    Draw_Columns=Draw_Rows=sqrt((double)Draw_Wins);
353 		    if (verbose)
354 			printf ("-number flag caught-will draw %d organisms\n",
355 				Draw_Wins);
356 		    }
357 		else
358 			{
359 			printf ("Invalid -num flag caught (%d) - must be a \
360 square number .... Exiting\n", Draw_Wins);
361 			exit (1);
362 			}
363 		}
364 
365 	if ((show_breed = getBoolResource(resourceDB,
366 		catlist(myname, ".showbreed", (char*)NULL),
367 		"Bugsx.Showbreed", False)))
368 		{
369 		if (!batch)
370 			{
371 			puts ("-showbreed does not make sense without -batch \
372 ... Exiting");
373 			exit (1);
374 			}
375 		if (!batch_breed)
376 			{
377 			puts ("-showbreed does not make sense with -nobreed \
378 ... Exiting");
379 			exit (1);
380 			}
381 		if (Draw_Wins == 1)
382 			{
383 			puts ("-showbreed does not make sense with '-number 0' \
384 ... Exiting");
385 			exit (1);
386 			}
387 		if (verbose)
388 			puts("-showbreed flag caught - will show breeding \
389 selection in batch mode");
390 		}
391 
392 	if ((G_show_genes = getBoolResource(resourceDB,
393 		catlist(myname, ".showgenes", (char*)NULL),
394 		"Bugsx.Showgenes", False)))
395 		if (verbose)
396 		    printf
397 		   	 ("-showgenes flag caught - setting showgenes=TRUE\n");
398 
399 }
400 
401 
do_help()402 void do_help ()
403 {
404 	printf ("\n\
405                       BBBB   U   U  GGGGG  SSSSS  X   X\n\
406                       B   B  U   U  G      S       X X\n\
407                       BBBB   U   U  G GGG  SSSSS    X\n\
408                       B   B  U   U  G   G      S   X X\n\
409                       BBBB    UUU   GGGGG  SSSSS  X   X\n\
410 \n\
411 bugsx runs under MIT's X11 window system. It was written under\n\
412 UNIX but should be easily portable.  It is a program which draws\n\
413 the biomorphs based on parametric plots of Fourier sine and cosine\n\
414 series and let's you play with them using the genetic algorithm.\n");
415 
416 printf ("\n\
417         +rv                     reverse video (use to override xrdb entry)\n\
418         +synchronous            syncronous mode (use to override xrdb entry)\n\
419         -?                      help\n\
420         -background <arg>       backgound color\n\
421         -batch                  run program in batch mode\n\
422         -bg <arg>               same as -background\n\
423         -bordercolor <arg>      border color\n\
424         -borderwidth <arg>      border width\n\
425 	-cycle <arg>		re-initialize population after n batch turns\n\
426         -display                display\n\
427         -extend_print           show extended reproduction info while running\n\
428         -fg <arg>               same as -forground\n\
429         -font <arg>             font\n\
430         -foreground <arg>       forground color (also file system bar color)\n\
431         -geometry <arg>         geometry\n\
432         -help                   help\n\
433         -iconic                 iconic\n\
434         -interval <arg>         interval used per turn\n\
435         -mb                     show menu border\n\
436         -name <arg>             run bugsx under this name\n\
437         -nobreed                do not breed when running in batch mode\n\
438         -number <arg>           number of biomorphs to draw (must be a square #)\n\
439         -printpop               print the population when breeding\n\
440         -rv                     reverse video\n\
441         -seed <arg>             use this seed for random number generator\n\
442         -segments <arg>         use this many segments to draw an organism\n\
443         -showbreed              show breeding subpopulation when in batch mode\n\
444 	-showgenes		show a graphic representation for the genes\n\
445         -synchronous            synchronous mode\n\
446         -v                      verbose\n\
447         -xrm                    make no entry in resrouce database\n\
448         help                    help\n\n");
449 
450 
451 	PRINT_COPYRIGHT;
452 	exit (0);
453 }
454 
455