Noble Ape
The Central Directories of the Noble Ape Simulation.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
console.c
Go to the documentation of this file.
1 /****************************************************************
2 
3  console.c
4 
5  =============================================================
6 
7  Copyright 1996-2014 Tom Barbalet. All rights reserved.
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or
14  sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  OTHER DEALINGS IN THE SOFTWARE.
29 
30  This software and Noble Ape are a continuing work of Tom Barbalet,
31  begun on 13 June 1996. No apes or cats were harmed in the writing
32  of this software.
33 
34  ****************************************************************/
35 
36 #ifndef _WIN32
37 #include "../entity/entity.h"
38 #else
39 #include "..\entity\entity.h"
40 #endif
41 
42 #include "universe_internal.h"
43 
44 #include <stdio.h>
45 
46 static n_int simulation_running = 1;
47 static n_int simulation_executing = 0;
48 
49 const n_string RUN_STEP_CONST = "RSC";
50 
53 
56 
59 
62 
65 
68 
70 {
71  return simulation_executing;
72 }
73 
75 {
77  {
78  n_string_block output;
79  n_int position = 0;
80  io_string_write(output, "External Action -> Watching ", &position);
81  io_string_write(output, being_get_select_name(sim_sim()), &position);
82  io_console_out(output);
83  }
84 }
85 
92 n_int console_being(void * ptr, n_string response, n_console_output output_function)
93 {
94  noble_simulation * local_sim = (noble_simulation *) ptr;
95 
96  output_function(being_get_select_name(local_sim));
97 
98  return 0;
99 }
100 
108 static void show_friends(void * ptr, n_string beingname, n_int friend_type, n_string result)
109 {
110  noble_simulation * local_sim = (noble_simulation *) ptr;
111  noble_being * local_being;
112  n_int i,found;
113  noble_social * local_social_graph;
114 
116  local_being = being_from_name(local_sim, beingname);
117  if (local_being == 0) return;
118 
120  local_social_graph = being_social(local_being);
121  if (local_social_graph == 0L) return;
122 
124  for (i = 1; i < SOCIAL_SIZE_BEINGS; i++)
125  {
127  if (SOCIAL_GRAPH_ENTRY_EMPTY(local_social_graph, i)) continue;
128 
129  found = 0;
130  switch(friend_type)
131  {
132  case 0:
133  {
134  if (local_social_graph[i].friend_foe >= social_respect_mean(local_sim,local_being))
135  {
136  found = 1;
137  }
138  break;
139  }
140  case 1:
141  {
142  if (local_social_graph[i].friend_foe < social_respect_mean(local_sim,local_being))
143  {
144  found = 1;
145  }
146  break;
147  }
148  case 2:
149  {
150  if (local_social_graph[i].attraction > 0)
151  {
152  found = 1;
153  }
154  break;
155  }
156  }
157 
158  if (found==1)
159  {
160  n_int relationship_index;
161  n_string_block relationship_str1;
162  n_string_block relationship_str2;
163  n_string_block met_being_name;
164  n_string_block result_str;
165 
167  social_graph_link_name(local_sim, local_being, i, BEING_MET, met_being_name);
168 
171  relationship_index = local_social_graph[i].relationship;
172  sprintf(relationship_str2," ");
173 
174  if (relationship_index > RELATIONSHIP_SELF)
175  {
176  being_relationship_description(relationship_index,relationship_str1);
177 
178  if (IS_FAMILY_MEMBER(local_social_graph,i))
179  {
180  sprintf(relationship_str2," (%s)",relationship_str1);
181  }
182  else
183  {
184  n_string_block meeter_being_name;
185  sprintf(meeter_being_name," ");
186  social_graph_link_name(local_sim, local_being, i, BEING_MEETER, meeter_being_name);
187  sprintf(relationship_str2," (%s of %s)",relationship_str1,meeter_being_name);
188  }
189  }
190 
191  if (i != GET_A(local_being,ATTENTION_ACTOR))
192  {
194  sprintf(result_str," %05d %s%s\n",(int)local_social_graph[i].familiarity,met_being_name,relationship_str2);
195  }
196  else
197  {
199  sprintf(result_str," %05d [%s]%s\n",(int)local_social_graph[i].familiarity,met_being_name,relationship_str2);
200  }
202  io_string_write(result, result_str, &watch_string_length);
203  }
204  }
205 }
206 
214 n_int get_time_interval(n_string str, n_int * number, n_int * interval)
215 {
216  n_int i,index=0,ctr=0,result=0,divisor=0;
217  char c;
218  n_string_block buf;
219  n_int retval = -1;
220  n_int length = io_length(str,256);
221 
222  for (i = 0; i < length; i++)
223  {
224  if (str[i] != ' ')
225  {
226  buf[ctr++] = str[i];
227  }
228 
229  if ((str[i] == ' ') || (i==(length-1)))
230  {
231  buf[ctr]=0;
232 
233  switch(index)
234  {
235  case 0:
236  {
237  io_number((n_string)buf, &result, &divisor);
238  *number = result;
239  retval = 0;
240  break;
241  }
242  case 1:
243  {
244  if (ctr==1)
245  {
246  char lower_c;
247  lower_c = c = buf[0];
248  IO_LOWER_CHAR(lower_c);
249  if (c=='m') *interval = INTERVAL_MINS;
250  if (lower_c=='h') *interval = INTERVAL_HOURS;
251  if (lower_c=='d') *interval = INTERVAL_DAYS;
252  if (c=='M') *interval = INTERVAL_MONTHS;
253  if (lower_c=='y') *interval = INTERVAL_YEARS;
254  }
255  else
256  {
257  IO_LOWER_CHAR(buf[0]);
258  if (io_find((n_string)buf,0,ctr,"min",3)>-1) *interval = INTERVAL_MINS;
259  if ((io_find((n_string)buf,0,ctr,"hour",4)>-1) ||
260  (io_find((n_string)buf,0,ctr,"hr",2)>-1))
261  {
262  *interval = INTERVAL_HOURS;
263  }
264  if (io_find((n_string)buf,0,ctr,"day",3)>-1) *interval = INTERVAL_DAYS;
265  if (io_find((n_string)buf,0,ctr,"mon",3)>-1) *interval = INTERVAL_MONTHS;
266  }
267 
268  break;
269  }
270  }
271 
272  index++;
273  ctr=0;
274  }
275  }
276  return retval;
277 }
278 
279 static void console_simulation_loop(noble_simulation * local_sim, noble_being * local_being, void * data)
280 {
281  n_int *int_data = data;
282  if (FIND_SEX(GET_I(local_being)) == SEX_FEMALE)
283  {
284  int_data[0]++;
285  }
286  if ((local_sim->land->date - being_dob(local_being)) < AGE_OF_MATURITY)
287  {
288  int_data[1]++;
289  }
290 }
291 
299 n_int console_simulation(void * ptr, n_string response, n_console_output output_function)
300 {
301  noble_simulation * local_sim = (noble_simulation *) ptr;
302  n_string_block beingstr, time;
303  n_int int_data[2];
304 
305  being_loop_no_thread(local_sim, 0L, console_simulation_loop, int_data);
306 
307  sprintf(beingstr,"Map dimension: %ld\n", land_map_dimension(local_sim->land));
308  sprintf(beingstr,"%sLand seed: %d %d\n",beingstr, (int)local_sim->land->genetics[0],(int)local_sim->land->genetics[1]);
309  sprintf(beingstr,"%sPopulation: %d ", beingstr, (int)local_sim->num);
310  sprintf(beingstr,"%sAdults: %d Juveniles: %d\n", beingstr, (int)(local_sim->num - int_data[1]),(int)int_data[1]);
311  if (local_sim->num > 0)
312  {
313  sprintf(beingstr,"%sFemales: %d (%.1f%%) Males: %d (%.1f%%)\n", beingstr,
314  (int)int_data[0], int_data[0]*100.0f/local_sim->num,
315  (int)(local_sim->num - int_data[0]),(local_sim->num - int_data[0])*100.0f/local_sim->num);
316  }
317  sprintf(beingstr,"%sTide level: %d\n", beingstr, (int)local_sim->land->tide_level);
318 
319  io_time_to_string(time, local_sim->land->time, local_sim->land->date);
320 
321  if (local_sim->delta_cycles)
322  {
323  sprintf(beingstr,"%sBrain Cycles Per Second: %ld\n", beingstr, local_sim->delta_cycles);
324  }
325 
326 
327  sprintf(beingstr,"%s%s", beingstr, time);
328 
329  if (simulation_executing)
330  {
331  sprintf(beingstr,"%s Simulation running", beingstr);
332  }
333  else
334  {
335  sprintf(beingstr,"%s Simulation not running", beingstr);
336  }
337 
338  output_function(beingstr);
339 
340  return 0;
341 }
342 
350 n_int console_list(void * ptr, n_string response, n_console_output output_function)
351 {
352  noble_simulation * local_sim = (noble_simulation *) ptr;
353  noble_being * local_being;
354  n_string_block line_text;
355  n_int location = 0;
356  n_uint loop = 0;
357 
359  while (loop < local_sim->num)
360  {
361  n_string_block name;
362  n_int length;
364  local_being = &local_sim->beings[loop];
365 
367  being_name_simple(local_being, name);
368 
369  io_string_write(line_text, name, &location);
370 
371  length = io_length(name, STRING_BLOCK_SIZE);
372 
373  while (length < 24)
374  {
375  io_string_write(line_text, " ", &location);
376  length++;
377  }
378 
379  if ((loop % 3) == 2)
380  {
381  output_function(line_text);
382  location = 0;
383  }
384  loop++;
385  }
386 
387  if (location != 0)
388  {
389  output_function(line_text);
390  }
391 
392  return 0;
393 }
394 
395 n_int console_next(void * ptr, n_string response, n_console_output output_function)
396 {
398  return 0;
399 }
400 
401 n_int console_previous(void * ptr, n_string response, n_console_output output_function)
402 {
404  return 0;
405 }
406 
407 #ifdef BRAINCODE_ON
409 {
410  if (local_sim->select)
411  {
412  noble_being * local_being = local_sim->select;
413  n_byte * internal_bc = being_braincode_internal(local_being);
414  n_byte * external_bc = being_braincode_external(local_being);
415  n_int loop = 0;
416 
417  n_string_block initial_information;
418  n_int position = 0;
419 
420  io_string_write(initial_information, "EXT INT", &position);
421 
422  (*function)(initial_information, -1);
423 
424  while(loop < 22)
425  {
426  n_string_block command_information;
427 
428  n_string_block first_internal;
429  n_string_block first_external;
430 
431  position = 0;
432 
433  brain_three_byte_command((n_string)first_internal, &internal_bc[loop*BRAINCODE_BYTES_PER_INSTRUCTION]);
434  brain_three_byte_command((n_string)first_external, &external_bc[loop*BRAINCODE_BYTES_PER_INSTRUCTION]);
435 
436  if (loop == 21)
437  {
438  io_string_write(command_information, first_external, &position);
439  io_string_write(command_information, " ", &position);
440  io_string_write(command_information, first_internal, &position);
441  }
442  else
443  {
444  n_string_block second_internal;
445  n_string_block second_external;
446 
447  brain_three_byte_command((n_string)second_internal, &internal_bc[(loop+22)*BRAINCODE_BYTES_PER_INSTRUCTION]);
448  brain_three_byte_command((n_string)second_external, &external_bc[(loop+22)*BRAINCODE_BYTES_PER_INSTRUCTION]);
449 
450  io_string_write(command_information, first_external, &position);
451  io_string_write(command_information, " ", &position);
452  io_string_write(command_information, second_external, &position);
453  io_string_write(command_information, " ", &position);
454  io_string_write(command_information, first_internal, &position);
455  io_string_write(command_information, " ", &position);
456  io_string_write(command_information, second_internal, &position);
457  }
458  (*function)(command_information, loop);
459  loop++;
460  }
461  }
462 
463 }
464 #endif
465 
473 static void watch_appearance(void *ptr, n_string beingname, noble_being * local_being, n_string result)
474 {
475  n_string_block str;
476  n_genetics * genetics = being_genetics(local_being);
477 
478  sprintf(str,"Height: %.3f m\n", (int)GET_BEING_HEIGHT(local_being)/1000.0f);
479  io_string_write(result, str, &watch_string_length);
480 
481  sprintf(str,"Mass: %.2f Kg\n", (float)GET_M(local_being)/100.0f);
482  io_string_write(result, str, &watch_string_length);
483 
484  sprintf(str,"Body fat: %.2f Kg\n", (float)GET_BODY_FAT(local_being)/100.0f);
485  io_string_write(result, str, &watch_string_length);
486 
487  sprintf(str,"Hair length: %.1f mm\n", (float)(GENE_HAIR(genetics)*100.0f/160.0f));
488  io_string_write(result, str, &watch_string_length);
489 
490  sprintf(str,"Pigmentation: %02d\n", (int)(GENE_PIGMENTATION(genetics)));
491  io_string_write(result, str, &watch_string_length);
492 
493  sprintf(str,"Body frame: %02d\n", (int)(GENE_FRAME(genetics)));
494  io_string_write(result, str, &watch_string_length);
495 
496  sprintf(str,"Eye separation: %.1f mm\n",
497  80.0f + ((float)(GENE_EYE_SEPARATION(genetics))));
498  io_string_write(result, str, &watch_string_length);
499 
500  sprintf(str,"Eye color: %02d Eye shape: %02d\n",
501  (int)(GENE_EYE_COLOR(genetics)),
502  (int)(GENE_EYE_SHAPE(genetics)));
503  io_string_write(result, str, &watch_string_length);
504 
505  sprintf(str,"Nose shape: %02d Ear shape: %02d\n",
506  (int)(GENE_NOSE_SHAPE(genetics)),
507  (int)(GENE_EAR_SHAPE(genetics)));
508  io_string_write(result, str, &watch_string_length);
509 
510  sprintf(str,"Eyebrow shape: %02d Mouth shape: %02d\n",
511  (int)(GENE_EYEBROW_SHAPE(genetics)),
512  (int)(GENE_MOUTH_SHAPE(genetics)));
513  io_string_write(result, str, &watch_string_length);
514 }
515 
516 static n_string static_result;
517 
518 static void watch_line_braincode(n_string string, n_int line)
519 {
520  io_string_write(static_result, string, &watch_string_length);
521  io_string_write(static_result, "\n", &watch_string_length);
522 }
523 
531 static void watch_braincode(void *ptr, n_string beingname, noble_being * local_being, n_string result)
532 {
533 #ifdef BRAINCODE_ON
534  n_int i;
535  io_string_write(result, "\nRegisters:\n", &watch_string_length);
536  for (i=0; i<BRAINCODE_PSPACE_REGISTERS; i++)
537  {
538  result[watch_string_length++]=(char)(65+(local_being->braincode_register[i]%60));
539  }
540  result[watch_string_length++]='\n';
541  result[watch_string_length++]='\n';
542 
543  static_result = result;
544 
545  console_populate_braincode(ptr,watch_line_braincode);
546 
547  static_result = 0L;
548  result[watch_string_length++]='\n';
549 #endif
550 }
551 
552 static void watch_speech(void *ptr, n_string beingname, noble_being * local, n_string result)
553 {
554 #ifdef BRAINCODE_ON
555  n_int loop;
556  n_byte * external_bc = being_braincode_external(local);
557  for (loop = 0; loop < BRAINCODE_SIZE/BRAINCODE_BYTES_PER_INSTRUCTION; loop++)
558  {
559  n_string_block sentence;
560 
561  brain_sentence((n_string)sentence, &external_bc[loop*3]);
562 
563  io_string_write(result, sentence, &watch_string_length);
564  if ((loop &3) == 3)
565  {
566  result[watch_string_length++]='.';
567  }
569  {
570  result[watch_string_length++]=' ';
571  }
572  }
573  result[watch_string_length++]='.';
574  result[watch_string_length++]='\n';
575 #endif
576 }
577 
578 
579 
587 static void watch_social_graph(void *ptr, n_string beingname, noble_being * local_being, n_string result)
588 {
589  io_string_write(result, "\nFriends:\n", &watch_string_length);
590  show_friends(ptr, beingname, 0, result);
591  io_string_write(result, "\nEnemies:\n", &watch_string_length);
592  show_friends(ptr, beingname, 1, result);
593 }
594 
602 static void watch_episodic(void *ptr, n_string beingname, noble_being * local_being, n_string result)
603 {
604  noble_simulation * local_sim = (noble_simulation *) ptr;
605 
606  n_uint i;
607  for (i = 0; i < EPISODIC_SIZE; i++)
608  {
609  n_string_block str = {0};
610  n_string_block description = {0};
611  n_int position = 0;
612  (void)episode_description(local_sim, local_being, i, str);
613  if (io_length(str, STRING_BLOCK_SIZE) > 0)
614  {
615  if (GET_A(local_being,ATTENTION_EPISODE) != i)
616  {
617  io_string_write(description, " ", &position);
618  io_string_write(description, str, &position);
619  io_string_write(description, "\n", &position);
620  }
621  else
622  {
623  io_string_write(description, " [", &position);
624  io_string_write(description, str, &position);
625  io_string_write(description, "]\n", &position);
626  }
627  io_string_write(result, description, &watch_string_length);
628  }
629  }
630 }
631 
639 static void watch_genome(void *ptr, n_string beingname, noble_being * local_being, n_string result)
640 {
641  n_int i,j;
642  n_byte genome[CHROMOSOMES*8+1];
643 
644  for (i = 0; i < 2; i++)
645  {
646  body_genome((n_byte)i, being_genetics(local_being), genome);
647  for (j = 0; j < CHROMOSOMES*8; j++)
648  {
649  if ((j>0) && (j%8==0))
650  {
651  io_string_write(result, "\t", &watch_string_length);
652  }
653  result[watch_string_length++] = genome[j];
654  }
656  }
657 }
658 
666 static void watch_brainprobes(void *ptr, n_string beingname, noble_being * local_being, n_string result)
667 {
668 #ifdef BRAINCODE_ON
669  n_int i;
670  n_string_block str2;
671  n_string_block type_str;
672  io_string_write(result, "\n Type Posn Freq Offset Addr State\n ", &watch_string_length);
673  for (i = 0; i < 36; i++)
674  {
675  io_string_write(result, "-", &watch_string_length);
676  }
677  io_string_write(result, "\n", &watch_string_length);
678 
679  sprintf((n_string)type_str,"%s","Input ");
680 
681  for (i = 0; i < BRAINCODE_PROBES; i++)
682  {
683  if (local_being->brainprobe[i].type == INPUT_SENSOR)
684  {
685  sprintf((n_string)str2," %s %03d %02d %03d %03d %d\n",
686  type_str,
687  local_being->brainprobe[i].position,
688  local_being->brainprobe[i].frequency,
689  local_being->brainprobe[i].offset,
690  local_being->brainprobe[i].address,
691  local_being->brainprobe[i].state);
693  }
694  }
695 
696  sprintf((n_string)type_str,"%s","Output");
697 
698  for (i = 0; i < BRAINCODE_PROBES; i++)
699  {
700  if (local_being->brainprobe[i].type == OUTPUT_ACTUATOR)
701  {
702  sprintf((n_string)str2," %s %03d %02d %03d %03d %d\n",
703  type_str,
704  local_being->brainprobe[i].position,
705  local_being->brainprobe[i].frequency,
706  local_being->brainprobe[i].offset,
707  local_being->brainprobe[i].address,
708  local_being->brainprobe[i].state);
710  }
711  }
712 #endif
713 }
714 
722 static void watch_stats(void *ptr, n_string beingname, noble_being * local_being, n_string result)
723 {
724  noble_simulation *local_sim = (noble_simulation *)ptr;
725  n_string_block str;
726  n_string_block relationship_str;
727  n_string_block status;
728  n_int heart_rate = 0;
729  n_int breathing_rate = 0;
730 
731  if (local_being == 0L)
732  {
733  (void)SHOW_ERROR("No being for stats");
734  return;
735  }
736 
737  being_state_description(being_state(local_being), status);
738  being_relationship_description(GET_A(local_being,ATTENTION_RELATIONSHIP),relationship_str);
739 
740  sprintf(str, "\n=== %s ===\n%s\nGeneration %lu:%lu\nHeart rate %d bpm\tBreathing rate %d Vf\nEnergy %ld\t\tLocation: %ld %ld\nHonor: %d\t\tHeight: %d\nFacing: %d\t\tSex: %c\nAge in days: %ld\nDrives:\n Hunger: %d\t\tSocial: %d\n Fatigue: %d\t\tSex: %d\nBody Attention: %s\nRelationship Attention: %s\n",
741  beingname, status,
742  (n_uint)local_being->generation_min,
743  (n_uint)local_being->generation_max,
744  (int)heart_rate, (int)breathing_rate,
745  being_energy(local_being),
746  being_location_x(local_being), being_location_y(local_being),
747  being_honor(local_being),
748  GET_BEING_HEIGHT(local_being),
749  being_facing(local_being),
750  ((FIND_SEX(GET_I(local_being)) == SEX_FEMALE) ? 'F' : 'M'),
751  local_sim->land->date - being_dob(local_being),
752  (int)being_drive(local_being, DRIVE_HUNGER),
753  (int)being_drive(local_being, DRIVE_SOCIAL),
754  (int)being_drive(local_being, DRIVE_FATIGUE),
755  (int)being_drive(local_being, DRIVE_SEX),
757  relationship_str
758  );
759 
760  io_string_write(result, str, &watch_string_length);
761  io_string_write(result, "Friends:\n", &watch_string_length);
762  show_friends(ptr, beingname, 0, result);
763  io_string_write(result, "Enemies:\n", &watch_string_length);
764  show_friends(ptr, beingname, 1, result);
765  io_string_write(result, "Episodic:\n", &watch_string_length);
766  watch_episodic(ptr, beingname, local_being, result);
767 }
768 
769 
779 static n_int console_duplicate(void * ptr, n_string response, n_console_output output_function, n_string title, console_generic watch_function)
780 {
781  noble_simulation * local_sim = (noble_simulation *) ptr;
782  noble_being * local_being = 0L;
783  n_string_block beingstr;
784 
786 
787  if ((response == 0) && (local_sim->select))
788  {
789  response = being_get_select_name(local_sim);
790  if (title != 0L)
791  {
792  io_string_write((n_string)beingstr, "\n", &watch_string_length);
793  io_string_write((n_string)beingstr, title, &watch_string_length);
794  io_string_write((n_string)beingstr, " for ", &watch_string_length);
795  io_string_write((n_string)beingstr, response, &watch_string_length);
796  io_string_write((n_string)beingstr, "\n", &watch_string_length);
797  }
798  }
799 
800  if (response != 0L)
801  {
802  local_being = being_from_name(local_sim, response);
803  if (local_being == 0L)
804  {
805  (void)SHOW_ERROR("Being not found");
806  return 0;
807  }
808  being_set_select_name(local_sim, response);
809 
810  watch_function(ptr, being_get_select_name(local_sim), local_being, (n_string)beingstr);
811  beingstr[watch_string_length] = 0;
812  output_function(beingstr);
813  return 0;
814  }
815 
816  (void)SHOW_ERROR("No being was specified");
817  return 0;
818 }
819 
827 n_int console_genome(void * ptr, n_string response, n_console_output output_function)
828 {
829  return console_duplicate(ptr, response, output_function, "Genome", watch_genome);
830 }
831 
839 n_int console_stats(void * ptr, n_string response, n_console_output output_function)
840 {
841  return console_duplicate(ptr, response, output_function, 0L, watch_stats);
842 }
843 
851 n_int console_probes(void * ptr, n_string response, n_console_output output_function)
852 {
853  return console_duplicate(ptr, response, output_function, "Brain probes", watch_brainprobes);
854 }
855 
863 n_int console_episodic(void * ptr, n_string response, n_console_output output_function)
864 {
865  return console_duplicate(ptr, response, output_function, "Episodic memory", watch_episodic);
866 }
867 
875 n_int console_social_graph(void * ptr, n_string response, n_console_output output_function)
876 {
877  return console_duplicate(ptr, response, output_function, "Social graph", watch_social_graph);
878 }
879 
887 n_int console_braincode(void * ptr, n_string response, n_console_output output_function)
888 {
889  return console_duplicate(ptr, response, output_function, "Braincode", watch_braincode);
890 }
891 
892 n_int console_speech(void * ptr, n_string response, n_console_output output_function)
893 {
894  return console_duplicate(ptr, response, output_function, "Speech", watch_speech);
895 }
896 
897 
905 n_int console_appearance(void * ptr, n_string response, n_console_output output_function)
906 {
907  return console_duplicate(ptr, response, output_function, "Appearance", watch_appearance);
908 }
909 
910 static void histogram_being_state_loop(noble_simulation * local_sim, noble_being * local_being, void * data)
911 {
912  n_uint * histogram = data;
913  n_uint n = 2;
914  if (being_state(local_being) == BEING_STATE_ASLEEP)
915  {
916  histogram[0]++;
917  }
918  else
919  {
920  while (n < BEING_STATES)
921  {
922  if (being_state(local_being) & (1<<(n-1)))
923  {
924  histogram[n]++;
925  }
926  n++;
927  }
928  }
929 }
930 
937 static void histogram_being_state(noble_simulation * local_sim, n_uint * histogram, n_byte normalize)
938 {
939  n_uint i;
940 
941  for (i = 0; i < BEING_STATES; i++) histogram[i] = 0;
942 
943  being_loop_no_thread(local_sim, 0L, histogram_being_state_loop, histogram);
944 
945  if (normalize)
946  {
947  n_uint tot=0;
948  for (i = 0; i < BEING_STATES; i++) tot += histogram[i];
949  if (tot > 0)
950  {
951  for (i = 0; i < BEING_STATES; i++) histogram[i] = histogram[i]*1000/tot;
952  }
953  }
954 }
955 
961 static void watch_being(void * ptr, n_console_output output_function)
962 {
963  noble_simulation * local_sim = (noble_simulation *) ptr;
964 
965  noble_being * local_being;
966  n_string_block beingstr;
967  n_uint i;
968  n_int j;
969  n_byte2 state;
970 
971 
972  if (being_remove_internal())
973  {
974  do {}
975  while(being_remove_internal());
976  }
977 
979 
980  if (watch_type == WATCH_STATES)
981  {
982  n_uint histogram[16];
983  n_string_block str;
984 
986 
987  sprintf((n_string)str,"\nTime: %02d:%02d\n\n",
988  (int)(local_sim->land->time/60),
989  (int)(local_sim->land->time%60));
990  io_string_write(beingstr,str,&watch_string_length);
991  histogram_being_state(local_sim, (n_uint*)histogram, 1);
992  for (i = 0; i < BEING_STATES; i++)
993  {
994  if (i == 1) continue;
996  if (i==0)
997  {
998  state = 0;
999  }
1000  else
1001  {
1002  state = (n_byte2)(1 << (i-1));
1003  }
1004 
1005  being_state_description(state, (n_string)str);
1007  io_string_write(beingstr,":",&watch_string_length);
1008  for (j = 0; j < 12 - io_length((n_string)str,STRING_BLOCK_SIZE); j++)
1009  {
1010  io_string_write(beingstr," ",&watch_string_length);
1011  }
1012 
1013  if (histogram[i] > 0)
1014  {
1015  sprintf((n_string)str,"%.1f\n",histogram[i]/10.0f);
1016  io_string_write(beingstr,str,&watch_string_length);
1017  }
1018  else
1019  {
1020  io_string_write(beingstr,"----\n",&watch_string_length);
1021  }
1022  }
1023  output_function(beingstr);
1024  return;
1025  }
1026 
1027  if (local_sim->select)
1028  {
1029  local_being = local_sim->select;
1030 
1031  watch_string_length = 0;
1032 
1033  switch(watch_type)
1034  {
1035  case WATCH_ALL:
1036  {
1037  watch_stats(ptr, being_get_select_name(local_sim), local_being, beingstr);
1038  break;
1039  }
1040  case WATCH_SOCIAL_GRAPH:
1041  {
1042  watch_social_graph(ptr, being_get_select_name(local_sim), local_being, beingstr);
1043  break;
1044  }
1045  case WATCH_EPISODIC:
1046  {
1047  watch_episodic(ptr, being_get_select_name(local_sim), local_being, beingstr);
1048  break;
1049  }
1050  case WATCH_BRAINCODE:
1051  {
1052  watch_braincode(ptr, being_get_select_name(local_sim), local_being, beingstr);
1053  break;
1054  }
1055  case WATCH_BRAINPROBES:
1056  {
1057  watch_brainprobes(ptr, being_get_select_name(local_sim), local_being, beingstr);
1058  break;
1059  }
1060 
1061  case WATCH_APPEARANCE:
1062  {
1063  watch_appearance(ptr, being_get_select_name(local_sim), local_being, beingstr);
1064  break;
1065  }
1066  case WATCH_SPEECH:
1067  {
1068  watch_speech(ptr, being_get_select_name(local_sim), local_being, beingstr);
1069  break;
1070  }
1071  }
1072 
1073  if (watch_type != WATCH_NONE)
1074  {
1075  output_function(beingstr);
1076  }
1077  }
1079 }
1080 
1081 static n_int console_on_off(n_string response)
1082 {
1083  n_uint length;
1084 
1085  if (response == 0) return -1;
1086 
1087  length = io_length(response,STRING_BLOCK_SIZE);
1088  if ((io_find(response,0,length,"off",3)>-1) ||
1089  (io_find(response,0,length,"0",1)>-1) ||
1090  (io_find(response,0,length,"false",5)>-1) ||
1091  (io_find(response,0,length,"no",2)>-1))
1092  {
1093  return 0;
1094  }
1095 
1096  if ((io_find(response,0,length,"on",2)>-1) ||
1097  (io_find(response,0,length,"1",1)>-1) ||
1098  (io_find(response,0,length,"true",4)>-1) ||
1099  (io_find(response,0,length,"yes",3)>-1))
1100  {
1101  return 1;
1102  }
1103 
1104  return -1;
1105 }
1106 
1107 n_int console_event(void * ptr, n_string response, n_console_output output_function)
1108 {
1109  n_int return_response = console_on_off(response);
1110 
1111  if (return_response == -1)
1112  {
1113  if (io_find(response, 0, io_length(response,STRING_BLOCK_SIZE),"social",6)>-1)
1114  {
1115  episodic_logging(output_function, 1);
1116  output_function("Event output for social turned on");
1117  }
1118  return 0;
1119  }
1120 
1121  if (return_response == 0)
1122  {
1123  episodic_logging(0L, 0);
1124  output_function("Event output turned off");
1125  }
1126  else
1127  {
1128  episodic_logging(output_function, 0);
1129  output_function("Event output turned on");
1130  }
1131 
1132  return 0;
1133 }
1134 
1135 
1143 n_int console_logging(void * ptr, n_string response, n_console_output output_function)
1144 {
1145  n_int return_response = console_on_off(response);
1146 
1147  if (return_response == -1)
1148  {
1149  return 0;
1150  }
1151  if (return_response == 0)
1152  {
1153  nolog = 1;
1154  indicator_index = 0;
1156  output_function("Logging turned off");
1157  }
1158  else
1159  {
1160  nolog = 0;
1161  indicator_index = 1;
1162  output_function("Logging turned on");
1163  }
1164  return 0;
1165 }
1166 
1167 
1175 static n_int console_compare_brain(n_byte * braincode0, n_byte * braincode1, n_int block_size)
1176 {
1177  n_int block_size_bytes = block_size*BRAINCODE_BYTES_PER_INSTRUCTION;
1178  n_int loop = 0;
1179  while (loop < (BRAINCODE_SIZE - block_size_bytes))
1180  {
1181  n_int loop2 = 0;
1182  while (loop2 < (BRAINCODE_SIZE - block_size_bytes))
1183  {
1184  n_int block_step = 0;
1185  while (block_step < block_size)
1186  {
1187  if (braincode0[loop + block_step*BRAINCODE_BYTES_PER_INSTRUCTION] ==
1188  braincode1[loop2 + block_step*BRAINCODE_BYTES_PER_INSTRUCTION])
1189  {
1190  block_step++;
1191  if (block_step == block_size)
1192  {
1193  return loop;
1194  }
1195  }
1196  else
1197  {
1198  break;
1199  }
1200  }
1202  }
1204  }
1205  return -1;
1206 }
1207 
1215 n_int console_idea(void * ptr, n_string response, n_console_output output_function)
1216 {
1217 #ifdef BRAINCODE_ON
1218 #ifndef CONSOLE_IDEA_MIN_BLOCK_SIZE
1219 #define CONSOLE_IDEA_MIN_BLOCK_SIZE 3
1220 #define CONSOLE_IDEA_MAX_BLOCK_SIZE 8
1221 #endif
1222  const n_int min_block_size = 3;
1223  const n_int max_block_size = 8;
1224  n_uint i, total_matches=0, total_tests=0;
1226  noble_simulation * local_sim = (noble_simulation *) ptr;
1227 
1228  /* clear the histogram */
1229  for (i = 0; i <= (n_uint)(max_block_size - min_block_size); i++)
1230  {
1231  histogram[i]=0;
1232  }
1233 
1234  if (local_sim->select)
1235  {
1236  n_uint loop = 0;
1237  while (loop < local_sim->num)
1238  {
1239  noble_being * local_being = &(local_sim->beings[loop]);
1240  n_byte * bc_external = being_braincode_external(local_being);
1241  if (bc_external)
1242  {
1243 
1244  n_uint loop2 = loop + 1;
1245  while (loop2 < local_sim->num)
1246  {
1247  noble_being * local_being2 = &(local_sim->beings[loop2]);
1248  n_byte * bc_external2 = being_braincode_external(local_being2);
1249 
1250  if (bc_external2)
1251  {
1252  n_int location = 0;
1253  n_int block_size = min_block_size;
1254 
1255  while (block_size <= max_block_size)
1256  {
1257  location = console_compare_brain(bc_external,
1258  bc_external2,
1259  block_size);
1260 
1261  if (location != -1)
1262  {
1263  histogram[block_size-min_block_size]++;
1264  total_matches++;
1265  /* n_string_block output;
1266  sprintf(output, "%ld %ld, %ld",loop, loop2, block_size);
1267  output_function(output); */
1268  }
1269  total_tests++;
1270  block_size++;
1271  }
1272  }
1273  loop2++;
1274  }
1275 
1276  }
1277  loop++;
1278  }
1279  }
1280 
1281  if (total_tests > 0)
1282  {
1283  n_string_block output;
1284 
1285  sprintf(output, "Matches %03u.%04u percent\n",
1286  (n_c_int)(total_matches*100/total_tests),
1287  (n_c_int)(total_matches*1000000/total_tests)%10000);
1288  output_function(output);
1289 
1290 
1291  output_function("Block Percent Instances");
1292  output_function("-------------------------");
1293 
1294  for (i = 0; i <= (n_uint)(max_block_size - min_block_size); i++)
1295  {
1296  sprintf(output, "%02u %03u.%04u %04u",
1297  (n_c_int)(i+min_block_size),
1298  (n_c_int)(histogram[i]*100/total_tests),
1299  (n_c_int)((histogram[i]*1000000/total_tests)%10000),
1300  (n_c_int)histogram[i]);
1301  output_function(output);
1302  }
1303  }
1304 
1305 #endif
1306  return 0;
1307 }
1308 
1316 n_int console_watch(void * ptr, n_string response, n_console_output output_function)
1317 {
1318  noble_simulation * local_sim = (noble_simulation *) ptr;
1319  n_int length;
1320  n_string_block output;
1321  n_int position = 0;
1322 
1323  if (response == 0L)
1324  {
1325  return 0;
1326  }
1327  else
1328  {
1329  length = io_length(response,STRING_BLOCK_SIZE);
1330  }
1331 
1332  if ((length<5) && (io_find(response,0,length,"off",3)>-1))
1333  {
1334  output_function("Stopped watching");
1336  return 0;
1337  }
1338 
1339  if ((length<10) && (io_find(response,0,length,"state",5)>-1))
1340  {
1342  output_function("Watching being states");
1343  return 0;
1344  }
1345 
1346  if (being_from_name(local_sim, response) != 0)
1347  {
1348  being_set_select_name(local_sim, response);
1349  io_string_write(output, "Watching ", &position);
1350  io_string_write(output, being_get_select_name(local_sim), &position);
1351  output_function(output);
1352  position = 0;
1354  }
1355  else
1356  {
1357  if (local_sim->select)
1358  {
1359  if (io_find(response,0,length,"braincode",9)>-1)
1360  {
1362  io_string_write(output, "Watching braincode for ", &position);
1363  io_string_write(output, being_get_select_name(local_sim), &position);
1364  output_function(output);
1365  return 0;
1366  }
1367  if ((io_find(response,0,length,"brainprobe",10)>-1) ||
1368  (io_find(response,0,length,"brain probe",11)>-1) ||
1369  (io_find(response,0,length,"probes",6)>-1))
1370  {
1372  io_string_write(output, "Watching brain probes for ", &position);
1373  io_string_write(output, being_get_select_name(local_sim), &position);
1374  output_function(output);
1375  return 0;
1376  }
1377  if ((io_find(response,0,length,"graph",5)>-1) ||
1378  (io_find(response,0,length,"friend",6)>-1))
1379  {
1381 
1382  io_string_write(output, "Watching social graph for ", &position);
1383  io_string_write(output, being_get_select_name(local_sim), &position);
1384  output_function(output);
1385  return 0;
1386  }
1387  if ((io_find(response,0,length,"episodic",8)>-1) ||
1388  (io_find(response,0,length,"episodic memory",15)>-1) ||
1389  (io_find(response,0,length,"memory",6)>-1))
1390  {
1392 
1393  io_string_write(output, "Watching episodic memory for ", &position);
1394  io_string_write(output, being_get_select_name(local_sim), &position);
1395  output_function(output);
1396  return 0;
1397  }
1398  if (io_find(response,0,length,"speech",6)>-1)
1399  {
1401 
1402  io_string_write(output, "Watching speech for ", &position);
1403  io_string_write(output, being_get_select_name(local_sim), &position);
1404  output_function(output);
1405  return 0;
1406  }
1407  if ((length<5) && (io_find(response,0,length,"all",3)>-1))
1408  {
1410 
1411  io_string_write(output, "Watching ", &position);
1412  io_string_write(output, being_get_select_name(local_sim), &position);
1413  output_function(output);
1414  return 0;
1415  }
1416 
1417  if (io_find(response,0,length,"appear",6)>-1)
1418  {
1420  io_string_write(output, "Watching appearance for ", &position);
1421  io_string_write(output, being_get_select_name(local_sim), &position);
1422  output_function(output);
1423  return 0;
1424  }
1425  }
1426 
1427  output_function("Being not found\n");
1428  }
1429  return 0;
1430 }
1431 
1439 n_int console_interval(void * ptr, n_string response, n_console_output output_function)
1440 {
1441  n_string_block output;
1442  n_int number=1,interval=INTERVAL_DAYS,interval_set=0;
1443 
1444  if (response != 0)
1445  {
1446  if (io_length(response, STRING_BLOCK_SIZE) > 0)
1447  {
1448  if (get_time_interval(response, &number, &interval) > -1)
1449  {
1450  if (number > 0)
1451  {
1452  save_interval_steps = number * interval_steps[interval];
1453  sprintf(output, "Logging interval set to %u %s",(unsigned int)number, interval_description[interval]);
1454  output_function(output);
1455  interval_set=1;
1456  }
1457  }
1458  }
1459  }
1460 
1461  if (interval_set == 0)
1462  {
1463  if (save_interval_steps < 60)
1464  {
1465  sprintf(output,"Current time interval is %d min(s)", (int)save_interval_steps);
1466  output_function(output);
1467  }
1468  else
1469  {
1470  if (save_interval_steps < 60*24)
1471  {
1472  sprintf(output,"Current time interval is %d hour(s)", (int)save_interval_steps/60);
1473  output_function(output);
1474  }
1475  else
1476  {
1477  sprintf(output,"Current time interval is %d day(s)", (int)save_interval_steps/(60*24));
1478  output_function(output);
1479  }
1480  }
1481  }
1482  return 0;
1483 }
1484 
1485 n_int console_stop(void * ptr, n_string response, n_console_output output_function)
1486 {
1487  simulation_running = 0;
1488  if (output_function)
1489  {
1490  output_function("Simulation stopped");
1491  }
1492  return 0;
1493 }
1494 
1501 n_int console_file(void * ptr, n_string response, n_console_output output_function)
1502 {
1503  io_search_file_format(noble_file_format, response);
1504  return 0;
1505 }
1506 
1514 n_int console_step(void * ptr, n_string response, n_console_output output_function)
1515 {
1516  noble_simulation * local_sim = (noble_simulation *) ptr;
1517  n_uint loop = 0;
1518 
1519  if (response != RUN_STEP_CONST)
1520  {
1521  if (simulation_executing == 1)
1522  {
1523  output_function("Simulation already running");
1524  return 0;
1525  }
1526 
1528  {
1529  output_function("File interaction in use: step");
1530  return 0;
1531  }
1532 
1533  simulation_executing = 1;
1534  }
1535  simulation_running = 1;
1536 
1537  while ((loop < save_interval_steps) && simulation_running)
1538  {
1539  sim_cycle();
1540  if (local_sim->num == 0)
1541  {
1542  simulation_running = 0;
1543  }
1544  loop++;
1545  }
1546  if (response != RUN_STEP_CONST)
1547  {
1548  watch_being(local_sim, output_function);
1549  }
1550 
1551  if (response != RUN_STEP_CONST)
1552  {
1553  simulation_executing = 0;
1554  }
1555 
1556  return 0;
1557 }
1558 
1566 n_int console_run(void * ptr, n_string response, n_console_output output_function)
1567 {
1568  n_uint run = 0;
1569  n_int number = 0, interval = INTERVAL_DAYS;
1570  n_int forever = 0;
1571 
1572  if (simulation_executing == 1)
1573  {
1574  output_function("Simulation already running");
1575  return 0;
1576  }
1577 
1579  {
1580  output_function("File interaction in use: run");
1581  return 0;
1582  }
1583 
1584  simulation_executing = 1;
1585 
1586  simulation_running = 1;
1587 
1588  if (response != 0L)
1589  {
1590  n_int length = io_length(response,STRING_BLOCK_SIZE);
1591  if (length > 0)
1592  {
1593  if ((io_find(response,0,length,"forever",7)>-1))
1594  {
1595  forever = 1;
1596  number = 1;
1597  }
1598  else if (get_time_interval(response, &number, &interval) <= -1)
1599  {
1600  number = -1;
1601  }
1602 
1603  if (number > 0)
1604  {
1605  n_uint i = 0;
1606  n_string_block output;
1607  n_uint end_point = (number * interval_steps[interval]);
1608  n_uint temp_save_interval_steps = save_interval_steps;
1609  n_uint count = 0;
1610  save_interval_steps = 1;
1611 
1612  if (forever)
1613  {
1614  sprintf(output, "Running forever (type \"stop\" to end)");
1615  }
1616  else
1617  {
1618  sprintf(output, "Running for %d %s", (int)number, interval_description[interval]);
1619  }
1620 
1621  output_function(output);
1622 
1623  while ((i < end_point) && simulation_running)
1624  {
1625  console_step(ptr, RUN_STEP_CONST, output_function);
1626 
1627  if (temp_save_interval_steps)
1628  {
1629  if ((count % temp_save_interval_steps) == 0)
1630  {
1631  watch_being(ptr, output_function);
1632  }
1633  }
1634  count++;
1635  if (!forever) i++;
1636  }
1637 
1638  if (temp_save_interval_steps)
1639  {
1640  if ((count % temp_save_interval_steps) != 1)
1641  {
1642  watch_being(ptr, output_function);
1643  }
1644  }
1645 
1646  save_interval_steps = temp_save_interval_steps;
1647  run = 1;
1648  }
1649 
1650  }
1651  }
1652 
1653  simulation_executing = 0;
1654 
1655  if (run == 0)
1656  {
1657  (void)SHOW_ERROR("Time not specified, examples: run 2 days, run 6 hours");
1658  }
1659 
1660  return 0;
1661 }
1662 
1670 n_int console_reset(void * ptr, n_string response, n_console_output output_function)
1671 {
1672  noble_simulation * local_sim = (noble_simulation *) ptr;
1673  n_byte2 seed[2];
1674 
1675  seed[0] = local_sim->land->genetics[0];
1676  seed[1] = local_sim->land->genetics[1];
1677 
1678  math_random3(seed);
1679 
1680  if (sim_init(KIND_NEW_SIMULATION, (seed[0]<<16)|seed[1], MAP_AREA, 0))
1681  {
1682  output_function("Simulation reset");
1683  return 0;
1684  }
1685  output_function("Simulation has not enough memory");
1686  return 1;
1687 }
1688 
1694 static n_byte get_response_mode(n_string response)
1695 {
1696  n_int length;
1697  if (response == 0L) return 0;
1698  length = io_length(response,STRING_BLOCK_SIZE);
1699  if (response != 0)
1700  {
1702  if (io_find(response,0,length,"fem",3)>-1)
1703  {
1704  return 1;
1705  }
1707  if (io_find(response,0,length,"male",4)>-1)
1708  {
1709  return 2;
1710  }
1712  if ((io_find(response,0,length,"juv",3)>-1) ||
1713  (io_find(response,0,length,"chil",4)>-1))
1714  {
1715  return 3;
1716  }
1717  }
1718  return 0;
1719 }
1720 
1721 n_int console_speak(void * ptr, n_string response, n_console_output output_function)
1722 {
1723  n_string_block paragraph = {0};
1724  noble_simulation * local_sim = (noble_simulation*) ptr;
1725  noble_being * local = local_sim->select;
1726  watch_speech(ptr, 0L, local, paragraph);
1727  watch_string_length = 0;
1728  speak_out(response, paragraph);
1729  return 0;
1730 }
1731 
1732 n_int console_alphabet(void * ptr, n_string response, n_console_output output_function)
1733 {
1734  speak_out(response, " aeio a e i o vfstpbjm abefijmopstv a b e f i j m o p s t v. . \n");
1735  return 0;
1736 }
1737 
1738 
1739 n_int console_save(void * ptr, n_string response, n_console_output output_function)
1740 {
1741  n_file * file_opened;
1742  n_string_block output_string;
1743 
1744  if (response == 0L) return 0;
1745 
1747  {
1748  if (output_function)
1749  {
1750  output_function("File interaction in use: save");
1751  }
1752  return 0;
1753  }
1754 
1755  console_stop(ptr,"",output_function);
1756 
1757  file_opened = file_out();
1758  if (file_opened == 0L)
1759  {
1760  return SHOW_ERROR("Failed to generate output contents");
1761  }
1762 
1764 
1765  io_disk_write(file_opened, response);
1766  io_file_free(&file_opened);
1767 
1768  if (output_function)
1769  {
1770  n_int position = 0;
1771  io_string_write(console_file_name, response, &position);
1772  position = 0;
1773 
1774  io_string_write(output_string, "Simulation file ", &position);
1775  io_string_write(output_string, response, &position);
1776  io_string_write(output_string, " saved\n", &position);
1777  output_function(output_string);
1778  }
1779 
1781 
1782  return 0;
1783 }
1784 
1785 /* load simulation-data/script */
1786 static n_int console_base_open(void * ptr, n_string response, n_console_output output_function, n_byte script)
1787 {
1788  if (response == 0L) return 0;
1789 
1791  {
1792  if (output_function)
1793  {
1794  output_function("File interaction in use: open");
1795  }
1796  return 0;
1797  }
1798 
1799  console_stop(ptr,"",output_function);
1801 
1802  if (io_disk_check(response)!=0)
1803  {
1804  n_file * file_opened = io_file_new();
1805  n_string_block output_string;
1806 
1807  if(io_disk_read(file_opened, response) != FILE_OKAY)
1808  {
1809  io_file_free(&file_opened);
1811  return SHOW_ERROR("Failed to open file");
1812  }
1813 
1814  if (script)
1815  {
1816  if (file_interpret(file_opened) != 0)
1817  {
1818  io_file_free(&file_opened);
1820  return SHOW_ERROR("Failed to interpret file");
1821  }
1822  }
1823  else
1824  {
1825  if (file_in(file_opened) != 0)
1826  {
1827  io_file_free(&file_opened);
1829  return SHOW_ERROR("Failed to read in file");
1830  }
1831  if (sim_init(KIND_LOAD_FILE, 0, MAP_AREA, 0) == 0L)
1832  {
1833  return SHOW_ERROR("Not enough memory to load file");
1834  }
1835  io_file_free(&file_opened);
1836  }
1838  if (output_function)
1839  {
1840  n_int position = 0;
1841  io_string_write(console_file_name, response, &position);
1842 
1843  position = 0;
1844 
1845  io_string_write(output_string, "Simulation file ", &position);
1846  io_string_write(output_string, response, &position);
1847  io_string_write(output_string, " open\n", &position);
1848 
1849  output_function(output_string);
1850  }
1851  }
1852  return 0;
1853 }
1854 
1855 /* load simulation data */
1856 n_int console_open(void * ptr, n_string response, n_console_output output_function)
1857 {
1858  return console_base_open(ptr, response, output_function, 0);
1859 }
1860 
1861 /* load apescript file */
1862 n_int console_script(void * ptr, n_string response, n_console_output output_function)
1863 {
1864  return console_base_open(ptr, response, output_function, 1);
1865 }
1866 
1874 n_int console_top(void * ptr, n_string response, n_console_output output_function)
1875 {
1876  noble_simulation * local_sim = (noble_simulation *) ptr;
1877  n_uint i,j;
1878  n_int k;
1879  n_uint max=10;
1880  n_byte * eliminated;
1881  n_uint current_date,local_dob,age_in_years,age_in_months,age_in_days;
1882  n_string_block str;
1883  noble_being * b;
1884  n_byte mode = get_response_mode(response);
1885 
1886  output_function("Honor Name Sex\tAge");
1887  output_function("-----------------------------------------------------------------");
1888 
1889  eliminated = (n_byte *)io_new(local_sim->num*sizeof(n_byte));
1890  for (i = 0; i < local_sim->num; i++) eliminated[i] = 0;
1891 
1892  if (local_sim->num < max) max = local_sim->num;
1893  for (i = 0; i < max; i++)
1894  {
1895  n_int winner = -1;
1896  n_int max_honor = 0;
1897  n_byte passed;
1898  n_string_block output_value;
1899 
1900  for (j = 0; j < local_sim->num; j++)
1901  {
1902  if (eliminated[j] == 0)
1903  {
1904  n_int honor;
1905  b = &local_sim->beings[j];
1906 
1907  honor = being_honor(b);
1908 
1909  if (honor >= max_honor)
1910  {
1911  passed=0;
1912  switch(mode)
1913  {
1914  case 0:
1915  {
1916  passed=1;
1917  break;
1918  }
1919  case 1:
1920  {
1921  if (FIND_SEX(GET_I(b)) == SEX_FEMALE) passed=1;
1922  break;
1923  }
1924  case 2:
1925  {
1926  if (FIND_SEX(GET_I(b)) != SEX_FEMALE) passed=1;
1927  break;
1928  }
1929  case 3:
1930  {
1931  if (AGE_IN_DAYS(local_sim,b)<AGE_OF_MATURITY) passed=1;
1932  break;
1933  }
1934  }
1935 
1936  if (passed!=0)
1937  {
1938  winner = j;
1939  max_honor = honor;
1940  }
1941  }
1942  }
1943  }
1944 
1945  if (winner==-1) break;
1946 
1947  eliminated[winner] = 1;
1948  b = &local_sim->beings[winner];
1949 
1950  sprintf(output_value, "%03d ", (int)(being_honor(b)));
1951 
1952  being_name_simple(b, str);
1953 
1954  sprintf(output_value, "%s%s", output_value,str);
1955 
1956  for (k=0; k<25-io_length(str,STRING_BLOCK_SIZE); k++) sprintf(output_value, "%s ", output_value);
1957 
1958  if (FIND_SEX(GET_I(b)) == SEX_FEMALE)
1959  {
1960  sprintf(output_value,"%sFemale\t",output_value);
1961  }
1962  else
1963  {
1964  sprintf(output_value,"%sMale\t",output_value);
1965  }
1966 
1967  current_date = local_sim->land->date;
1968  local_dob = being_dob(b);
1969  age_in_years = AGE_IN_YEARS(local_sim,b);
1970  age_in_months = ((current_date - local_dob) - (age_in_years * TIME_YEAR_DAYS)) / (TIME_YEAR_DAYS/12);
1971  age_in_days = (current_date - local_dob) - ((TIME_YEAR_DAYS/12) * age_in_months) - (age_in_years * TIME_YEAR_DAYS);
1972 
1973  if (age_in_years>0)
1974  {
1975  sprintf(output_value, "%s%02d yrs ", output_value, (int)age_in_years);
1976  }
1977  if (age_in_months>0)
1978  {
1979  sprintf(output_value,"%s%02d mnths ", output_value,(int)age_in_months);
1980  }
1981  sprintf(output_value,"%s%02d days",output_value, (int)age_in_days);
1982 
1983  output_function(output_value);
1984 
1985  }
1986 
1987  io_free((void**)&eliminated);
1988 
1989  return 0;
1990 }
1991 
1992 n_int console_debug(void * ptr, n_string response, n_console_output output_function)
1993 {
1994  file_audit();
1995 
1996  return 0;
1997 }
1998 
2006 n_int console_epic(void * ptr, n_string response, n_console_output output_function)
2007 {
2008  noble_simulation * local_sim = (noble_simulation *) ptr;
2009  n_uint i, j, k, e;
2010  noble_being * local_being;
2011  noble_episodic * local_episodic;
2012  const n_uint max = 1024;
2013  n_byte2 * first_name = (n_byte2*)io_new(max*sizeof(n_byte2));
2014  n_byte2 * family_name = (n_byte2*)io_new(max*sizeof(n_byte2));
2015  n_byte2 * hits = (n_byte2*)io_new(max*sizeof(n_byte2));
2016  n_byte2 temp;
2017  n_string_block name;
2018  n_byte passed,mode = get_response_mode(response);
2019 
2021  for (i = 0; i < max; i++)
2022  {
2023  first_name[i] = 0;
2024  family_name[i] = 0;
2025  hits[i] = 0;
2026  }
2027 
2028  for (i = 0; i < local_sim->num; i++)
2029  {
2031  local_being = &local_sim->beings[i];
2032 
2034  local_episodic = being_episodic(local_being);
2035 
2037  if (local_episodic == 0L) continue;
2038 
2040  for (e = 0; e < EPISODIC_SIZE; e++)
2041  {
2043  if (local_episodic[e].event > 0)
2044  {
2047  for (j = BEING_MEETER; j <= BEING_MET; j++)
2048  {
2050  if (local_episodic[e].first_name[j] +
2051  local_episodic[e].family_name[j] > 0)
2052  {
2053  passed=0;
2054  switch(mode)
2055  {
2056  case 0:
2057  {
2058  passed=1;
2059  break;
2060  }
2061  case 1:
2062  {
2063  if ((local_episodic[e].first_name[j]>>8) == SEX_FEMALE) passed=1;
2064  break;
2065  }
2066  case 2:
2067  {
2068  if ((local_episodic[e].first_name[j]>>8) != SEX_FEMALE) passed=1;
2069  break;
2070  }
2071  case 3:
2072  {
2073  noble_being * b=0L;
2074  n_string_block name;
2075 
2076  being_name_byte2(local_episodic[e].first_name[j], local_episodic[e].family_name[j], name);
2077 
2078  b = being_from_name(local_sim, name);
2079  if (b!=0L)
2080  {
2081  if (AGE_IN_DAYS(local_sim,b)<AGE_OF_MATURITY) passed=1;
2082  }
2083  break;
2084  }
2085  }
2086 
2087  if (passed != 0)
2088  {
2092  if (being_name_comparison(local_being, local_episodic[e].first_name[j], local_episodic[e].family_name[j]))
2093  {
2094  if (((j == BEING_MET) &&
2095  (local_episodic[e].event != EVENT_SEEK_MATE) &&
2096  (local_episodic[e].event != EVENT_EAT)) ||
2097  (j == BEING_MEETER))
2098  {
2100  for (k = 0; k < max; k++)
2101  {
2102  if (hits[k] == 0)
2103  {
2104  first_name[k] = local_episodic[e].first_name[j];
2105  family_name[k] = local_episodic[e].family_name[j];
2106  break;
2107  }
2108  if (first_name[k] == local_episodic[e].first_name[j])
2109  {
2110  if (family_name[k] == local_episodic[e].family_name[j])
2111  {
2113  break;
2114  }
2115  }
2116  }
2118  if (k < max) hits[k]++;
2119  }
2120  }
2121  }
2122  }
2123  }
2124  }
2125  }
2126  }
2127 
2129  for (i = 0; i < 10; i++)
2130  {
2132  for (j = i+1; j < max; j++)
2133  {
2134  if (hits[j] == 0) break;
2138  if (hits[j] > hits[i])
2139  {
2141  temp = first_name[j];
2142  first_name[j] = first_name[i];
2143  first_name[i] = temp;
2144 
2145  temp = family_name[j];
2146  family_name[j] = family_name[i];
2147  family_name[i] = temp;
2148 
2149  temp = hits[j];
2150  hits[j] = hits[i];
2151  hits[i] = temp;
2152  }
2153  }
2154  if (hits[i] > 0)
2155  {
2156  n_string_block output_value;
2159  being_name_byte2(first_name[i], family_name[i], name);
2160 
2161  sprintf(output_value, "%06d %s", (int)hits[i], name);
2162  output_function(output_value);
2163  }
2164  }
2165 
2167  io_free((void**)&first_name);
2168  io_free((void**)&family_name);
2169  io_free((void**)&hits);
2170 
2171  return 0;
2172 }
2173 
2174 n_int console_quit(void * ptr, n_string response, n_console_output output_function)
2175 {
2176  simulation_executing = 0;
2177  (void)console_stop(ptr, response, output_function);
2178  return io_quit(ptr, response, output_function);
2179 }
2180