35 #include <libFreeWRL.h>
37 #include "../vrml_parser/Structs.h"
38 #include "../main/headers.h"
40 #include "../x3d_parser/Bindable.h"
41 #include "../scenegraph/Collision.h"
42 #include "../scenegraph/quaternion.h"
44 #include "EAIHeaders.h"
45 #include "SensInterps.h"
47 #include "../vrml_parser/Structs.h"
48 #include "../main/headers.h"
49 #include "../vrml_parser/CParseGeneral.h"
50 #include "../scenegraph/Vector.h"
51 #include "../vrml_parser/CFieldDecls.h"
52 #include "../world_script/CScripts.h"
53 #include "../world_script/fieldSet.h"
54 #include "../world_script/fieldGet.h"
55 #include "../vrml_parser/CParseParser.h"
56 #include "../vrml_parser/CParseLexer.h"
57 #include "../vrml_parser/CParse.h"
59 #include "../x3d_parser/X3DParser.h"
61 #include "EAIHelpers.h"
98 struct Vector *EAINodeIndex;
100 void *EAIHelpers_constructor()
106 void EAIHelpers_init(
struct tEAIHelpers* t){
111 t->prv = EAIHelpers_constructor();
114 p->EAINodeIndex = NULL;
133 struct X3D_Node* thisFieldNodePointer;
138 char *invokedPROTOValue;
146 struct Vector* nodeParams;
153 char *getEAIMemoryPointer (
int node,
int field) {
160 printf (
"bad node in getEAIMemoryPointer\n");
165 if (me->nodeType == EAI_NODETYPE_SCRIPT) {
166 ConsoleMessage (
"EAI error - getting EAIMemoryPointer on a Script node");
170 myParam = vector_get(
struct EAINodeParams *, me->nodeParams, field);
172 if (myParam == NULL) {
173 printf (
"bad field in getEAIMemoryPointer\n");
179 memptr = (
char *)myParam->thisFieldNodePointer;
180 memptr += myParam->fieldOffset;
189 static char * getEAIInvokedValue(
int node,
int field) {
197 printf (
"bad node in getEAIInvokedValue\n");
201 myParam = vector_get(
struct EAINodeParams *, me->nodeParams, field);
203 if (myParam == NULL) {
204 printf (
"bad field in getEAIInvokedValue\n");
208 if (me->nodeType != EAI_NODETYPE_PROTO) {
209 ConsoleMessage (
"getting EAIInvokedValue on a node that is NOT a PROTO");
213 return myParam->invokedPROTOValue;
218 int getEAIActualOffset(
int node,
int field) {
225 printf (
"bad node in getEAIActualOffset\n");
229 myParam = vector_get(
struct EAINodeParams *, me->nodeParams, field);
231 if (myParam == NULL) {
232 printf (
"bad field in getEAIActualOffset\n");
236 return myParam->fieldOffset;
240 int getEAINodeTypeFromTable(
int node) {
246 printf (
"bad node in getEAINodeFromTable\n");
253 struct X3D_Node *getEAINodeFromTable(
int index,
int field) {
261 printf (
"internal EAI error - requesting %d, highest node %d\n",
262 index,vectorSize(p->EAINodeIndex) );
270 if (field <0)
return me->actualNodePtr;
274 myParam = vector_get(
struct EAINodeParams *, me->nodeParams, field);
276 if (myParam == NULL) {
277 printf (
"bad field in getEAINodeFromTable\n");
281 return myParam->thisFieldNodePointer;
285 int registerEAINodeForAccess(
struct X3D_Node* myn) {
291 eaiverbose = tg->EAI_C_CommonFunctions.eaiverbose;
294 if (myn == NULL)
return -1;
295 if (eaiverbose) printf (
"registerEAINodeForAccess - myn %p, type %s\n",myn,stringNodeType(myn->_nodeType));
297 if (p->EAINodeIndex == NULL) {
300 if (eaiverbose) printf (
"creating EAINodeIndexVector\n");
308 for (ctr=1; ctr<=vectorSize(p->EAINodeIndex)-1; ctr++) {
312 if (me->actualNodePtr == myn) {
313 if (eaiverbose) printf (
"registerEAINodeForAccess - already got node\n");
323 newp->actualNodePtr = myn;
324 newp->nodeParams = NULL;
326 if (myn->_nodeType == NODE_Script) newp->nodeType = EAI_NODETYPE_SCRIPT;
328 else if(isProto(myn))
329 newp->nodeType = EAI_NODETYPE_PROTO;
330 else newp->nodeType = EAI_NODETYPE_STANDARD;
335 mynindex = vectorSize(p->EAINodeIndex) -1;
337 if (eaiverbose) printf (
"registerEAINodeForAccess returning index %d nt %s, internal type %d\n",mynindex,
338 stringNodeType(myn->_nodeType),newp->nodeType);
347 int EAI_GetRootNode(
void) {
348 return registerEAINodeForAccess(X3D_NODE(rootNode()));
353 int EAI_GetNode(
const char *str) {
356 int eaiverbose = gglobal()->EAI_C_CommonFunctions.eaiverbose;
358 printf (
"EAI_GetNode - getting %s\n",str);
362 myNode = X3DParser_getNodeFromName(str);
364 if (myNode == NULL) {
366 myNode = parser_getNodeFromName(str);
370 return registerEAINodeForAccess(myNode);
376 int EAI_GetNodeParents(
int cNode,
int **parentNodesAdr)
380 int parentVectorSize;
386 myNode = getEAINodeFromTable(cNode, -1);
391 parentVectorSize = myNode->_parentVector->n;
393 tmp =(
int*) calloc(parentVectorSize,
sizeof(
int));
396 for(i=0; i < parentVectorSize; i++)
399 parentNode = vector_get(
struct X3D_Node *,myNode->_parentVector,i);
402 parentAdr = registerEAINodeForAccess(parentNode);
414 *parentNodesAdr = tmp;
420 int mapToKEYWORDindex (indexT pkwIndex) {
421 if (pkwIndex == PKW_inputOutput)
return KW_inputOutput;
422 if (pkwIndex == PKW_inputOnly)
return KW_inputOnly;
423 if (pkwIndex == PKW_outputOnly)
return KW_outputOnly;
424 if (pkwIndex == PKW_initializeOnly)
return KW_initializeOnly;
429 static int changeExpandedPROTOtoActualNode(
int cNode,
struct X3D_Node **np,
char **fp,
int direction) {
432 int eaiverbose = gglobal()->EAI_C_CommonFunctions.eaiverbose;
435 if (getEAINodeTypeFromTable(cNode) != EAI_NODETYPE_PROTO) {
441 printf (
"changeExpanded - looking for field %s in node...\n",*fp);
444 myProtoDecl = getVRMLbrotoDefinition(X3D_PROTO(*np));
446 printf (
"and, the proto name is %s\n",myProtoDecl->protoName);
450 if (strlen(*fp)>1000)
return FALSE;
452 sprintf (thisID,
"PROTO_%p_%s",myProtoDecl,*fp);
454 if (eaiverbose) printf (
"looking for name :%s:\n",thisID);
456 *np = parser_getNodeFromName(thisID);
457 if ((*np) == 0)
return FALSE;
460 printf (
"np is %p\n", *np);
461 printf (
"and, found node %p type %s\n",*np, stringNodeType((*np)->_nodeType));
466 if (direction == 0) *fp =
"valueChanged";
else *fp =
"setValue";
487 void EAI_GetType (
int cNode,
char *inputFieldString,
char *accessMethod,
488 int *cNodePtr,
int *fieldOffset,
489 int *dataLen,
int *typeString,
int *scripttype,
int *accessType) {
493 struct X3D_Node* nodePtr = getEAINodeFromTable(cNode,-1);
494 char *fieldString = inputFieldString;
498 char *invokedValPtr = NULL;
499 int myScriptType = EAI_NODETYPE_STANDARD;
502 int isProtoExpansion = FALSE;
506 eaiverbose = tg->EAI_C_CommonFunctions.eaiverbose;
509 eaiverbose = gglobal()->EAI_C_CommonFunctions.eaiverbose;
513 if (strncmp(accessMethod,
"in",strlen(
"in")) == 0) direction=1;
514 else if (strncmp(accessMethod,
"eventIn",strlen(
"eventIn"))==0) direction=1;
524 printf (
"call to EAI_GetType, cNode %d fieldString :%s: accessMethod %s\n",cNode,fieldString,accessMethod);
528 if ((cNode == 0) || (cNode > vectorSize(p->EAINodeIndex) )) {
529 printf (
"THIS IS AN ERROR! CNode is zero!!!\n");
530 *cNodePtr = 0; *fieldOffset = 0; *dataLen = 0; *typeString = 0; *scripttype=0; *accessType=KW_eventIn;
535 printf (
"start of EAI_GetType, this is a valid C node %p\n",nodePtr);
536 printf (
" of string type %s\n",stringNodeType(nodePtr->_nodeType));
558 isProtoExpansion = TRUE;
562 if (isProtoExpansion) {
564 if (!changeExpandedPROTOtoActualNode (cNode, &nodePtr, &fieldString,direction)) {
565 ConsoleMessage (
"Did NOT find field :%s: in PROTO expansion",fieldString);
568 if (eaiverbose) printf (
"EAI_GetType - no, this is NOT a proto node\n");
571 if (nodePtr == NULL) {
572 if (isProtoExpansion)
573 ConsoleMessage (
"error looking up field :%s: a PROTO Definition\n", fieldString);
575 ConsoleMessage (
"error looking up field :%s: in an unknown node\n", fieldString);
580 printf (
"node here is %p\n",nodePtr);
581 printf (
"ok, going to try and find field :%s: in a node of type :%s:\n",fieldString,stringNodeType(nodePtr->_nodeType));
585 myField = findRoutedFieldInFIELDNAMES(nodePtr,fieldString,direction);
587 if (eaiverbose) printf (
"EAI_GetType, for field %s, myField is %d\n",fieldString,myField);
590 findFieldInOFFSETS(nodePtr->_nodeType, myField, &myFieldOffs, &ctype, accessType);
593 printf (
"EAI_GetType, after changeExpandedPROTOtoActualNode, C node %p\n",nodePtr);
594 printf (
" of string type %s\n",stringNodeType(nodePtr->_nodeType));
598 if (eaiverbose) printf (
"EAI_GetType, after findFieldInOFFSETS, have myFieldOffs %u, ctype %d, accessType %d \n",myFieldOffs, ctype, *accessType);
601 if (myFieldOffs <= 0) {
605 if (nodePtr->_nodeType == NODE_Script) {
610 printf (
"EAI_GetType, node is a Script node...\n");
611 myScript = X3D_SCRIPT(nodePtr)->__scriptObj;
612 myScriptType = EAI_NODETYPE_SCRIPT;
614 for (i = 0; i != vectorSize(myScript->fields); ++i) {
618 printf (
" field %d, name %s type %s (type %s accessType %d (%s), indexName %d, stringType %s)\n",
620 fieldDecl_getShaderScriptName(sfield->fieldDecl),
621 stringFieldtypeType(fieldDecl_getType(sfield->fieldDecl)),
622 stringFieldtypeType(fieldDecl_getType(sfield->fieldDecl)),
623 fieldDecl_getAccessType(sfield->fieldDecl),
624 stringPROTOKeywordType(fieldDecl_getAccessType(sfield->fieldDecl)),
625 fieldDecl_getIndexName(sfield->fieldDecl),
626 fieldDecl_getStringName(globalParser->lexer,sfield->fieldDecl)
630 if (strcmp(fieldString,fieldDecl_getShaderScriptName(sfield->fieldDecl)) == 0) {
634 printf (
"found it at index, %d but returning JSparamIndex %d\n",i,
635 fieldDecl_getShaderScriptIndex(sfield->fieldDecl));
637 myFieldOffs = fieldDecl_getShaderScriptIndex(sfield->fieldDecl);
639 *accessType = mapToKEYWORDindex(fieldDecl_getAccessType(sfield->fieldDecl));
640 ctype = fieldDecl_getType(sfield->fieldDecl);
647 printf (
"EAI_GetType, warning: field :%s: not not found in node of type :%s:\n",fieldString,stringNodeType(nodePtr->_nodeType));
649 printf (
"EAI_GetType, warning: field :%s: not not found in node with pointer of NULL\n",fieldString);
657 if (me->nodeParams == NULL) {
659 if (eaiverbose) printf (
"creating new field vector for this node\n");
666 newp->fieldOffset = myFieldOffs;
667 newp->datalen = ctype;
668 newp->typeString = mapFieldTypeToEAItype(ctype);
669 newp->scripttype = myScriptType;
670 newp->invokedPROTOValue = invokedValPtr;
673 if (me->actualNodePtr != nodePtr) {
675 newp->thisFieldNodePointer= nodePtr;
678 newp->thisFieldNodePointer= me->actualNodePtr;
681 vector_pushBack(
struct EAINodeParams *, me->nodeParams, newp);
688 *fieldOffset = vectorSize(me->nodeParams)-1;
689 *dataLen = (int) newp->datalen;
690 *typeString = newp->typeString;
691 *scripttype =newp->scripttype;
696 char *EAI_GetTypeName (
unsigned int uretval) {
697 printf (
"HELP::EAI_GetTypeName %d\n",uretval);
698 return "unknownType";
702 int SAI_IntRetCommand (
char cmnd,
const char *fn) {
703 printf (
"HELP::SAI_IntRetCommand, %c, %s\n",cmnd,fn);
707 char * SAI_StrRetCommand (
char cmnd,
const char *fn) {
708 printf (
"HELP::SAI_StrRetCommand, %c, %s\n",cmnd,fn);
709 return "iunknownreturn";
713 struct X3D_Node *EAI_GetViewpoint(
const char *str) {
720 printf (
"X3DParser_getNodeFromName not here yet\n");
722 myNode = X3DParser_getNodeFromName(str);
724 if (myNode == NULL) {
726 myNode = parser_getNodeFromName(str);
735 void handleEAIGetValue (
char command,
char *bufptr,
int repno) {
737 int nodeIndex, fieldIndex, length;
748 eaiverbose = gglobal()->EAI_C_CommonFunctions.eaiverbose;
751 eaiverbose = gglobal()->EAI_C_CommonFunctions.eaiverbose;
753 if (eaiverbose) printf (
"GETVALUE %s \n",bufptr);
756 retint=sscanf (bufptr,
"%d %d %c %d", &nodeIndex,&fieldIndex,ctmp,&length);
757 myNode = getEAINodeFromTable(nodeIndex, fieldIndex);
760 if (myNode == NULL) {
761 printf (
"handleEAIGetValue - node does not exist!\n");
767 printf (
"handleEAIGetValue - node does not exist in vector!\n");
779 myParam = vector_get(
struct EAINodeParams *, me->nodeParams, fieldIndex);
781 if (myParam == NULL) {
782 printf (
"bad field in handleEAIGetValue\n");
786 if (myParam->invokedPROTOValue != NULL) {
787 sprintf (tg->EAIHelpers.outBuffer,
"RE\n%f\n%d\n%s",TickTime(),repno,getEAIInvokedValue(nodeIndex,fieldIndex));
789 EAI_Convert_mem_to_ASCII (repno,
"RE",mapEAItypeToFieldType(ctmp[0]),getEAIMemoryPointer(nodeIndex,fieldIndex), tg->EAIHelpers.outBuffer);
795 char *eaiPrintCommand (
char command) {
799 case GETNODE:
return (
"GETNODE");
800 case GETEAINODETYPE:
return (
"GETEAINODETYPE");
801 case SENDCHILD:
return (
"SENDCHILD");
802 case SENDEVENT:
return (
"SENDEVENT");
803 case GETVALUE:
return (
"GETVALUE");
804 case GETFIELDTYPE:
return (
"GETFIELDTYPE");
805 case REGLISTENER:
return (
"REGLISTENER");
806 case ADDROUTE:
return (
"ADDROUTE");
807 case REREADWRL:
return (
"REREADWRL");
808 case DELETEROUTE:
return (
"DELETEROUTE");
809 case GETNAME:
return (
"GETNAME");
810 case GETVERSION:
return (
"GETVERSION");
811 case GETCURSPEED:
return (
"GETCURSPEED");
812 case GETFRAMERATE:
return (
"GETFRAMERATE");
813 case GETURL:
return (
"GETURL");
814 case REPLACEWORLD:
return (
"REPLACEWORLD");
815 case LOADURL:
return (
"LOADURL");
816 case VIEWPOINT:
return (
"VIEWPOINT");
817 case CREATEVS:
return (
"CREATEVS");
818 case CREATEVU:
return (
"CREATEVU");
819 case STOPFREEWRL:
return (
"STOPFREEWRL");
820 case UNREGLISTENER:
return (
"UNREGLISTENER");
821 case GETRENDPROP:
return (
"GETRENDPROP");
822 case GETENCODING:
return (
"GETENCODING");
823 case CREATENODE:
return (
"CREATENODE");
824 case CREATEPROTO:
return (
"CREATEPROTO");
825 case UPDNAMEDNODE:
return (
"UPDNAMEDNODE");
826 case REMNAMEDNODE:
return (
"REMNAMEDNODE");
827 case GETPROTODECL:
return (
"GETPROTODECL");
828 case UPDPROTODECL:
return (
"UPDPROTODECL");
829 case REMPROTODECL:
return (
"REMPROTODECL");
830 case GETFIELDDEFS:
return (
"GETFIELDDEFS");
831 case GETNODEDEFNAME:
return (
"GETNODEDEFNAME");
832 case GETROUTES:
return (
"GETROUTES");
833 case GETNODETYPE:
return (
"GETNODETYPE");
836 return "unknown command...";
841 void outBufferCat (
char *str) {
843 struct tEAIHelpers* t = &gglobal()->EAIHelpers;
844 a = (int) strlen (t->outBuffer);
845 b = (int) strlen (str);
848 if ((a+b+2) >= t->outBufferLen) {
849 t->outBufferLen = a+b+200;
851 t->outBuffer = REALLOC(t->outBuffer, t->outBufferLen);
853 strcat (t->outBuffer, str);
860 int* getSwitchNodeFromTable(
int cNode) {
867 return offsetPointer_deref (
int *, me->actualNodePtr, offsetof (
struct X3D_Switch, whichChoice));
871 void confidenceConeSet(
int coneNode,
int shown,
int found) {
879 #define CONE_MOVE_HEIGHT 14.0
883 if (coneNode <=0)
return;
886 trans = X3D_TRANSFORM(me->actualNodePtr);
888 if (trans->_nodeType != NODE_Transform) {
889 ConsoleMessage (
"confidence - not a Transform");
894 ConsoleMessage (
"confidence - nothing found, nothing to do");
899 translation = offsetPointer_deref (
struct SFVec3f*, trans, offsetof (
struct X3D_Transform, translation));
901 translation->c[1] = (float)shown/(
float)found*CONE_MOVE_HEIGHT - (CONE_MOVE_HEIGHT/2);
909 void textureToMultiTexture(
int texNode,
int MultiTexture,
int add) {
921 if (add) addFlag=1;
else addFlag=2;
925 sprintf (line,
"textureToMultiTexture, texnode %d multitexture %d add %d",texNode,MultiTexture,add);
926 ConsoleMessage(line);
929 if ((texNode <=0) || (MultiTexture<=0)) {
930 sprintf (line,
"textureToMultiTexture, can not do: texnode %d multitexture %d add %d",texNode,MultiTexture,add);
931 ConsoleMessage (line);
936 MultiTexNode = me->actualNodePtr;
938 myTex = me->actualNodePtr;
940 sprintf (line,
"textureToMultitexture, adding a :%s: to a :%s:",stringNodeType(myTex->_nodeType),
941 stringNodeType(MultiTexNode->_nodeType));
942 ConsoleMessage(line);
944 if ((myTex->_nodeType!=NODE_PixelTexture) && (MultiTexNode->_nodeType != NODE_MultiTexture)) {
945 ConsoleMessage (
"textureToMultitexture, wrong type(s)");
953 for (count=0; count<texture->n; count++) {
954 if (texture->p[count] == myTex)
return;
959 AddRemoveChildren (X3D_NODE(MultiTexNode),
960 offsetPointer_deref(
void *, MultiTexNode, offsetof (
struct X3D_MultiTexture, texture)),
961 (
struct X3D_Node * *)&myTex,1,addFlag,__FILE__,__LINE__);