From: Andy D. <kha...@us...> - 2005-09-08 16:27:18
|
Update of /cvsroot/planeshift/planeshift/src/npcclient In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1743/src/npcclient Modified Files: networkmgr.cpp npc.cpp npcbehave.cpp networkmgr.h npc.h npcbehave.h Log Message: Added initial artificial life implementation, NPCs can now talk or emote and can reproduce, currently implemented as a dynamic spawn point.Fixed uninitialized rot variable that was causing NPCs to walk into walls. Added provision of a single dynamic spawn point added for a psCharacter when requested by NPC client. Index: networkmgr.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/networkmgr.cpp,v retrieving revision 1.82 retrieving revision 1.83 diff -C2 -d -r1.82 -r1.83 *** networkmgr.cpp 6 Sep 2005 12:25:22 -0000 1.82 --- networkmgr.cpp 8 Sep 2005 16:27:05 -0000 1.83 *************** *** 695,698 **** --- 695,716 ---- } + void NetworkManager::QueueSpawnCommand(iCelEntity *mother, iCelEntity *father) + { + outbound->msg->Add( (int8_t) psNPCCommandsMessage::CMD_SPAWN); + outbound->msg->Add( (uint32_t) mother->GetID() ); + outbound->msg->Add( (uint32_t) father->GetID() ); + cmd_count++; + } + + void NetworkManager::QueueTalkCommand(iCelEntity *speaker, const char* text) + { + outbound->msg->Add( (int8_t) psNPCCommandsMessage::CMD_TALK); + outbound->msg->Add( (uint32_t) speaker->GetID() ); + + outbound->msg->Add(text); + + cmd_count++; + } + void NetworkManager::SendAllCommands() { Index: npc.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npc.cpp,v retrieving revision 1.62 retrieving revision 1.63 diff -C2 -d -r1.62 -r1.63 *** npc.cpp 31 Jul 2005 16:47:05 -0000 1.62 --- npc.cpp 8 Sep 2005 16:27:05 -0000 1.63 *************** *** 30,33 **** --- 30,35 ---- #include <iengine/mesh.h> #include <iengine/movable.h> + #include <ivaria/collider.h> + #include <cstool/collider.h> #include "net/msghandler.h" *************** *** 221,224 **** --- 223,227 ---- iSector* sector; float rot,min_range; + target_id = (uint32_t)-1; psGameObject::GetPosition(entity,loc,rot,sector); *************** *** 231,234 **** --- 234,239 ---- { iCelEntity *ent = nearlist->Get(i); + if(ent == entity) + continue; csVector3 loc2; iSector *sector2; *************** *** 246,253 **** } } ! else { ! target_id = (uint32_t)-1; } } --- 251,301 ---- } } ! } ! ! iCelEntity* NPC::GetNearestVisibleFriend(float range) ! { ! csVector3 loc; ! iSector* sector; ! float rot,min_range; ! iCelEntity *friendEnt = NULL; ! ! psGameObject::GetPosition(entity,loc,rot,sector); ! ! csRef<iCelEntityList> nearlist = npcclient->GetPlLayer()->FindNearbyEntities(sector,loc,range); ! if (nearlist) { ! min_range=range; ! for (size_t i=0; i<nearlist->GetCount(); i++) ! { ! iCelEntity *ent = nearlist->Get(i); ! NPC* npcFriend = npcclient->FindAttachedNPC(ent); ! ! if (!npcFriend || npcFriend == this) ! continue; ! ! csVector3 loc2, isect; ! iSector *sector2; ! float rot2; ! psGameObject::GetPosition(ent,loc2,rot2,sector2); ! ! float dist = (loc2 - loc).Norm(); ! if(min_range < dist) ! continue; ! ! // Is this friend visible? ! csIntersectingTriangle closest_tri; ! iMeshWrapper* sel = 0; ! ! dist = csColliderHelper::TraceBeam (npcclient->GetCollDetSys(), sector, ! loc + csVector3(0, 0.6f, 0), loc2 + csVector3(0, 0.6f, 0), true, closest_tri, isect, &sel); ! // Not visible ! if (dist > 0) ! continue; ! ! min_range = (loc2 - loc).Norm(); ! friendEnt = ent; ! } } + return friendEnt; } Index: npcbehave.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npcbehave.cpp,v retrieving revision 1.102 retrieving revision 1.103 diff -C2 -d -r1.102 -r1.103 *** npcbehave.cpp 5 Sep 2005 22:10:41 -0000 1.102 --- npcbehave.cpp 8 Sep 2005 16:27:05 -0000 1.103 *************** *** 528,531 **** --- 528,553 ---- sequence.Push(op2); } + else if ( strcmp( node->GetValue(), "talk" ) == 0 ) + { + TalkOperation *op = new TalkOperation; + if (!op->Load(node)) + { + Error1("Could not load TalkOperation. Error in XML"); + delete op; + return false; + } + sequence.Push(op); + } + else if ( strcmp( node->GetValue(), "reproduce" ) == 0 ) + { + ReproduceOperation *op = new ReproduceOperation; + if (!op->Load(node)) + { + Error1("Could not load ReproduceOperation. Error in XML"); + delete op; + return false; + } + sequence.Push(op); + } else { *************** *** 830,834 **** void ScriptOperation::TurnTo(NPC *npc,csVector3& dest, csVector3& forward) { ! CPrintf(CON_SPAM, "TurnTo localDest=(%1.2f,%1.2f,%1.2f)\n",dest.x,dest.y,dest.z); csRef<iPcMesh> pcmesh = CEL_QUERY_PROPCLASS(npc->GetEntity()->GetPropertyClassList(), iPcMesh); --- 852,856 ---- void ScriptOperation::TurnTo(NPC *npc,csVector3& dest, csVector3& forward) { ! npc->Printf("TurnTo localDest=(%1.2f,%1.2f,%1.2f)\n",dest.x,dest.y,dest.z); csRef<iPcMesh> pcmesh = CEL_QUERY_PROPCLASS(npc->GetEntity()->GetPropertyClassList(), iPcMesh); *************** *** 919,923 **** npc->GetLinMove()->GetLastPosition(pos,rot,sector); ! CPrintf(CON_SPAM, "advance: pos=(%f,%f,%f) localDest=(%f,%f,%f) dist=%f\n", pos.x,pos.y,pos.z, localDest.x,localDest.y,localDest.z, psGameObject::Distance(localDest, pos)); TurnTo(npc, localDest, forward); --- 941,945 ---- npc->GetLinMove()->GetLastPosition(pos,rot,sector); ! npc->Printf("advance: pos=(%f,%f,%f) localDest=(%f,%f,%f) dist=%f\n", pos.x,pos.y,pos.z, localDest.x,localDest.y,localDest.z, psGameObject::Distance(localDest, pos)); TurnTo(npc, localDest, forward); *************** *** 1048,1052 **** bool verified = false; int count=0; ! float rot,rot_angle=0; while (rgn && op_type == ROT_REGION && !verified && count<10) { --- 1070,1074 ---- bool verified = false; int count=0; ! float rot=0,rot_angle=0; while (rgn && op_type == ROT_REGION && !verified && count<10) { *************** *** 1307,1310 **** --- 1329,1335 ---- npc->Printf(">>>LocateOp "); + // Reset old target + npc->SetTarget(NULL); + if (object == "perception") { *************** *** 1354,1357 **** --- 1379,1397 ---- located_angle = 0; } + else if(object == "friend") + { + iCelEntity *ent = npc->GetNearestVisibleFriend(20); + if(ent) + npc->SetTarget(ent); + else + return true; + + float rot; + iSector *sector; + csVector3 pos; + psGameObject::GetPosition(ent,pos,rot,sector); + located_pos = pos; + located_angle = 0; + } else if (!static_loc || !located) { *************** *** 1751,1755 **** ! CPrintf(CON_DEBUG, "advance: pos=(%f,%f,%f) localDest=(%f,%f,%f) dist=%f\n", myPos.x,myPos.y,myPos.z, localDest.x,localDest.y,localDest.z, psGameObject::Distance(localDest, myPos)); npc->GetLinMove()->ExtrapolatePosition(timedelta); --- 1791,1795 ---- ! npc->Printf("advance: pos=(%f,%f,%f) localDest=(%f,%f,%f) dist=%f\n", myPos.x,myPos.y,myPos.z, localDest.x,localDest.y,localDest.z, psGameObject::Distance(localDest, myPos)); npc->GetLinMove()->ExtrapolatePosition(timedelta); *************** *** 1814,1818 **** --- 1854,1918 ---- } + bool TalkOperation::Load(iDocumentNode *node) + { + text = node->GetAttributeValue("text"); + target = node->GetAttributeValueAsBool("target"); + command = node->GetAttributeValue("command"); + + return true; + } + + ScriptOperation *TalkOperation::MakeCopy() + { + TalkOperation *op = new TalkOperation; + op->text = text; + op->target = target; + op->command = command; + return op; + } + + bool TalkOperation::Run(NPC *npc,EventManager *eventmgr) + { + + if(!target) + { + npcclient->GetNetworkMgr()->QueueTalkCommand(npc->GetEntity(), npc->GetName() + text); + return true; + } + + if(!npc->GetTarget()) + return true; + + NPC* friendNPC = npcclient->FindAttachedNPC(npc->GetTarget()); + if(friendNPC) + { + npcclient->GetNetworkMgr()->QueueTalkCommand(npc->GetEntity(), npc->GetName() + text); + + Perception collision("friend:" + command); + friendNPC->TriggerEvent(&collision,eventmgr); + } + return true; + } + + bool ReproduceOperation::Load(iDocumentNode *node) + { + return true; + } + ScriptOperation *ReproduceOperation::MakeCopy() + { + ReproduceOperation *op = new ReproduceOperation; + return op; + } + + bool ReproduceOperation::Run(NPC *npc,EventManager *eventmgr) + { + if(!npc->GetTarget()) + return true; + NPC * friendNPC = npcclient->FindAttachedNPC(npc->GetTarget()); + if(friendNPC) + npcclient->GetNetworkMgr()->QueueSpawnCommand(npc->GetEntity(), npc->GetTarget()); + return true; + } bool MeleeOperation::Load(iDocumentNode *node) Index: networkmgr.h =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/networkmgr.h,v retrieving revision 1.34 retrieving revision 1.35 diff -C2 -d -r1.34 -r1.35 *** networkmgr.h 17 Jun 2005 14:09:55 -0000 1.34 --- networkmgr.h 8 Sep 2005 16:27:05 -0000 1.35 *************** *** 80,83 **** --- 80,85 ---- void QueueDRData(iCelEntity *entity,iPcLinearMovement *linmove); void QueueAttackCommand(iCelEntity *attacker, iCelEntity *target); + void QueueSpawnCommand(iCelEntity *mother, iCelEntity *father); + void QueueTalkCommand(iCelEntity *speaker, const char* text); void SendAllCommands(); Index: npc.h =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npc.h,v retrieving revision 1.43 retrieving revision 1.44 diff -C2 -d -r1.43 -r1.44 *** npc.h 17 Jun 2005 14:09:55 -0000 1.43 --- npc.h 8 Sep 2005 16:27:05 -0000 1.44 *************** *** 149,152 **** --- 149,154 ---- void GetNearestEntity(uint32_t& target_id,csVector3& dest,csStringBase& name,float range); + iCelEntity * GetNearestVisibleFriend(float range); + void Printf(const char *msg,...); Index: npcbehave.h =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npcbehave.h,v retrieving revision 1.52 retrieving revision 1.53 diff -C2 -d -r1.52 -r1.53 *** npcbehave.h 6 Jul 2005 21:19:15 -0000 1.52 --- npcbehave.h 8 Sep 2005 16:27:05 -0000 1.53 *************** *** 226,232 **** --- 226,234 ---- /// This function is used by MoveTo AND Navigate operations int StartMoveTo(NPC *npc,EventManager *eventmgr,csVector3& dest,float vel,const char *action, bool autoresume=true); + void TurnTo(NPC *npc,csVector3& dest,csVector3& forward); public: + virtual bool Run(NPC *npc,EventManager *eventmgr)=0; virtual void Advance(float timedelta,NPC *npc,EventManager *eventmgr); *************** *** 502,505 **** --- 504,543 ---- /** + * Talk will tell the npc to communicate to a nearby + * entity. + */ + class TalkOperation : public ScriptOperation + { + protected: + csString text; + csString command; + bool target; + + public: + + TalkOperation() {}; + virtual ~TalkOperation() {}; + virtual bool Run(NPC *npc,EventManager *eventmgr); + virtual bool Load(iDocumentNode *node); + virtual ScriptOperation *MakeCopy(); + }; + + /** + * Reproduce will make the npc to setup a spawn point here + */ + class ReproduceOperation : public ScriptOperation + { + protected: + + public: + + ReproduceOperation() {}; + virtual ~ReproduceOperation() {}; + virtual bool Run(NPC *npc,EventManager *eventmgr); + virtual bool Load(iDocumentNode *node); + virtual ScriptOperation *MakeCopy(); + }; + + /** * BeginLoop will initialize data values and * will check the range and jump to the end |