Noble Ape
The Central Directories of the Noble Ape Simulation.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
brain.c
Go to the documentation of this file.
1 /****************************************************************
2 
3  brain.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 #include "entity.h"
37 #include "entity_internal.h"
38 
39 #include <stdio.h>
40 
41 /* typical minimum spacing between MVB instructions */
42 #define BRAINCODE_MIN_MVB_SPACING 2
43 
44 #define BRAINCODE_CONSTANT0_BIT (64)
45 #define BRAINCODE_CONSTANT1_BIT (128)
46 
47 #define BRAINCODE_DATA_START BRAINCODE_DAT0
48 #define BRAINCODE_DATA_NUMBER (1 + BRAINCODE_DAT1 - BRAINCODE_DATA_START)
49 
50 #define BRAINCODE_OPERATORS_START BRAINCODE_ADD
51 #define BRAINCODE_OPERATORS_NUMBER (1 + BRAINCODE_LTP - BRAINCODE_OPERATORS_START)
52 
53 #define BRAINCODE_CONDITIONALS_START BRAINCODE_JMZ
54 #define BRAINCODE_CONDITIONALS_NUMBER (1 + BRAINCODE_SLT - BRAINCODE_CONDITIONALS_START)
55 
56 #define BRAINCODE_SENSORS_START BRAINCODE_SEN
57 #define BRAINCODE_SENSORS_NUMBER (1 + BRAINCODE_SEN3 - BRAINCODE_SENSORS_START)
58 
59 #define BRAINCODE_ACTUATORS_START BRAINCODE_ACT
60 #define BRAINCODE_ACTUATORS_NUMBER (1 + BRAINCODE_ANE - BRAINCODE_ACTUATORS_START)
61 
62 #define BRAINCODE_INSTRUCTION(braincode,i) ((braincode[i] & (BRAINCODE_CONSTANT0_BIT-1)) % BRAINCODE_INSTRUCTIONS)
63 #define BRAINCODE_CONSTANT0(braincode,i) (braincode[i] & BRAINCODE_CONSTANT0_BIT)
64 #define BRAINCODE_CONSTANT1(braincode,i) (braincode[i] & BRAINCODE_CONSTANT1_BIT)
65 #define BRAINCODE_VALUE(braincode,i,n) (braincode[i+1+n])
66 
67 /* Brain definitions */
68 
69 #define B_SIZE (32768)
70 #define B_WR (B_SIZE - 1)
71 #define F_X (1)
72 #define F_Y (32)
73 #define F_Z (1024)
74 #define B_Z (B_SIZE - F_Z)
75 #define B_Y (B_SIZE - F_Y)
76 #define B_X (B_SIZE - F_X)
77 
78 /*
79  * The basic brain formula is;
80  * b(t+1) = a*l + b(t)*m + (b(t)-b(t-1))*n;
81  *
82  * The waking mind works differently to the sleeping mind. This is quantified
83  * with two distinct but similar equations. There are two versions for the awake
84  * and asleep states, in this function it is simplified to;
85  * b(t+1) = a*l_a + b(t)*l_b - b(t-1)*l_c;
86  *
87  * where, l_a = l, l_b = m+n, l_c = n
88  */
89 
90 #define B_FN(ave, bra, obra) ((ave+bra-obra)>>10)
91 
93 #define B_P_LH (br[loc+F_Z]+br[loc+F_Y]+br[loc+F_X])
94 #define B_P_UH (br[loc-F_X]+br[loc-F_Y]+br[loc-F_Z])
95 #define B_N_LH (br[(loc+F_X)&B_WR]+br[(loc+F_Y)&B_WR]+br[(loc+F_Z)&B_WR])
96 #define B_N_UH (br[(loc+B_Z)&B_WR]+br[(loc+B_Y)&B_WR]+br[(loc+B_X)&B_WR])
97 
103 void brain_cycle(n_byte * local, n_byte2 * constants)
104 {
105  n_byte br[B_SIZE];
106  n_byte *bract = local, *obr = &local[B_SIZE];
107  n_int l_a = constants[0];
108  n_int l_c = constants[2];
109  n_int l_b = constants[1] + l_c;
110  n_int loc = 0;
111  n_int average;
112  n_int obr_tmp;
113  n_int br_tmp;
114  n_int count = F_Z;
115 
116  io_copy(bract, br, B_SIZE);
117 
118  do
119  {
120  average = (B_P_LH + B_N_UH);
121  br_tmp = br[loc];
122  obr_tmp = obr[loc];
123 
124  average *= l_a;
125  obr_tmp *= l_c;
126 
127  br_tmp *= l_b;
128  br_tmp -= obr_tmp;
129  average += br_tmp;
130 
131  br[loc++] = (n_byte)(average>>10);
132  count--;
133 
134  }
135  while(count);
136  count = B_Z - F_Z;
137  do
138  {
139  average = br[loc-F_Z];
140  average += br[loc-F_Y];
141  average += br[loc-F_X];
142 
143  br_tmp = br[loc];
144 
145  average += br[loc+F_X];
146  average += br[loc+F_Y];
147  average += br[loc+F_Z];
148 
149  obr_tmp = obr[loc];
150  average *= l_a;
151  obr_tmp *= l_c;
152  br_tmp *= l_b;
153  br_tmp -= obr_tmp;
154  average += br_tmp;
155  br[loc++] = (n_byte)(average>>10);
156  count--;
157  }
158  while (count);
159  count = F_Z;
160  do
161  {
162  average = B_P_UH;
163  br_tmp = br[loc];
164  average += B_N_LH;
165  obr_tmp = obr[loc];
166 
167  average *= l_a;
168  obr_tmp *= l_c;
169  br_tmp *= l_b;
170  br_tmp -= obr_tmp;
171  average += br_tmp;
172 
173  br[loc++] = (n_byte)(average>>10);
174  count--;
175  }
176  while (count);
177 
178  io_copy(bract, obr, B_SIZE);
179  io_copy(br, bract, B_SIZE);
180 
181 }
182 
186 #define BC_FORMAT_A 0
187 #define BC_FORMAT_C 1
188 #define BC_FORMAT_E 2
189 #define BC_FORMAT_F 3
190 #define BC_FORMAT_G 4
191 #define BC_FORMAT_H 5
192 
194 {
196  "DAT0",
197  "DAT1",
198 
200  "ADD ",
201  "SUB ",
202  "MUL ",
203  "DIV ",
204  "MOD ",
205  "MVB ",
206  "MOV ",
207  "JMP ",
208  "CTR ",
209  "SWP ",
210  "INV ",
211  "STP ",
212  "LTP ",
213 
215  "JMZ ",
216  "JMN ",
217  "DJN ",
218  "AND ",
219  "OR ",
220  "SEQ ",
221  "SNE ",
222  "SLT ",
223 
225  "SEN ",
226  "SEN2",
227  "SEN3",
228 
230  "ACT ",
231  "ACT2",
232  "ACT3",
233  "ANE "
234 };
235 
244 static n_int brain_format(n_byte instruction, n_byte command, n_byte value0, n_byte value1)
245 {
246  n_byte is_constant0 = ((command & BRAINCODE_CONSTANT0_BIT) != 0);
247  n_byte is_constant1 = ((command & BRAINCODE_CONSTANT1_BIT) != 0);
248 
249  switch(instruction)
250  {
251  case BRAINCODE_AND:
252  case BRAINCODE_OR:
253  case BRAINCODE_MOV:
254  case BRAINCODE_ADD:
255  case BRAINCODE_SUB:
256  case BRAINCODE_MUL:
257  case BRAINCODE_MOD:
258  if ((!is_constant0) && (!is_constant1))
259  {
260  return BC_FORMAT_A;
261  }
262  return BC_FORMAT_C;
263  break;
264  case BRAINCODE_JMZ:
265  case BRAINCODE_JMN:
266  case BRAINCODE_DJN:
267  return BC_FORMAT_E;
268  break;
269  case BRAINCODE_SEQ:
270  case BRAINCODE_SNE:
271  case BRAINCODE_SLT:
272  if ((!is_constant0) && (!is_constant1))
273  {
274  return BC_FORMAT_A;
275  }
276  return BC_FORMAT_F;
277  break;
278  case BRAINCODE_DAT0:
279  case BRAINCODE_DAT1:
280  case BRAINCODE_JMP:
281  return BC_FORMAT_C;
282  break;
283  case BRAINCODE_INV:
284  if (is_constant0)
285  {
286  return BC_FORMAT_G;
287  }
288 
289  return BC_FORMAT_H;
290 
291  break;
292  case BRAINCODE_STP:
293  case BRAINCODE_LTP:
294  if ((is_constant0) && (!is_constant1))
295  {
296  return BC_FORMAT_F;
297  }
298  if ((is_constant0) && (is_constant1))
299  {
300  return BC_FORMAT_C;
301  }
302  if ((!is_constant0) && (is_constant1))
303  {
304  return BC_FORMAT_E;
305  }
306  break;
307  }
308  return BC_FORMAT_A;
309 }
310 
314 {
316  "a",
317  "o",
320  "mam",
321  "vos",
322  "sie",
323  "fes",
324  "feo",
325  "mas",
326  "vam",
327  "amo",
328  "sam",
329  "mao",
330  "ova",
331  "eef",
332  "fee",
335  "om",
336  "ov",
337  "fi",
338  "im",
339  "se",
340  "es",
341  "os",
342  "is",
345  "favos",
346  "vamos",
347  "famov",
350  "iema",
351  "iova",
352  "iafi",
353  "ovma",
354 };
355 
356 static n_byte brain_vc(n_byte value, n_byte vowel)
357 {
358  if (vowel)
359  {
360  switch (value)
361  {
362  case 3:
363  return 'a';
364  case 1:
365  return 'e';
366  case 2:
367  return 'i';
368  default:
369  return 'o';
370  }
371  }
372  switch (value)
373  {
374  case 0:
375  return 'v';
376  case 1:
377  return 'f';
378  case 2:
379  return 's';
380  case 3:
381  return 't';
382  case 4:
383  return 'p';
384  case 5:
385  return 'b';
386  case 6:
387  return 'j';
388  default:
389  return 'm';
390  }
391 }
392 
393 static void brain_longword(n_string output, n_byte value)
394 {
395  output[0] = brain_vc((value >> 0) & 7,0);
396  output[1] = brain_vc((value >> 3) & 3,1);
397  output[2] = brain_vc((value >> 5) & 7,0);
398  output[4] = 0;
399 }
400 
406 void brain_three_byte_command(n_string string, n_byte * response)
407 {
408  n_byte command = response[0];
409  n_byte value0 = response[1];
410  n_byte value1 = response[2];
411  n_byte instruction = (command & (BRAINCODE_CONSTANT0_BIT-1)) % BRAINCODE_INSTRUCTIONS;
412  n_int format = brain_format(instruction, command, value0, value1);
413 
414  switch(format)
415  {
416  case BC_FORMAT_A:
417  sprintf(string,"%s %03d %03d", braincode_mnemonic[instruction], value0, value1);
418  break;
419  case BC_FORMAT_C:
420  sprintf(string,"%s *%03d *%03d", braincode_mnemonic[instruction], value0, value1);
421  break;
422  case BC_FORMAT_E:
423  sprintf(string,"%s %03d *%03d", braincode_mnemonic[instruction], value0, value1);
424  break;
425  case BC_FORMAT_F:
426  sprintf(string,"%s *%03d %03d", braincode_mnemonic[instruction], value0, value1);
427  break;
428  case BC_FORMAT_G:
429  sprintf(string,"%s %03d ---", braincode_mnemonic[instruction], value0);
430  break;
431  default:
432  sprintf(string,"%s --- %03d", braincode_mnemonic[instruction], value1);
433  break;
434  }
435 }
436 
442 void brain_sentence(n_string string, n_byte * response)
443 {
444  n_byte command = response[0];
445  n_byte value0 = response[1];
446  n_byte value1 = response[2];
447  n_byte instruction = (command & (BRAINCODE_CONSTANT0_BIT-1)) % BRAINCODE_INSTRUCTIONS;
448  n_int format = brain_format(instruction, command, value0, value1);
449  n_string_block first_word, second_word;
450  n_int position = 0;
451 
452  brain_longword(first_word, value0);
453  brain_longword(second_word, value1);
454 
455  switch(format)
456  {
457  case BC_FORMAT_A:
458  case BC_FORMAT_C:
459  case BC_FORMAT_E:
460  case BC_FORMAT_F:
461  io_string_write(string, braincode_spoken_dictionary[instruction], &position);
462  io_string_write(string, " ", &position);
463  io_string_write(string, first_word, &position);
464  io_string_write(string, second_word, &position);
465  break;
466  case BC_FORMAT_G:
467  io_string_write(string, braincode_spoken_dictionary[instruction], &position);
468  io_string_write(string, " ", &position);
469  io_string_write(string, first_word, &position);
470  break;
471  default:
472  io_string_write(string, braincode_spoken_dictionary[instruction], &position);
473  io_string_write(string, " ", &position);
474  io_string_write(string, second_word, &position);
475  break;
476  }
477 }
478 
479 #ifdef BRAINCODE_ON
480 
487 {
488  n_byte2 local_random[2];
489 
490  math_random3(local_random);
491  switch(instruction_type)
492  {
493  case 0:
494  return BRAINCODE_DATA_START+(local_random[0]%(BRAINCODE_DATA_NUMBER));
495  case 1:
496  return BRAINCODE_SENSORS_START+(local_random[0]%(BRAINCODE_SENSORS_NUMBER));
497  case 2:
498  return BRAINCODE_ACTUATORS_START+(local_random[0]%(BRAINCODE_ACTUATORS_NUMBER));
499  case 3:
500  return BRAINCODE_OPERATORS_START+(local_random[0]%(BRAINCODE_OPERATORS_NUMBER));
501  case 4:
503  }
504 
505  return BRAINCODE_DATA_START;
506 }
507 
508 /* return the number of instruction_types in the braincode */
509 
511  noble_simulation * sim,
512  noble_being * local_being,
513  n_int * no_of_sensors,
514  n_int * no_of_actuators,
515  n_int * no_of_operators,
516  n_int * no_of_conditionals,
517  n_int * no_of_data)
518 {
519 #ifdef BRAINCODE_ON
520  n_int i,j,instruction;
521 
522  *no_of_sensors = 0;
523  *no_of_actuators = 0;
524  *no_of_operators = 0;
525  *no_of_conditionals = 0;
526  *no_of_data = 0;
527 
528  for (i=0; i<BRAINCODE_SIZE; i+=3)
529  {
530  for (j=0; j<2; j++)
531  {
532  if (j==0)
533  {
534  instruction = being_braincode_internal(local_being)[i] & 63;
535  }
536  else
537  {
538  instruction = being_braincode_external(local_being)[i] & 63;
539  }
540  if ((instruction >= BRAINCODE_SENSORS_START) && (instruction < BRAINCODE_ACTUATORS_START))
541  {
542  *no_of_sensors = *no_of_sensors + 1;
543  }
544  if ((instruction >= BRAINCODE_ACTUATORS_START) && (instruction < BRAINCODE_OPERATORS_START))
545  {
546  *no_of_actuators = *no_of_actuators + 1;
547  }
548  if ((instruction >= BRAINCODE_OPERATORS_START) && (instruction < BRAINCODE_CONDITIONALS_START))
549  {
550  *no_of_operators = *no_of_operators + 1;
551  }
552  if ((instruction >= BRAINCODE_CONDITIONALS_START) && (instruction < BRAINCODE_DATA_START))
553  {
554  *no_of_conditionals = *no_of_conditionals + 1;
555  }
556  if ((instruction >= BRAINCODE_DATA_START) && (instruction < BRAINCODE_INSTRUCTIONS))
557  {
558  *no_of_data = *no_of_data + 1;
559  }
560  }
561  }
562 #endif
563 }
564 
571 {
572  n_byte2 prob[5], total, index;
573  n_genetics * genetics = being_genetics(local_being);
574  n_byte i;
575  const n_byte2 min=2;
576 
577  prob[0] = (n_byte2)(min + GENE_BRAINCODE_SENSORS(genetics));
578  prob[1] = (n_byte2)(min + GENE_BRAINCODE_ACTUATORS(genetics));
579  prob[2] = (n_byte2)(min + GENE_BRAINCODE_CONDITIONALS(genetics));
580  prob[3] = (n_byte2)(min + GENE_BRAINCODE_OPERATORS(genetics));
581  prob[4] = (n_byte2)(min + GENE_BRAINCODE_DATA(genetics));
582 
583  total = prob[0] + prob[1] + prob[2] + prob[3] + prob[4];
584 
585  if (total == 0)
586  {
587  index = 0;
588  }
589  else
590  {
591  index = being_random(local_being);
592  }
593  total = 0;
594  for (i=0; i<5; i++,total+=prob[i])
595  {
596  if (index>=total)
597  {
599  }
600  }
602 }
603 
610 static n_int get_actor_index(noble_social * social_graph, n_int value)
611 {
612  n_int i;
613 
614  for (i=0; i<SOCIAL_SIZE; i++)
615  {
616  if (SOCIAL_GRAPH_ENTRY_EMPTY(social_graph,i)) break;
617  }
618 
619  if (i == 0) return 0;
620 
621  return value % i;
622 }
623 
631 static n_int get_actor_index_from_episode(
632  noble_social * social_graph,
633  noble_episodic * episodic_event,
634  n_int episode_index)
635 {
636  n_int i,actor_index=-1;
637 
638  for (i=1; i<SOCIAL_SIZE_BEINGS; i++)
639  {
640  if (!SOCIAL_GRAPH_ENTRY_EMPTY(social_graph,i))
641  {
642  if (social_graph[i].family_name == episodic_event[episode_index].family_name)
643  {
644  actor_index = i;
645  if (social_graph[i].first_name == episodic_event[episode_index].first_name)
646  {
647  actor_index = i;
648  break;
649  }
650  }
651  }
652  }
653  return actor_index;
654 }
655 
656 typedef n_int (n_similar)(noble_episodic * episodic, n_int * carry_through);
657 
667 static n_int attention_similar(n_int episode_index,
668  noble_episodic * episodic,
669  n_int * memory_visited,
670  n_int * carry_through,
671  n_similar function)
672 {
673  n_int i;
674  n_int visited_max = memory_visited[episode_index] - (EPISODIC_SIZE>>1);
675  n_int min=-1;
676  n_int next_episode_index = -1;
677  if (visited_max<0) visited_max=0;
678 
679  for (i = 0; i < EPISODIC_SIZE; i++)
680  {
681  if (episodic[i].event == 0) continue;
682 
683  if (i != episode_index)
684  {
686  if (memory_visited[i] <= visited_max)
687  {
689  n_int diff = function(&episodic[i], carry_through);
690  if (diff < 0) diff = -diff;
691  if ((min == -1) || (diff < min))
692  {
694  min = diff;
695  next_episode_index = i;
696  }
697  }
698  }
699  }
700  if (next_episode_index>-1)
701  {
703  memory_visited[next_episode_index] = memory_visited[episode_index]+1;
704  }
705  return next_episode_index;
706 }
707 
714 static n_int similar_time(noble_episodic * episodic, n_int * carry_through)
715 {
716  n_int dt = episodic->space_time.time - carry_through[0];
717  if (dt < 0)
718  {
719  dt = - dt;
720  }
721  return dt;
722 }
723 
731 static n_int attention_similar_time(n_int episode_index,
732  noble_episodic * episodic,
733  n_int * memory_visited)
734 {
735  n_int time = episodic[episode_index].space_time.time;
736  return attention_similar(episode_index, episodic, memory_visited, &time, similar_time);
737 }
738 
745 static n_int similar_affect(noble_episodic * episodic, n_int * carry_through)
746 {
747  n_int da = episodic->affect - carry_through[0];
748  if (da < 0)
749  {
750  da = - da;
751  }
752  return da;
753 }
761 static n_int attention_similar_affect(n_int episode_index,
762  noble_episodic * episodic,
763  n_int * memory_visited)
764 {
765  n_int affect = episodic[episode_index].affect;
766  return attention_similar(episode_index, episodic, memory_visited, &affect, similar_affect);
767 }
768 
775 static n_int similar_name(noble_episodic * episodic, n_int * carry_through)
776 {
777  n_int similarity = 3;
778  n_byte values[2];
779 
780  being_unpack_family(episodic->family_name[BEING_MET], values);
781 
782  if (values[0] == carry_through[0]) similarity--;
783  if (values[1] == carry_through[1]) similarity--;
784  if (episodic->first_name[BEING_MET] == carry_through[2]) similarity--;
785  return similarity;
786 }
787 
788 
796 static n_int attention_similar_name(n_int episode_index,
797  noble_episodic * episodic,
798  noble_being * meeter,
799  n_int * memory_visited)
800 {
801  n_int name[3];
802  n_byte values[2];
803 
804  being_unpack_family(episodic->family_name[BEING_MET], values);
805 
806  name[0] = values[0];
807  name[1] = values[1];
808  name[2] = episodic[episode_index].first_name[BEING_MET];
809 
810  return attention_similar(episode_index, episodic, memory_visited, name, similar_name);
811 }
812 
819 static n_int similar_date(noble_episodic * episodic, n_int * carry_through)
820 {
821  n_int dd = episodic->space_time.date - carry_through[0];
822  if (dd < 0)
823  {
824  dd = - dd;
825  }
826  return dd;
827 }
828 
836 static n_int attention_similar_date(n_int episode_index,
837  noble_episodic * episodic,
838  n_int * memory_visited)
839 {
840  n_int time = episodic[episode_index].space_time.date;
841  return attention_similar(episode_index, episodic, memory_visited, &time, similar_date);
842 }
843 
850 static n_int similar_place(noble_episodic * episodic, n_int * carry_through)
851 {
852  n_int dx = episodic->space_time.location[0] - carry_through[0];
853  n_int dy = episodic->space_time.location[1] - carry_through[1];
855  n_int da = (dx * dx) + (dy * dy);
856  return da;
857 }
858 
866 static n_int attention_similar_place(n_int episode_index,
867  noble_episodic * episodic,
868  n_int * memory_visited)
869 {
870  n_int location[2];
871  location[0] = episodic[episode_index].space_time.location[0];
872  location[1] = episodic[episode_index].space_time.location[1];
873  return attention_similar(episode_index, episodic, memory_visited, location, similar_place);
874 }
875 
886 static n_byte brain_first_sense(noble_simulation * sim, noble_being * meeter_being, noble_being * met_being, noble_social * meeter_social_graph, n_int actor_index, n_byte switcher)
887 {
888  switch (switcher % 32)
889  {
890  case 0:
891  return being_honor(meeter_being);
892  case 1:
893  return being_honor(met_being);
894  case 2:
895  return meeter_being->parasites;
896  case 3:
897  return met_being->parasites;
898  case 4:
899  return meeter_being->crowding;
900  case 5:
901  return being_family_first_name(meeter_being);
902  case 6:
903  return being_family_second_name(meeter_being);
904  case 7:
905  return being_family_first_name(met_being);
906  case 8:
907  return being_family_second_name(met_being);
908  case 9:
909  return being_facing(meeter_being);
910  case 10:
911  return being_facing(met_being);
912  case 11:
913  return being_speed(meeter_being);
914  case 12:
915  return meeter_social_graph[actor_index].familiarity&255;
916  case 13:
917  return meeter_social_graph[actor_index].friend_foe;
918  case 14:
919  return meeter_social_graph[actor_index].attraction;
921  case 15:
922  return (n_byte)(APESPACE_TO_MAPSPACE(being_location_x(meeter_being)) * 255 / land_map_dimension(sim->land));
923  case 16:
924  return (n_byte)(APESPACE_TO_MAPSPACE(being_location_y(meeter_being)) * 255 / land_map_dimension(sim->land));
926  case 17:
927  return (n_byte)(being_state(meeter_being)&255);
929  case 18:
930  return (n_byte)((being_state(meeter_being)>>8)&255);
932  case 19:
933  return (n_byte)being_drive(meeter_being, DRIVE_HUNGER);
934  case 20:
935  return (n_byte)being_drive(meeter_being, DRIVE_SOCIAL);
936  case 21:
937  return (n_byte)being_drive(meeter_being, DRIVE_FATIGUE);
938  case 22:
939  return (n_byte)being_drive(meeter_being, DRIVE_SEX);
940 
942  case 23:
943  if (FIND_SEX(GET_I(meeter_being)) == FIND_SEX(GET_I(met_being)))
944  {
945  return 0;
946  }
947  else
948  {
949  return 255;
950  }
951  break;
952  case 24:
953  if (FIND_SEX(GET_I(met_being)) == SEX_FEMALE)
954  {
955  return 255;
956  }
957  else
958  {
959  return 0;
960  }
961  break;
962  case 25:
963  if (FIND_SEX(GET_I(met_being)) != SEX_FEMALE)
964  {
965  return 255;
966  }
967  else
968  {
969  return 0;
970  }
972  case 26:
973  {
974  n_int v=0;
975  n_int n;
976  for (n=0; n<INVENTORY_SIZE; n++)
977  {
978  if (met_being->inventory[n] & INVENTORY_GROOMED) v++;
979  }
980  return (n_byte) (v<<4);
981  }
982  case 27:
983  {
984  n_int v=0;
985  n_int n;
986  for (n=0; n<INVENTORY_SIZE; n++)
987  {
988  if (meeter_being->inventory[n] & INVENTORY_GROOMED) v++;
989  }
990  return (n_byte) (v<<4);
991  }
992 
994  case 28:
995  {
996  n_int v=0;
997  n_int n;
998  for (n=0; n<INVENTORY_SIZE; n++)
999  {
1000  if (met_being->inventory[n] & INVENTORY_WOUND) v++;
1001  }
1002  return (n_byte) (v<<4);
1003  }
1004  case 29:
1005  {
1006  n_int v=0;
1007  n_int n;
1008  for (n=0; n<INVENTORY_SIZE; n++)
1009  {
1010  if (meeter_being->inventory[n] & INVENTORY_WOUND) v++;
1011  }
1012  return (n_byte) (v<<4);
1013  }
1014 
1016  case 30:
1017  return being_posture(meeter_being);
1018  }
1020  return being_posture(met_being);
1021 
1022 }
1023 
1034 static n_byte brain_third_sense(noble_simulation * sim, noble_being * meeter_being, noble_being * met_being, n_byte internal, n_byte switcher, n_byte * additional_write)
1035 {
1036  n_byte half_switcher = switcher >> 1;
1037  noble_being * important_being = ((switcher & 1) ? met_being : meeter_being);
1038  n_genetics * genetics = being_genetics(important_being);
1039  switch (half_switcher % 10)
1040  {
1043  case 0:
1044  return (n_byte)(GENE_EYE_SHAPE(genetics) << 4);
1045  case 1:
1046  return (n_byte)(GENE_EYE_COLOR(genetics) << 4);
1047  case 2:
1048  return (n_byte)(GENE_EYE_SEPARATION(genetics) << 4);
1049  case 3:
1050  return (n_byte)(GENE_NOSE_SHAPE(genetics) << 4);
1051  case 4:
1052  return (n_byte)(GENE_EAR_SHAPE(genetics) << 4);
1053  case 5:
1054  return (n_byte)(GENE_EYEBROW_SHAPE(genetics) << 4);
1055  case 6:
1056  return (n_byte)(GENE_MOUTH_SHAPE(genetics) << 4);
1057  case 7:/* healthyness */
1058  {
1059  n_byte return_value = 0;
1060 #ifdef IMMUNE_ON
1061  n_int n;
1062  noble_immune_system * immune = &(important_being->immune_system);
1063  return_value = immune->antigens[0];
1064  for (n=1; n<IMMUNE_ANTIGENS; n++)
1065  {
1066  if (immune->antigens[n]>return_value)
1067  {
1068  return_value = immune->antigens[n];
1069  }
1070  }
1071 #endif
1072  return return_value;
1073  }
1075  case 9:
1076  if ((internal!=0) &&
1077  (!(being_state(meeter_being)&BEING_STATE_SHOUTING)) &&
1078  (!(being_state(meeter_being)&BEING_STATE_SPEAKING)) &&
1079  (meeter_being->shout[SHOUT_HEARD]>0))
1080  {
1081  return meeter_being->shout[SHOUT_HEARD];
1082  }
1083  break;
1084  break;
1086  default:
1087  if (switcher == 16)
1088  {
1089  n_uint positive = being_affect(sim,meeter_being,1)>>7;
1090  if (positive>255) positive=255;
1091  return (n_byte)positive;
1092 
1093  }
1094  {
1095  /* (switcher == 17) negative affect */
1096 
1097  n_uint negative=being_affect(sim,meeter_being,0)>>1;
1098  if (negative>255) negative=255;
1099  return (n_byte)negative;
1100  }
1101  }
1102  return additional_write[0];
1103 }
1104 
1111 static n_byte territory_familiarity(noble_being * local_being,
1112  n_byte2 index)
1113 {
1114  n_byte result=0;
1115 #ifdef TERRITORY_ON
1116  n_uint familiarity = (n_uint)(local_being->territory[index].familiarity);
1117  n_uint i,max_familiarity = 1;
1118 
1120  for (i=0; i<TERRITORY_AREA; i++)
1121  {
1122  if (local_being->territory[i].familiarity > max_familiarity)
1123  {
1124  max_familiarity = (n_uint)local_being->territory[i].familiarity;
1125  }
1126  }
1127 
1128  if (max_familiarity == 0)
1129  {
1130  return 0;
1131  }
1132 
1133  result = (n_byte)(familiarity*255/max_familiarity);
1134 #endif
1135  return result;
1136 }
1137 
1138 #define IS_CONST0 (is_constant0 ? value0 : addr0[0])
1139 #define IS_CONST1 (is_constant1 ? value1 : addr1[0])
1140 
1141 
1153  noble_simulation * sim,
1154  n_byte awake,
1155  noble_being * meeter_being,
1156  noble_being * met_being,
1157  n_byte * bc0,
1158  n_byte * bc1,
1160 {
1161 #ifdef EPISODIC_ON
1162  n_byte internal = (meeter_being == met_being);
1163  const n_int braincode_min_loop = 8*BRAINCODE_BYTES_PER_INSTRUCTION;
1164  n_int i = 0, itt = 0;
1165  n_int actor_index, possible_actor_index;
1166  n_int episode_index = (n_int)(GET_A(meeter_being,ATTENTION_EPISODE));
1167  n_int territory_index = (n_int)(GET_A(meeter_being,ATTENTION_TERRITORY));
1168  n_int relationship_index = (n_int)(GET_A(meeter_being,ATTENTION_RELATIONSHIP));
1169  n_int anecdote_episode_index=-1;
1170  n_int intention_episode_index=-1;
1171  n_int memory_visited[EPISODIC_SIZE];
1172 
1173  noble_social * meeter_social_graph = being_social(meeter_being);
1174  noble_episodic * episodic = being_episodic(meeter_being);
1175  n_int max_itterations;
1176  n_byte * pspace = (n_byte*)meeter_being->braincode_register;
1177 
1178  /* what is the current actor index within episodic memory? */
1179  if (being_index>-1)
1180  {
1181  actor_index = being_index;
1182  }
1183  else
1184  {
1185  being_index = GET_A(meeter_being,ATTENTION_ACTOR);
1186  actor_index = being_index;
1187  }
1188 
1189  if (meeter_being == met_being)
1190  {
1193  }
1194  else
1195  {
1197  max_itterations = 8 + meeter_being->learned_preference[PREFERENCE_CHAT];
1198  }
1199 
1202  for (i = 0; i < EPISODIC_SIZE; i++)
1203  {
1204  memory_visited[i] = 0;
1205  }
1206 
1207  i = 0;
1208 
1209  while (itt<max_itterations)
1210  {
1211  n_byte instruction = BRAINCODE_INSTRUCTION(bc0, i);
1212  n_byte is_constant0 = BRAINCODE_CONSTANT0(bc0, i);
1213  n_byte is_constant1 = BRAINCODE_CONSTANT1(bc0, i);
1214  n_byte value0 = BRAINCODE_VALUE(bc0, i, 0);
1215  n_byte value1 = BRAINCODE_VALUE(bc0, i, 1);
1216  n_byte *addr0 = math_general_allocation(bc0, bc1, i+value0);
1217  n_byte *addr1 = math_general_allocation(bc0, bc1, i+value1);
1218 
1219  switch(instruction)
1220  {
1222  case BRAINCODE_SEN:
1223  {
1224  addr1[0] = brain_first_sense(sim,meeter_being, met_being, meeter_social_graph, actor_index, addr0[0]);
1225  break;
1226  }
1227  case BRAINCODE_SEN2:
1228  {
1229  n_int new_episode_index=-1;
1230  n_int switcher = addr0[0]%25;
1231  n_int local_x = APESPACE_TO_MAPSPACE(being_location_x(meeter_being));
1232  n_int local_y = APESPACE_TO_MAPSPACE(being_location_y(meeter_being));
1233  switch (switcher)
1234  {
1236  case 0:
1237  actor_index = get_actor_index(meeter_social_graph, IS_CONST1 % SOCIAL_SIZE);
1239  GET_A(meeter_being,ATTENTION_ACTOR) = (n_byte)actor_index;
1240  break;
1242  case 1:
1243  new_episode_index = IS_CONST1 % EPISODIC_SIZE;
1244  break;
1246  case 2:
1247  territory_index = IS_CONST1;
1248  GET_A(meeter_being,ATTENTION_TERRITORY) = (n_byte)territory_index;
1249  break;
1251  case 3:
1252  GET_A(meeter_being,ATTENTION_BODY) = IS_CONST1 % INVENTORY_SIZE;
1253  break;
1254  case 4:
1255  new_episode_index = attention_similar_place(episode_index, episodic, memory_visited);
1256  break;
1257  case 5:
1258  new_episode_index = attention_similar_time(episode_index, episodic, memory_visited);
1259  break;
1260  case 6:
1261  new_episode_index = attention_similar_date(episode_index, episodic, memory_visited);
1262  break;
1263  case 7:
1264  new_episode_index = attention_similar_name(episode_index, episodic, meeter_being, memory_visited);
1265  break;
1266  case 8:
1267  new_episode_index = attention_similar_affect(episode_index, episodic, memory_visited);
1268  break;
1269  case 9:
1270  addr1[0] = episodic[episode_index].event;
1271  break;
1272  case 10:
1273  addr1[0] = episodic[episode_index].food;
1274  break;
1275  case 11:
1276  addr1[0] = episodic[episode_index].affect&255;
1277  break;
1278  case 12:
1279  addr1[0] = episodic[episode_index].arg&255;
1280  break;
1281  case 13:
1282  addr1[0] = (n_byte)(episodic[episode_index].space_time.location[0] * 255 / land_map_dimension(sim->land));
1283  break;
1284  case 14:
1285  addr1[0] = (n_byte)(episodic[episode_index].space_time.location[1] * 255 / land_map_dimension(sim->land));
1286  break;
1287  case 15:
1288  {
1290  n_int pressure = weather_pressure(sim->land, POSITIVE_LAND_COORD(local_x), POSITIVE_LAND_COORD(local_y));
1291 
1292  if (pressure > 100000) pressure = 100000;
1293  if (pressure < 0) pressure = 0;
1294  addr1[0] = (n_byte)(pressure>>9);
1295  break;
1296  }
1297  case 16:
1298  {
1300  n_vect2 wind;
1301  n_vect2 position;
1302  vect2_populate(&position, local_x, local_y);
1303  weather_wind_vector(sim->land, &position, &wind);
1304  if (wind.x<0) wind.x=-wind.x;
1305  if (wind.y<0) wind.y=-wind.y;
1306  addr1[0] = (n_byte)((wind.x+wind.y)>>7);
1307  break;
1308  }
1309  case 17:
1310  addr1[0] = (n_byte)(sim->land->time>>3);
1311  break;
1312  case 18:
1314  addr1[0] = GET_A(meeter_being,ATTENTION_BODY)*30;
1315  break;
1316  case 19:
1317  #ifdef TERRITORY_ON
1318 
1319  addr1[0] = meeter_being->territory[territory_index].name;
1320  #endif
1321  break;
1322  case 20:
1324  addr1[0] = territory_familiarity(meeter_being,(n_byte2)territory_index);
1325  break;
1326  case 21:
1328  addr1[0] = territory_familiarity(met_being,(n_byte2)territory_index);
1329  break;
1330  case 22:
1331  {
1333  n_byte2 carrying = being_carried(meeter_being,BODY_RIGHT_HAND);
1334  n_byte2 obj_type=0;
1335 
1336  if (carrying==0) carrying = being_carried(meeter_being,BODY_LEFT_HAND);
1337  if (carrying!=0)
1338  {
1339  /* TODO Is this willed into existence? */
1340  switch(addr0[0]%12)
1341  {
1342  case 0:
1343  obj_type = INVENTORY_BRANCH;
1344  break;
1345  case 1:
1346  obj_type = INVENTORY_TWIG;
1347  break;
1348  case 2:
1349  obj_type = INVENTORY_ROCK;
1350  break;
1351  case 3:
1352  obj_type = INVENTORY_SHELL;
1353  break;
1354  case 4:
1355  obj_type = INVENTORY_GRASS;
1356  break;
1357  case 5:
1358  obj_type = INVENTORY_NUT;
1359  break;
1360  case 6:
1361  obj_type = INVENTORY_NUT_CRACKED;
1362  break;
1363  case 7:
1364  obj_type = INVENTORY_SCRAPER;
1365  break;
1366  case 8:
1367  obj_type = INVENTORY_SPEAR;
1368  break;
1369  case 9:
1370  obj_type = INVENTORY_FISH;
1371  break;
1372  case 10:
1373  obj_type = INVENTORY_BIRD_EGGS;
1374  break;
1375  case 11:
1376  obj_type = INVENTORY_LIZARD_EGGS;
1377  break;
1378  }
1379  if (carrying & obj_type)
1380  {
1381  addr1[0] = 255;
1382  }
1383  else
1384  {
1385  addr1[0] = 0;
1386  }
1387  }
1388 
1389  break;
1390  }
1391  case 23:
1392  {
1394  n_int idx = social_get_relationship(meeter_being,(n_byte)relationship_index,sim);
1395  if (idx > -1)
1396  {
1397  actor_index = idx;
1399  GET_A(meeter_being,ATTENTION_ACTOR) = (n_byte)actor_index;
1400  }
1401  break;
1402  }
1403  case 24:
1404  {
1406  relationship_index = 1+(addr1[0]%(OTHER_MOTHER-1));
1408  GET_A(meeter_being,ATTENTION_RELATIONSHIP) = (n_byte)relationship_index;
1409  break;
1410  }
1411  }
1412 
1414  if (new_episode_index>-1)
1415  {
1416  episode_index = new_episode_index;
1417  GET_A(meeter_being,ATTENTION_EPISODE) = (n_byte)episode_index;
1419  possible_actor_index = get_actor_index_from_episode(meeter_social_graph,episodic,episode_index);
1420  if (possible_actor_index>-1)
1421  {
1422  actor_index = possible_actor_index;
1424  GET_A(meeter_being,ATTENTION_ACTOR) = (n_byte)actor_index;
1425  }
1427  GET_A(meeter_being,ATTENTION_TERRITORY) =
1428  (APESPACE_TO_TERRITORY(episodic[episode_index].space_time.location[1])*16)+
1429  APESPACE_TO_TERRITORY(episodic[episode_index].space_time.location[0]);
1430  }
1431  break;
1432  }
1433  case BRAINCODE_SEN3:
1434 
1435  addr1[0] = brain_third_sense(sim,meeter_being, met_being, internal, addr0[0], addr1);
1436  break;
1438  case BRAINCODE_ACT:
1439  {
1440  switch(addr0[0]%6)
1441  {
1443  case 0:
1444  if ((awake != 0) && (addr0[0] > 127))
1445  {
1446  if (internal == 0)
1447  {
1448  social_action(sim, meeter_being, met_being, addr1[0]);
1449  }
1450  else
1451  {
1452  social_action(sim, meeter_being, 0L, addr1[0]);
1453  }
1454  addr0[0] = 0;
1455  }
1456  break;
1458  case 1:
1459  if (!(meeter_being->script_overrides&OVERRIDE_GOAL))
1460  {
1461  meeter_being->goal[0] = GOAL_LOCATION;
1462  meeter_being->goal[1] = episodic[episode_index].space_time.location[0];
1463  meeter_being->goal[2] = episodic[episode_index].space_time.location[1];
1464  meeter_being->goal[3] = GOAL_TIMEOUT;
1465  }
1466  break;
1468  case 2:
1469  {
1470  n_byte fof0=pspace[0];
1471  n_byte fof1=addr1[0];
1472 
1473  if (fof0>(n_byte)(fof1+85))
1474  {
1475  if (meeter_social_graph[actor_index].friend_foe < 170)
1476  {
1477  meeter_social_graph[actor_index].friend_foe++;
1478  }
1479  }
1480  if (fof1>(n_byte)(fof0+85))
1481  {
1482  if (meeter_social_graph[actor_index].friend_foe > 85)
1483  {
1484  meeter_social_graph[actor_index].friend_foe--;
1485  }
1486  }
1487  break;
1488  }
1490  case 3:
1491  {
1492  n_byte att0=addr1[0],att1=pspace[0];
1493 
1494  if (att0>(n_byte)(att1+85))
1495  {
1496  if (meeter_social_graph[actor_index].attraction < 255)
1497  {
1498  meeter_social_graph[actor_index].attraction++;
1499  }
1500  }
1501  if (att1>(n_byte)(att0+85))
1502  {
1503  if (meeter_social_graph[actor_index].attraction > 16)
1504  {
1505  meeter_social_graph[actor_index].attraction--;
1506  }
1507  }
1508  break;
1509  }
1511  case 4:
1513  if ((addr1[0]>100) && (addr1[0]<150))
1514  {
1515  if (meeter_social_graph[actor_index].familiarity < 65535)
1516  {
1517  meeter_social_graph[actor_index].familiarity++;
1518  addr1[0] = 0;
1519  }
1520  }
1521  if ((addr1[0]>150) && (addr1[0]<200))
1522  {
1523  if (meeter_social_graph[actor_index].familiarity > 10)
1524  {
1525  meeter_social_graph[actor_index].familiarity--;
1526  addr1[0] = 0;
1527  }
1528  }
1529  break;
1531  case 5:
1532  {
1533  n_int n = pspace[0] % BRAINCODE_PROBES;
1535 
1536  meeter_being->brainprobe[n].frequency = f;
1537  break;
1538  }
1539  }
1540  break;
1541  }
1542  case BRAINCODE_ACT2:
1543  {
1544  switch(addr0[0]%6)
1545  {
1546  case 0: /* brainprobe type */
1547  {
1548  n_int n = pspace[0] % BRAINCODE_PROBES;
1549  n_byte typ = IS_CONST1 & 1;
1550 
1551  meeter_being->brainprobe[n].type = typ;
1552  break;
1553  }
1554  case 1:
1555  {
1556  n_int n = pspace[0] % BRAINCODE_PROBES;
1557  n_byte adr = IS_CONST1;
1558 
1559  meeter_being->brainprobe[n].address = adr;
1560  break;
1561  }
1562  case 2:
1563  {
1564  n_byte msg = addr1[0];
1565  if (is_constant1)
1566  {
1567  msg = value1;
1568  }
1569  if ((internal!=0) && (awake!=0) &&
1570  (!(being_state(meeter_being)&BEING_STATE_SHOUTING)) &&
1571  (!(being_state(meeter_being)&BEING_STATE_SPEAKING)) &&
1572  (meeter_being->shout[SHOUT_CONTENT]==0) &&
1573  (meeter_being->shout[SHOUT_HEARD]==0) &&
1574  (meeter_being->shout[SHOUT_CTR]==0) &&
1575  (msg>0))
1576  {
1577  meeter_being->shout[SHOUT_CTR] = SHOUT_REFRACTORY;
1578  being_add_state(meeter_being, BEING_STATE_SHOUTING);
1580  meeter_being->shout[SHOUT_VOLUME] = pspace[0];
1582  meeter_being->shout[SHOUT_CONTENT] = msg;
1583  }
1584  break;
1585  }
1586  case 3:
1587  {
1588  if (intention_episode_index != episode_index)
1589  {
1590  n_byte v0 = pspace[0];
1591  n_byte v1 = IS_CONST1;
1592  if (episodic_intention(sim,meeter_being,episode_index,(n_byte2)(v0*10),v1)!=0)
1593  {
1594  intention_episode_index = episode_index;
1595  }
1596  }
1597  break;
1598  }
1599  case 4:
1600  {
1601  n_int n = pspace[0] % BRAINCODE_PROBES;
1602  n_byte offset = IS_CONST1;
1603 
1604  meeter_being->brainprobe[n].offset = offset;
1605  break;
1606  }
1607  case 5:
1608  if (awake != 0)
1609  {
1610  being_set_posture(meeter_being, addr1[0]);
1611  }
1612  break;
1613  }
1614  break;
1615  }
1616  case BRAINCODE_ACT3:
1617  switch(addr0[0]%2)
1618  {
1620  case 0:
1621  {
1622  n_int n = pspace[0] % BRAINCODE_PROBES;
1623  n_byte p = IS_CONST1;
1624  meeter_being->brainprobe[n].position = p;
1625  break;
1626  }
1628  case 1:
1629  {
1630  n_int n;
1631  n_byte prf = addr1[0];
1632 
1633  n = pspace[0]%PREFERENCES;
1634 
1635  if ((prf > 55) && (prf<155))
1636  {
1637  if (meeter_being->learned_preference[n] < 255)
1638  {
1639  meeter_being->learned_preference[n]++;
1640  addr1[0] = 0;
1641  }
1642  }
1643  if (prf >= 155)
1644  {
1645  if (meeter_being->learned_preference[n] > 0)
1646  {
1647  meeter_being->learned_preference[n]--;
1648  addr1[0] = 0;
1649  }
1650  }
1651  break;
1652  }
1653  break;
1654  }
1656  case BRAINCODE_ANE:
1657  if (internal == 0)
1658  {
1661  if (anecdote_episode_index != episode_index)
1662  {
1663  if (episodic_anecdote(sim, meeter_being, met_being)!=0)
1664  {
1665  anecdote_episode_index = episode_index;
1666  }
1667  }
1668  }
1669  break;
1670 
1671  default:
1672  math_general_execution(instruction, is_constant0, is_constant1,
1673  addr0, addr1, value0, &i,
1675  pspace,
1676  &addr0, &addr1,
1677  bc0, bc1,
1678  braincode_min_loop);
1679  break;
1680 
1681 
1682  }
1684  itt++;
1685 
1686  if (i >= BRAINCODE_SIZE)
1687  {
1688  i -= BRAINCODE_SIZE;
1689  }
1690  }
1691 #endif
1692 }
1693 
1694 #endif
1695 
1696