Jump to content

Home

become hostile script.


Darth Balor

Recommended Posts

I need a script to make a character attack that can be fired by dialog

 

If this is for KOTOR 2 you can do something like this:

[color=PaleGreen]// ST:  st_a_attack.nss[/color]

[color=PaleGreen]// ST: Needs the AI include files from scripts.bif to compile.[/color]
#include "k_inc_generic"

void Attack(object oTarget=OBJECT_INVALID);


[color=PaleGreen]// ST: Main function[/color]
void main() {
   object oVictim = GetObjectByTag(GetScriptStringParameter());

   DelayCommand(0.5, Attack(oVictim));
}


[color=PaleGreen]// ST: Support function, go hostile and attack.[/color]
void Attack(object oTarget=OBJECT_INVALID) {
   if (!GetIsObjectValid(oTarget)) {
       oTarget = GetFirstPC();
   }

   [color=PaleGreen]// ST: Turn hostile if not already that[/color]
   if (!GetIsEnemy(oTarget)) {
       if (IsObjectPartyMember(oTarget)) {
           ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_HOSTILE_1);
       }
       else {
           int iFac = GetStandardFaction(oTarget);
           if ((iFac == STANDARD_FACTION_HOSTILE_1) || (iFac == STANDARD_FACTION_HOSTILE_2)) {
               ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_FRIENDLY_1);
           }
           else {
               ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_HOSTILE_1);
           }
       }
   }

   [color=PaleGreen]// ST: Start combat against the victim[/color]
   GN_DetermineCombatRound(oTarget);
}

 

Set the String Param field for the script of the dialog node to the Tag of the NPC the dialog owner should attack, or leave it blank to make him/her attack the player character.

 

Also how do you get dialoge to fire when the NPC see's you insted of having to click him.

 

You can either give the NPC a custom OnPerception event script that initiates dialog when the player is spotted, or, if the NPC always is in the same location, you can place a trigger that starts the conversation when the player trips it.

 

Here is an example of an OnPerception event script that would start conversation with the player as soon as the NPC sees them for the first time:

void main() {
   if (!GetLocalBoolean(OBJECT_SELF, 40)
       && GetLastPerceptionSeen()
       && (GetFirstPC() == GetLastPerceived()))
   {
       ClearAllActions();
       SetLocalBoolean(OBJECT_SELF, 40, TRUE);
       ActionStartConversation(GetFirstPC(), "", FALSE, CONVERSATION_TYPE_CINEMATIC, TRUE);
   }
   else {
       ExecuteScript("k_ai_master", OBJECT_SELF, 1002);
   }
}

Link to comment
Share on other sites

// ST: Needs the AI include files from scripts.bif to compile.

What does that mean?

 

Ok I'm still having problems.

 

What I'm trying to do is.

 

*Have NPC spawn in, walk to pc when spotted(works fine)

*NPC start dialoge when sees PC(doesn't work, I have to select the NPC to initiate dialoge!)

*NPC become hostile after the last dialoge node.(doesn't work)

I was trying to use one script to have him spawn, walk to, and intiate dialoge. spawning and walking works, but he doesn't begin dialoge.

and at the end of the dialoge he doesn't become hostile. also i tried using you'r onPerception script to no avail.

 

script for spawning

void main() 
{
//do a whereami cheat to get the coordinates of the location 
//where you want the npc to spawn
float x=0.28780;  
float y=-46.47008;
float z=11.00560;
//unless you want something specific, forget about orientation
float r=0.00;
vector vecNPC=Vector(x,y,z);
location locNPC=Location(vecNPC, r);
//insert your npc tag instead of my_creature_resref. Don't remove the quotes and do not exceed 16 chars.
object oNPC=CreateObject(OBJECT_TYPE_CREATURE,"g_201thug",locNPC);
object oPC=GetFirstPC();
AssignCommand(oNPC,ActionMoveToObject(oPC));
//replace my_dlg by the name of the dlg file the npc is supposed to use
AssignCommand(oNPC,ActionStartConversation(oPC,"durothug"));
}

 

become hostile script.

// ST:  st_a_attack.nss

// ST: Needs the AI include files from scripts.bif to compile.
#include "k_inc_generic"

void Attack(object oTarget=OBJECT_INVALID);


// ST: Main function
void main() {
   object oVictim = GetObjectByTag(GetScriptStringParameter());

   DelayCommand(0.5, Attack(oVictim));
}


// ST: Support function, go hostile and attack.
void Attack(object oTarget=OBJECT_INVALID) {
   if (!GetIsObjectValid(oTarget)) {
       oTarget = GetFirstPC();
   }

   // ST: Turn hostile if not already that
   if (!GetIsEnemy(oTarget)) {
       if (IsObjectPartyMember(oTarget)) {
           ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_HOSTILE_1);
       }
       else {
           int iFac = GetStandardFaction(oTarget);
           if ((iFac == STANDARD_FACTION_HOSTILE_1) || (iFac == STANDARD_FACTION_HOSTILE_2)) {
               ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_FRIENDLY_1);
           }
           else {
               ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_HOSTILE_1);
           }
       }
   }

   // ST: Start combat against the victim
   GN_DetermineCombatRound(oTarget);
}

Link to comment
Share on other sites

I was trying to use one script to have him spawn, walk to, and intiate dialoge. spawning and walking works, but he doesn't begin dialoge.

