Update of /cvsroot/planeshift/planeshift/src/npcclient In directory sc8-pr-cvs1:/tmp/cvs-serv21329 Modified Files: command.cpp networkmgr.cpp npc.cpp npc.h npcbehave.cpp npcbehave.h npcclient.cpp npcclient.h perceptions.cpp Log Message: NPC now attempts to fight back when attacked. Perceptions of attack and direct damage are both implemented and hate lists are maintained for each npc. NPC will attack whoever attacks it if it detects it within its combat range--this does not always work and I am currently investigating why not. Index: command.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/command.cpp,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** command.cpp 17 Sep 2003 21:46:24 -0000 1.9 --- command.cpp 20 Sep 2003 07:39:25 -0000 1.10 *************** *** 26,38 **** #include "npcclient.h" - //#include "psserver.h" #include "util/strutil.h" - //#include "EntityManager.h" - //#include "engine/GEMSupervisor.h" - //#include "psdatabase.h" - //#include "spawnmanager.h" - //#include "psprop/pc/pspcproxlist.h" - //#include "admin/adminmessage.h" - //#include "adminmanager.h" /* shut down the server and exit program */ --- 26,30 ---- *************** *** 48,52 **** int com_list(char *arg) { ! npcclient->ListNPC(arg); return 0; } --- 40,50 ---- int com_list(char *arg) { ! npcclient->ListAllNPCs(arg); ! return 0; ! } ! ! int com_entlist(char *arg) ! { ! npcclient->ListAllEntities(); return 0; } *************** *** 100,106 **** /* add all new commands here */ COMMAND commands[] = { ! { "help", com_help, "Show help information" }, ! { "list", com_list, "List all NPCs"}, ! { "quit", com_quit, "Makes the npc client exit"}, { 0, 0, 0 } }; --- 98,105 ---- /* add all new commands here */ COMMAND commands[] = { ! { "help", com_help, "Show help information" }, ! { "npclist", com_list, "List all NPCs (npclist [pattern])"}, ! { "entlist", com_entlist, "List all known entities"}, ! { "quit", com_quit, "Makes the npc client exit"}, { 0, 0, 0 } }; Index: networkmgr.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/networkmgr.cpp,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** networkmgr.cpp 17 Sep 2003 18:08:20 -0000 1.16 --- networkmgr.cpp 20 Sep 2003 07:39:25 -0000 1.17 *************** *** 355,358 **** --- 355,360 ---- void NetworkManager::QueueAttackCommand(iCelEntity *attacker, iCelEntity *target) { + printf("%s is attacking %s\n",attacker->GetName(),target->GetName() ); + outbound->msg->Add( (int8_t) psNPCCommandsMessage::CMD_ATTACK); outbound->msg->Add( (uint32_t) netpersist->GetMappedID(attacker) ); Index: npc.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npc.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** npc.cpp 17 Sep 2003 18:08:20 -0000 1.8 --- npc.cpp 20 Sep 2003 07:39:25 -0000 1.9 *************** *** 65,68 **** --- 65,73 ---- vel = node->GetAttributeValueAsFloat("vel"); + if (node->GetAttributeValue("debug")) + is_debugging = true; + else + is_debugging = false; + brain = new NPCType(*t); // deep copy constructor *************** *** 91,95 **** void NPC::TriggerEvent(Perception *pcpt,EventManager *eventmgr) { ! printf("Got event '%s'.\n",pcpt->GetName() ); brain->FirePerception(this,eventmgr,pcpt); } --- 96,100 ---- void NPC::TriggerEvent(Perception *pcpt,EventManager *eventmgr) { ! Printf("Got event '%s'.\n",pcpt->GetName() ); brain->FirePerception(this,eventmgr,pcpt); } *************** *** 170,173 **** --- 175,192 ---- } + void NPC::Printf(const char *msg,...) + { + if (!is_debugging) + return; + + char str[1024]; + va_list args; + + va_start(args, msg); + vsprintf(str, msg, args); + va_end(args); + + printf("%s> %s\n",entity->GetName(),str); + } Index: npc.h =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npc.h,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** npc.h 17 Sep 2003 18:19:32 -0000 1.12 --- npc.h 20 Sep 2003 07:39:25 -0000 1.13 *************** *** 70,76 **** Perception *last_perception; HateList hatelist; public: ! NPC() { last_perception=NULL; region=NULL; last_update=0; brain=NULL; entity=NULL; id=0; ang_vel=vel=999; } ~NPC() { if (brain) delete brain; } --- 70,77 ---- Perception *last_perception; HateList hatelist; + bool is_debugging; public: ! NPC() { is_debugging=false; last_perception=NULL; region=NULL; last_update=0; brain=NULL; entity=NULL; id=0; ang_vel=vel=999; } ~NPC() { if (brain) delete brain; } *************** *** 121,124 **** --- 122,127 ---- void GetNearestEntity(iCelEntity *& target_entity,csVector3& dest,csString& name,float range); + + void Printf(const char *msg,...); }; Index: npcbehave.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npcbehave.cpp,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** npcbehave.cpp 17 Sep 2003 18:08:20 -0000 1.24 --- npcbehave.cpp 20 Sep 2003 07:39:25 -0000 1.25 *************** *** 172,177 **** --- 172,179 ---- next = ++iter; // save next position to restore next time through loop behaviors.Delete(b); // Remove and reinsert after changing key value + b->CommitAdvance(); // Update key to correct value behaviors.Add(b); // Now reinsert + if (next) iter.Find(next); // Find rebuilds the iterator, so it doesn't matter if tree structure has changed *************** *** 202,209 **** if (b->CurrentNeed() > active->CurrentNeed() + 20) { ! printf("%s switching behavior from '%s' to '%s'.\n", ! npc->GetEntity()->GetName(), ! active->GetName(), ! b->GetName() ); active->InterruptScript(npc,eventmgr); active->SetActive(false); --- 204,212 ---- if (b->CurrentNeed() > active->CurrentNeed() + 20) { ! npc->Printf("%s switching behavior from '%s' to '%s'.\n", ! npc->GetEntity()->GetName(), ! active->GetName(), ! b->GetName() ); ! npc->DumpBehaviorList(); active->InterruptScript(npc,eventmgr); active->SetActive(false); *************** *** 477,486 **** void Behavior::Advance(csTicks delta,NPC *npc,EventManager *eventmgr) { ! CommitAdvance(); // if new-need has been modified, be sure to roll that in here float d = .001 * delta; if (is_active) { ! new_need = current_need - (d * need_decay_rate); if (current_step < sequence.Length()) sequence[current_step]->Advance(d,npc,eventmgr); --- 480,492 ---- void Behavior::Advance(csTicks delta,NPC *npc,EventManager *eventmgr) { ! // npc->Printf("Advancing %s need of %1.1f/%1.1f",name.GetData(),new_need,current_need); ! ! if (new_need == -999) ! new_need = current_need; float d = .001 * delta; if (is_active) { ! new_need = new_need - (d * need_decay_rate); if (current_step < sequence.Length()) sequence[current_step]->Advance(d,npc,eventmgr); *************** *** 488,492 **** else { ! new_need = current_need + (d * need_growth_rate); } } --- 494,498 ---- else { ! new_need = new_need + (d * need_growth_rate); } } *************** *** 516,520 **** if (completion_decay) { ! printf("Subtracting completion decay of %1f from behavior '%s'.\n",completion_decay,GetName() ); new_need = current_need - completion_decay; } --- 522,526 ---- if (completion_decay) { ! npc->Printf("Subtracting completion decay of %1f from behavior '%s'.\n",completion_decay,GetName() ); new_need = current_need - completion_decay; } *************** *** 548,551 **** --- 554,562 ---- } + void ScriptOperation::Advance(float timedelta,NPC *npc,EventManager *eventmgr) + { + npc->Printf("AnonOp advanced"); + } + bool MoveOperation::Load(iDocumentNode *node) { *************** *** 566,575 **** bool MoveOperation::Run(NPC *npc,EventManager *eventmgr) { ! printf("MoveOp "); LocationType *rgn = npc->GetRegion(); if (!rgn) { ! printf("Region was not specified or found for npc %s.\n", npc->GetEntity()->GetName()); return false; // this halts this script --- 577,586 ---- bool MoveOperation::Run(NPC *npc,EventManager *eventmgr) { ! npc->Printf("MoveOp "); LocationType *rgn = npc->GetRegion(); if (!rgn) { ! printf("ERROR: Region was not specified or found for npc %s.\n", npc->GetEntity()->GetName()); return false; // this halts this script *************** *** 653,657 **** { consec_collisions++; ! printf("Bang (%1.2f,%1.2f)...",diffx,diffz); if (consec_collisions > 8) // allow for hitting trees but not walls { --- 664,668 ---- { consec_collisions++; ! npc->Printf("Bang (%1.2f,%1.2f)...",diffx,diffz); if (consec_collisions > 8) // allow for hitting trees but not walls { *************** *** 868,872 **** bool RotateOperation::Run(NPC *npc,EventManager *eventmgr) { ! printf("RotateOp "); if (op_type == this->ROT_RANDOM) --- 879,883 ---- bool RotateOperation::Run(NPC *npc,EventManager *eventmgr) { ! npc->Printf("RotateOp "); if (op_type == this->ROT_RANDOM) *************** *** 911,915 **** // Pick an angle in that range radians = rng.Get() * (max_angle-min_angle) + min_angle; ! printf("Turning from %1.3f to %1.3f.\n",rot,radians); radians -= rot; // now radians is delta --- 922,926 ---- // Pick an angle in that range radians = rng.Get() * (max_angle-min_angle) + min_angle; ! npc->Printf("Turning from %1.3f to %1.3f.\n",rot,radians); radians -= rot; // now radians is delta *************** *** 931,935 **** radians = rng.Get() * TWO_PI - 3.14159; msec = (int)fabs(1000.0*radians/GetAngularVelocity(npc) ); ! printf("%1.2f degrees in %d msec.\n",radians*360/TWO_PI,msec); } --- 942,946 ---- radians = rng.Get() * TWO_PI - 3.14159; msec = (int)fabs(1000.0*radians/GetAngularVelocity(npc) ); ! npc->Printf("%1.2f degrees in %d msec.\n",radians*360/TWO_PI,msec); } *************** *** 938,942 **** // clamp values psGameObject::ClampRadians(target_angle); ! printf("End angle should be %1.3f, or %1.3f\n",rot+radians,target_angle); linmove->SetRotation( csVector3(0,(radians>0)?-ang_vel:ang_vel,0) ); --- 949,953 ---- // clamp values psGameObject::ClampRadians(target_angle); ! npc->Printf("End angle should be %1.3f, or %1.3f\n",rot+radians,target_angle); linmove->SetRotation( csVector3(0,(radians>0)?-ang_vel:ang_vel,0) ); *************** *** 971,975 **** ang_vel = GetAngularVelocity(npc); ! printf("Turning from rot %1.2f to angle %1.2f at %1.2f rad/sec...",rot,target_angle,ang_vel); float angle = target_angle-rot; --- 982,986 ---- ang_vel = GetAngularVelocity(npc); ! npc->Printf("Turning from rot %1.2f to angle %1.2f at %1.2f rad/sec...",rot,target_angle,ang_vel); float angle = target_angle-rot; *************** *** 999,1003 **** angle_delta=target_angle; ! printf("Rotating %1.2f in %d msec.\n",angle,msec); return false; --- 1010,1014 ---- angle_delta=target_angle; ! npc->Printf("Rotating %1.2f in %d msec.\n",angle,msec); return false; *************** *** 1056,1060 **** bool LocateOperation::Run(NPC *npc,EventManager *eventmgr) { ! printf("LocateOp "); if (object == "perception") --- 1067,1071 ---- bool LocateOperation::Run(NPC *npc,EventManager *eventmgr) { ! npc->Printf("LocateOp "); if (object == "perception") *************** *** 1075,1079 **** if (!npcclient->Locate(object,located_pos,located_angle,range)) { ! printf("Couldn't locate any <%s> in npc script for <%s>.\n", (const char *)object,npc->GetEntity()->GetName() ); return true; --- 1086,1090 ---- if (!npcclient->Locate(object,located_pos,located_angle,range)) { ! npc->Printf("Couldn't locate any <%s> in npc script for <%s>.\n", (const char *)object,npc->GetEntity()->GetName() ); return true; *************** *** 1113,1117 **** npc->GetActiveLocate(dest,rot); ! printf("(%1.2f,%1.2f,%1.2f) at %1.2f m/sec.\n",dest.x,dest.y,dest.z,vel); StartMoveTo(npc,eventmgr,dest,GetVelocity(npc),action); --- 1124,1128 ---- npc->GetActiveLocate(dest,rot); ! npc->Printf("(%1.2f,%1.2f,%1.2f) at %1.2f m/sec.\n",dest.x,dest.y,dest.z,vel); StartMoveTo(npc,eventmgr,dest,GetVelocity(npc),action); *************** *** 1172,1176 **** bool ChaseOperation::Run(NPC *npc,EventManager *eventmgr) { ! printf("NavigateOp "); csVector3 dest; --- 1183,1187 ---- bool ChaseOperation::Run(NPC *npc,EventManager *eventmgr) { ! npc->Printf("NavigateOp "); csVector3 dest; *************** *** 1178,1182 **** npc->GetNearestEntity(target_entity,dest,name,range); ! printf("Started chasing %s at (%1.2f,%1.2f,%1.2f)...\n",(const char *)name,dest.x,dest.y,dest.z); if (target_entity) --- 1189,1193 ---- npc->GetNearestEntity(target_entity,dest,name,range); ! npc->Printf("Started chasing %s at (%1.2f,%1.2f,%1.2f)...\n",(const char *)name,dest.x,dest.y,dest.z); if (target_entity) *************** *** 1214,1218 **** if (target_entity) { ! printf("Still chasing %s at (%1.2f,%1.2f,%1.2f)...\n",(const char *)name,pos.x,pos.y,pos.z); float angle = psGameObject::CalculateIncidentAngle(pos2,pos); // keep turning towards the chased player if (fabs(angle-rot)>EPSILON) --- 1225,1229 ---- if (target_entity) { ! npc->Printf("Still chasing %s at (%1.2f,%1.2f,%1.2f)...\n",(const char *)name,pos.x,pos.y,pos.z); float angle = psGameObject::CalculateIncidentAngle(pos2,pos); // keep turning towards the chased player if (fabs(angle-rot)>EPSILON) *************** *** 1232,1236 **** bool ChaseOperation::CompleteOperation(NPC *npc,EventManager *eventmgr) { ! printf("Chase completed. Stopping now.\n"); // Stop the movement --- 1243,1247 ---- bool ChaseOperation::CompleteOperation(NPC *npc,EventManager *eventmgr) { ! npc->Printf("Chase completed. Stopping now.\n"); // Stop the movement *************** *** 1265,1269 **** bool PickupOperation::Run(NPC *npc,EventManager *eventmgr) { ! printf("PickupOp "); return true; } --- 1276,1280 ---- bool PickupOperation::Run(NPC *npc,EventManager *eventmgr) { ! npc->Printf("PickupOp "); return true; } *************** *** 1287,1291 **** bool MeleeOperation::Run(NPC *npc,EventManager *eventmgr) { ! printf("MeleeOp "); attacked_ent = npc->GetMostHated(range); --- 1298,1302 ---- bool MeleeOperation::Run(NPC *npc,EventManager *eventmgr) { ! npc->Printf("MeleeOperation running "); attacked_ent = npc->GetMostHated(range); *************** *** 1295,1318 **** } ! return true; } void MeleeOperation::Advance(float timedelta,NPC *npc,EventManager *eventmgr) { - printf("Melee operation advancing\n"); // Check hate list to make sure we are still attacking the right person iCelEntity *ent = npc->GetMostHated(range); if (ent != attacked_ent) { ! printf("Melee switching to attack %s\n",ent->GetName() ); attacked_ent = ent; npcclient->GetNetworkMgr()->QueueAttackCommand(npc->GetEntity(),ent); } - } void MeleeOperation::InterruptOperation(NPC *npc,EventManager *eventmgr) { ! printf("Interrupting melee operation"); npcclient->GetNetworkMgr()->QueueAttackCommand(npc->GetEntity(),NULL); } --- 1306,1327 ---- } ! return false; } void MeleeOperation::Advance(float timedelta,NPC *npc,EventManager *eventmgr) { // Check hate list to make sure we are still attacking the right person iCelEntity *ent = npc->GetMostHated(range); if (ent != attacked_ent) { ! npc->Printf("Melee switching to attack %s\n",ent->GetName() ); attacked_ent = ent; npcclient->GetNetworkMgr()->QueueAttackCommand(npc->GetEntity(),ent); } } void MeleeOperation::InterruptOperation(NPC *npc,EventManager *eventmgr) { ! npc->Printf("Interrupting melee operation"); npcclient->GetNetworkMgr()->QueueAttackCommand(npc->GetEntity(),NULL); } *************** *** 1320,1324 **** bool MeleeOperation::CompleteOperation(NPC *npc,EventManager *eventmgr) { ! printf("Completing melee operation"); npcclient->GetNetworkMgr()->QueueAttackCommand(npc->GetEntity(),NULL); return true; --- 1329,1333 ---- bool MeleeOperation::CompleteOperation(NPC *npc,EventManager *eventmgr) { ! npc->Printf("Completing melee operation"); npcclient->GetNetworkMgr()->QueueAttackCommand(npc->GetEntity(),NULL); return true; *************** *** 1343,1347 **** bool BeginLoopOperation::Run(NPC *npc,EventManager *eventmgr) { ! printf("BeginLoop "); return true; } --- 1352,1356 ---- bool BeginLoopOperation::Run(NPC *npc,EventManager *eventmgr) { ! npc->Printf("BeginLoop "); return true; } *************** *** 1360,1364 **** bool EndLoopOperation::Run(NPC *npc,EventManager *eventmgr) { ! printf("EndLoop "); return true; } --- 1369,1373 ---- bool EndLoopOperation::Run(NPC *npc,EventManager *eventmgr) { ! npc->Printf("EndLoop "); return true; } *************** *** 1416,1420 **** bool DropOperation::Run(NPC *npc,EventManager *eventmgr) { ! printf("DropOp "); return true; } --- 1425,1429 ---- bool DropOperation::Run(NPC *npc,EventManager *eventmgr) { ! npc->Printf("DropOp "); return true; } *************** *** 1477,1481 **** psGameObject::GetPosition(npc->GetEntity(),pos,rot,sec); ! printf("MovePath Loc is %1.2f, %1.2f, %1.2f\n",pos.x,pos.y,pos.z); } --- 1486,1490 ---- psGameObject::GetPosition(npc->GetEntity(),pos,rot,sec); ! npc->Printf("MovePath Loc is %1.2f, %1.2f, %1.2f\n",pos.x,pos.y,pos.z); } Index: npcbehave.h =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npcbehave.h,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** npcbehave.h 17 Sep 2003 18:19:32 -0000 1.19 --- npcbehave.h 20 Sep 2003 07:39:26 -0000 1.20 *************** *** 51,54 **** --- 51,55 ---- protected: BinaryTree<Behavior> behaviors; + //csArray<Behavior*> behaviors; Behavior *active; *************** *** 220,224 **** public: virtual bool Run(NPC *npc,EventManager *eventmgr)=0; ! virtual void Advance(float timedelta,NPC *npc,EventManager *eventmgr) { }; virtual void InterruptOperation(NPC *npc,EventManager *eventmgr) { } virtual bool CompleteOperation(NPC *npc,EventManager *eventmgr) { return true; } --- 221,225 ---- public: virtual bool Run(NPC *npc,EventManager *eventmgr)=0; ! virtual void Advance(float timedelta,NPC *npc,EventManager *eventmgr); virtual void InterruptOperation(NPC *npc,EventManager *eventmgr) { } virtual bool CompleteOperation(NPC *npc,EventManager *eventmgr) { return true; } Index: npcclient.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npcclient.cpp,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** npcclient.cpp 17 Sep 2003 18:19:32 -0000 1.18 --- npcclient.cpp 20 Sep 2003 07:39:26 -0000 1.19 *************** *** 535,539 **** ! void psNPCClient::ListNPC(char * pattern) { for (int i = 0; i < npcs.Length(); i++) --- 535,539 ---- ! void psNPCClient::ListAllNPCs(char * pattern) { for (int i = 0; i < npcs.Length(); i++) *************** *** 546,549 **** --- 546,565 ---- (npcs[i]->GetCurrentBehavior()?npcs[i]->GetCurrentBehavior()->GetName():"")); } + } + } + + void psNPCClient::ListAllEntities() + { + for (int i=0; i < GetActorCount(); i++) + { + iCelEntity *ent = GetActor(i); + csVector3 pos; + float rot; + iSector *sector; + psGameObject::GetPosition(ent,pos,rot,sector); + printf("%d\t%-20s %3.0f %3.0f %3.0f\n", + ent->GetID(), + ent->GetName(), + pos.x,pos.y,pos.z); } } Index: npcclient.h =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/npcclient.h,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** npcclient.h 17 Sep 2003 18:19:32 -0000 1.14 --- npcclient.h 20 Sep 2003 07:39:26 -0000 1.15 *************** *** 188,194 **** /** ! * List all NPCs matching pattern to console */ ! void ListNPC(char *pattern); protected: --- 188,200 ---- /** ! * List all NPCs matching pattern to console. */ ! void ListAllNPCs(char *pattern); ! ! /** ! * List all known entities on superclient. ! */ ! void ListAllEntities(); ! protected: Index: perceptions.cpp =================================================================== RCS file: /cvsroot/planeshift/planeshift/src/npcclient/perceptions.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** perceptions.cpp 17 Sep 2003 18:08:20 -0000 1.5 --- perceptions.cpp 20 Sep 2003 07:39:26 -0000 1.6 *************** *** 93,100 **** void Reaction::React(NPC *who,EventManager *eventmgr,Perception *pcpt) { if (!pcpt->ShouldReact(this)) return; ! printf("Adding %1.1f need to behavior %s for npc %s.\n", delta_desire, affected->GetName(), who->GetEntity()->GetName() ); --- 93,103 ---- void Reaction::React(NPC *who,EventManager *eventmgr,Perception *pcpt) { + if (!who) + printf("ERROR: perception for NULL npc\n"); + if (!pcpt->ShouldReact(this)) return; ! who->Printf("Adding %1.1f need to behavior %s for npc %s.\n", delta_desire, affected->GetName(), who->GetEntity()->GetName() ); *************** *** 226,230 **** if (name == reaction->GetEventType()) { ! printf("Correct Name is only condition right now.\n"); return true; } --- 229,233 ---- if (name == reaction->GetEventType()) { ! printf("Damage of %1.1f from player %s.\n",damage,attacker->GetName() ); return true; } |