and at the end of the dialoge he doesn't become hostile. also i tried using you'r onPerception script to no avail.

 

If I understand you correctly things should happen in separate stages, so it's best to use separate scripts for it, IMO. So, if I understand correctly you want this to happen?

  1. The NPC spawns into the area and just waits around until the player happens to come by.
  2. Whenever the player gets within sight of the NPC, move to them and start conversation.
  3. At the end of the conversation, attack the player.

 

 

*Have NPC spawn in, walk to pc when spotted(works fine)

 

I assume the NPC should hang around the area to begin with, waiting for the player to come close? If so you'd spawn it in with a script from somewhere (another dialog, Area OnEnter event script, whatever) like:

 

st_spawnthug.nss

#include "st_inc_util"

void main() {
   ST_SpawnNPC("g_201thug", 0.29, -46.47);
}

 

*NPC start dialoge when sees PC(doesn't work, I have to select the NPC to initiate dialoge!)

 

A more specialized variant of the OnPerception AI script should work for this. Compile it and in then open the UTC template of the NPC with KotorTool, go to the scripts tab and set the name of the script in the OnNotice box (st_ai_thugperc in the below example since the script is called st_ai_thugperc.ncs). This assumes that durothug.dlg is the dialog file to use.

 

st_ai_thugperc.nss

void main() {
   object oPC = GetFirstPC();
   if (!GetLocalBoolean(OBJECT_SELF, 140)
       && GetLastPerceptionSeen()
       && (oPC == GetLastPerceived()))
   {
       SetLocalBoolean(OBJECT_SELF, 140, TRUE);
       SetLocalBoolean(OBJECT_SELF, 87, TRUE);
       ClearAllActions();
       ActionForceMoveToObject(oPC, TRUE);
       ActionDoCommand(SetLocalBoolean(OBJECT_SELF, 87, FALSE));
       ActionStartConversation(oPC, "durothug");
   }
   else if (!GetLocalBoolean(OBJECT_SELF, 87)) {
       ExecuteScript("k_ai_master", OBJECT_SELF, 1002);
   }
}

 

*NPC become hostile after the last dialoge node.(doesn't work)

 

Set the name of a script like compiled from the below example as action script on the last dialog node after which the NPC should attack.

 

st_ai_thugperc.nss

#include "st_inc_util"

void main() {
   ST_Attack();
}

 

(Or in this case you could just use the standard script a_atkonend that comes with the game already which does pretty much the same thing for this purpose. :))

 

 

 

 

The above scripts require a file named st_inc_util.nss to be present in the override folder (if you use KotorTool's script compiler to produce the NCS files) to work, whose content (the relevant parts of it anyway) should look like:

 

Excerpt from include file: st_inc_util.nss

#include "k_inc_generic"

object ST_SpawnNPC(string sResRef, float x, float y, float fAngle = 0.0);
void   ST_Attack(object oTarget=OBJECT_INVALID);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Wrapper function: Spawn NPCs into the game a bit quicker
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
object ST_SpawnNPC(string sResRef, float x, float y, float fAngle = 0.0) {
   return CreateObject(OBJECT_TYPE_CREATURE, sResRef, Location(Vector(x, y, 0.0), fAngle));
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ST: Support function, make caller go hostile to oTarget and attack them.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ST_Attack(object oTarget=OBJECT_INVALID) {
   if (!GetIsObjectValid(oTarget)) {
       oTarget = GetFirstPC();
   }

   // ST: Turn hostile if not already that
   if (!GetIsEnemy(oTarget)) {
       if (IsObjectPartyMember(oTarget)) {
           ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_HOSTILE_1);
       }
       else {
           int iFac = GetStandardFaction(oTarget);
           if ((iFac == STANDARD_FACTION_HOSTILE_1) || (iFac == STANDARD_FACTION_HOSTILE_2)) {
               ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_FRIENDLY_1);
           }
           else if ((iFac == STANDARD_FACTION_FRIENDLY_1) || (iFac == STANDARD_FACTION_FRIENDLY_2)) {
               ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_HOSTILE_1);
           }
           else {
               ChangeToStandardFaction(OBJECT_SELF, STANDARD_FACTION_GIZKA_1);
               ChangeToStandardFaction(oTarget, STANDARD_FACTION_GIZKA_2);
           }
       }
   }

   // ST: Start combat against the victim
   GN_DetermineCombatRound(oTarget);
}

 

 

Why the long script?

 

To make a generic, multipurpose script that you can reuse later in any number of similar situations (to make an NPC attack any other NPC or the player in this case) without any changes to the script itself. It may be more work initially, but if you do a lot of scripting you won't have to re-invent the wheel over and over if you make yourself a collection of generic dialog conditional/action scripts for common operations, and a set of include files with reusable, generic utility functions for common, often needed situations and tasks.

 

In the examples I used above I have an utility include file, st_inc_util.nss, which contains a bunch of custom made generic functions for various tasks such as spawning NPCs, initiating combat, making NPCs turn and move around, party management, healing, manipulating effects and force powers etc. As seen it reduces the whole "spawn an NPC" and "attack a target" scripts to one fairly short line whenever you need to do that as long as you just include your utility include file.

 

(That, and I tend to like convoluted solutions to simple problems :p)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...