Advertisement
Guest User

prc default

a guest
Mar 6th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. //::///////////////////////////////////////////////
  2. //:: Default eventscript
  3. //:: default
  4. //:://////////////////////////////////////////////
  5. /** @file
  6. This script is executed by the engine for events
  7. when a creature does not have a script defined
  8. for the event in question. This includes PCs.
  9.  
  10. The purpose of this script is to determine
  11. which particular event triggered it's execution
  12. and to route execution to scripts dedicated to
  13. that event.
  14. */
  15. //:://////////////////////////////////////////////
  16. //:://////////////////////////////////////////////
  17.  
  18. #include "prc_alterations"
  19. #include "prc_inc_leadersh"
  20.  
  21. const int LOCAL_DEBUG = FALSE; //DEBUG;
  22.  
  23. /**************************/
  24. /* Declarations for Tests */
  25. /**************************/
  26.  
  27. int IsBlocked(); // test GetBlockingDoor()
  28. int IsCombatRoundEnd(); // Need to fake this
  29. int IsConversation(); // test a local variable
  30. int IsDamaged(); // test GetLastDamager()
  31. int IsDeath(); // test GetIsDead(OBJECT_SELF)
  32. int IsDisturbed(); // test GetLastDisturbed()
  33. int IsHeartbeat(); // test game time
  34. int IsPerception(); // test GetLastPerceived()
  35. int IsPhysicalAttacked(); // test GetLastAttacker()
  36. int IsRested(); // test GetIsResting(GetMaster())
  37. int IsSpawn(); // run once, never again
  38. int IsSpellCastAt(); // test GetLastSpellCaster()
  39. int IsUserDefined(); // test GetUserDefinedEventNumber()
  40.  
  41. /*********************************/
  42. /* Utility Function declarations */
  43. /*********************************/
  44.  
  45. //void ForceAddHenchman(object oHenchman);
  46. //int IsLinkboyAttached();
  47. //void GetLinkboy();
  48. int IsObjectChanged(object oTest, string sVarname);
  49. int IsIntChanged(int iTest, string sVarname);
  50. int IsStringChanged(string sTest, string sVarname);
  51. void RunScript(int nEvent);
  52. //void SpawnRecallLocals(object oPC);
  53. //void StartGroupDiscussion();
  54.  
  55. /*****************************/
  56. /* Implementation of Actions */
  57. /*****************************/
  58.  
  59. void OnSpawn() { RunScript(EVENT_VIRTUAL_ONSPAWNED); }
  60. void OnDeath() { RunScript(EVENT_VIRTUAL_ONDEATH); }
  61. void OnRested() { RunScript(EVENT_VIRTUAL_ONRESTED); }
  62. void OnHeartbeat() { RunScript(EVENT_VIRTUAL_ONHEARTBEAT); }
  63. void OnPerception() { RunScript(EVENT_VIRTUAL_ONPERCEPTION); }
  64. void OnBlocked() { RunScript(EVENT_VIRTUAL_ONBLOCKED); }
  65. void OnCombatRoundEnd() { RunScript(EVENT_VIRTUAL_ONCOMBATROUNDEND); }
  66. void OnDisturbed() { RunScript(EVENT_VIRTUAL_ONDISTURBED); }
  67. void OnPhysicalAttacked() { RunScript(EVENT_VIRTUAL_ONPHYSICALATTACKED); }
  68. void OnSpellCastAt() { RunScript(EVENT_VIRTUAL_ONSPELLCASTAT); }
  69. void OnDamaged() { RunScript(EVENT_VIRTUAL_ONDAMAGED); }
  70. void OnUserDefined() { RunScript(EVENT_VIRTUAL_ONUSERDEFINED); }
  71. void OnConversation() { RunScript(EVENT_VIRTUAL_ONCONVERSATION); }
  72.  
  73. /******************/
  74. /* Main Procedure */
  75. /******************/
  76.  
  77. void main()
  78. {
  79. if(LOCAL_DEBUG) DoDebug("default running for " + DebugObject2Str(OBJECT_SELF));
  80.  
  81. // OnConversation is exclusive of everything else, since it is just routed through this script
  82. if(IsConversation()) OnConversation();
  83. else
  84. {
  85. if(IsBlocked()) OnBlocked();
  86. if(IsCombatRoundEnd()) OnCombatRoundEnd();
  87. if(IsDamaged()) OnDamaged();
  88. if(IsDeath()) OnDeath();
  89. if(IsDisturbed()) OnDisturbed();
  90. if(IsHeartbeat()) OnHeartbeat();
  91. if(IsPerception()) OnPerception();
  92. if(IsPhysicalAttacked()) OnPhysicalAttacked();
  93. if(IsRested()) OnRested();
  94. if(IsSpawn()) OnSpawn();
  95. if(IsSpellCastAt()) OnSpellCastAt();
  96. if(IsUserDefined()) OnUserDefined();
  97. }
  98. }
  99.  
  100. /************************/
  101. /* Tests for conditions */
  102. /************************/
  103.  
  104. int IsBlocked()
  105. {
  106. return IsObjectChanged(GetBlockingDoor(), "BlockingDoor");
  107. }
  108.  
  109. int IsCombatRoundEnd()
  110. {
  111. // Need to fake this.
  112. // Return TRUE iff you are in combat and not doing anything useful
  113. if(GetIsInCombat() ||
  114. (GetIsObjectValid(GetMaster()) &&
  115. GetIsInCombat(GetMaster())
  116. )
  117. )
  118. {
  119. int nGCA = GetCurrentAction();
  120. if(nGCA == ACTION_ATTACKOBJECT ||
  121. nGCA == ACTION_CASTSPELL ||
  122. nGCA == ACTION_COUNTERSPELL ||
  123. nGCA == ACTION_HEAL ||
  124. nGCA == ACTION_FOLLOW ||
  125. nGCA == ACTION_ITEMCASTSPELL ||
  126. nGCA == ACTION_KIDAMAGE ||
  127. nGCA == ACTION_OPENDOOR ||
  128. nGCA == ACTION_SMITEGOOD
  129. )
  130. {
  131. return FALSE;
  132. }
  133. else
  134. {
  135. return TRUE;
  136. }
  137. }
  138. else
  139. {
  140. return FALSE;
  141. }
  142. }
  143.  
  144. int IsConversation()
  145. {
  146. object oCreature = OBJECT_SELF;
  147. if(GetLocalInt(oCreature, "default_conversation_event"))
  148. {
  149. DeleteLocalInt(oCreature, "default_conversation_event");
  150. return TRUE;
  151. }
  152.  
  153. return FALSE;
  154. }
  155.  
  156. int IsDamaged()
  157. {
  158. object oCreature = OBJECT_SELF;
  159. object oDamager = GetLastDamager(oCreature);
  160.  
  161. // The damage source must be valid
  162. if(GetIsObjectValid(oDamager))
  163. {
  164. // Get previous damage data
  165. string sOldDamage = GetLocalString(oCreature, "PRC_Event_OnDamaged_Data");
  166.  
  167. // Create string based on current damage values
  168. // Start with the damaging object
  169. string sNewDamage = ObjectToString(oDamager);
  170. // Catenate amount of damage of each damage type
  171. int i;
  172. for(i = DAMAGE_TYPE_BLUDGEONING; i <= DAMAGE_TYPE_BASE_WEAPON; i = i << 1)
  173. sNewDamage += IntToString(GetDamageDealtByType(i));
  174.  
  175. // Determine if the damage dealt has changed
  176. if(sOldDamage != sNewDamage)
  177. {
  178. if(LOCAL_DEBUG) DoDebug("default: Damage has changed:\n" + sNewDamage);
  179. SetLocalString(oCreature, "PRC_Event_OnDamaged_Data", sNewDamage);
  180.  
  181. // Update damage counter
  182. SetLocalInt(oCreature, "PRC_LastDamageTaken", GetTotalDamageDealt());
  183.  
  184. return TRUE;
  185. }
  186. }
  187.  
  188. return FALSE;
  189. }
  190.  
  191. int IsDeath()
  192. {
  193. return GetIsDead(OBJECT_SELF);
  194. }
  195.  
  196. int IsDisturbed()
  197. {
  198. object oCreature = OBJECT_SELF;
  199. object oDisturber = GetLastDisturbed();
  200.  
  201. if(GetIsObjectValid(oDisturber)) // The creature has been disturbed at least once during the game
  202. {
  203. // Get previous disturb data
  204. string sOldDisturb = GetLocalString(oCreature, "PRC_Event_OnDisturbed_Data");
  205.  
  206. // Create string based on current disturb values
  207. string sNewDisturb = ObjectToString(oDisturber);
  208. sNewDisturb += IntToString(GetInventoryDisturbType());
  209. sNewDisturb += ObjectToString(GetInventoryDisturbItem());
  210.  
  211. // Determine if the data has changed
  212. if(sOldDisturb != sNewDisturb)
  213. {
  214. if(LOCAL_DEBUG) DoDebug("default: Disturbed has changed:\n" + sNewDisturb);
  215. SetLocalString(oCreature, "PRC_Event_OnDisturbed_Data", sNewDisturb);
  216. return TRUE;
  217. }
  218. }
  219.  
  220. return FALSE;
  221. }
  222.  
  223. int IsHeartbeat()
  224. {
  225. object oCreature = OBJECT_SELF;
  226. // PCs use the module HB
  227. if(!GetIsPC(oCreature))
  228. {
  229. // Check how long since last recorded heartbeat
  230. int nSecsChange = (GetTimeSecond() - GetLocalInt(oCreature, "PRC_LastHeartbeatSeconds") + 60) % 60;
  231.  
  232. // See if the master clock has ticked or 9 seconds have elapsed anyway
  233. if(nSecsChange >= 6)
  234. {
  235. SetLocalInt(oCreature, "PRC_LastHeartbeatSeconds", GetTimeSecond());
  236. return TRUE;
  237. }
  238. }
  239.  
  240. return FALSE;
  241. }
  242.  
  243. int IsPerception()
  244. {
  245. object oCreature = OBJECT_SELF;
  246. object oPerceived = GetLastPerceived();
  247.  
  248. if(GetIsObjectValid(oPerceived)) // The creature has perceived something at least once during the game
  249. {
  250. // Get previous perception data
  251. string sOldPerception = GetLocalString(oCreature, "PRC_Event_OnPerception_Data");
  252.  
  253. // Create string based on current perception values
  254. string sNewPerception = ObjectToString(oPerceived);
  255. sNewPerception += IntToString(GetLastPerceptionHeard());
  256. sNewPerception += IntToString(GetLastPerceptionInaudible());
  257. sNewPerception += IntToString(GetLastPerceptionSeen());
  258. sNewPerception += IntToString(GetLastPerceptionVanished());;
  259.  
  260. // Determine if the data has changed
  261. if(sOldPerception != sNewPerception)
  262. {
  263. if(LOCAL_DEBUG) DoDebug("default: Perception has changed:\n" + sNewPerception);
  264. SetLocalString(oCreature, "PRC_Event_OnPerception_Data", sNewPerception);
  265. return TRUE;
  266. }
  267. }
  268.  
  269. return FALSE;
  270. }
  271.  
  272. int IsPhysicalAttacked()
  273. {
  274. object oCreature = OBJECT_SELF;
  275. object oAttacker = GetLastAttacker();
  276.  
  277. // Recent enough event that the attacker is at least still valid
  278. if(GetIsObjectValid(oAttacker))
  279. {
  280. // Get previous attack data
  281. string sOldAttack = GetLocalString(oCreature, "PRC_Event_OnPhysicalAttacked_Data");
  282.  
  283. // Create string for the current attack data
  284. string sNewAttack = ObjectToString(oAttacker);
  285. sNewAttack += ObjectToString(GetLastWeaponUsed(oAttacker));
  286. sNewAttack += IntToString(GetLastAttackMode(oAttacker));
  287. sNewAttack += IntToString(GetLastAttackType(oAttacker));
  288.  
  289. // Determine if the data has changed
  290. if(sOldAttack != sNewAttack)
  291. {
  292. if(LOCAL_DEBUG) DoDebug("default: Attack has changed:\n" + sNewAttack);
  293. SetLocalString(oCreature, "PRC_Event_OnPhysicalAttacked_Data", sNewAttack);
  294. return TRUE;
  295. }
  296. }
  297.  
  298. return FALSE;
  299. }
  300.  
  301. int IsRested()
  302. {
  303. // PCs use the module OnRest events
  304. if(!GetIsPC(OBJECT_SELF))
  305. {
  306. // Goes TRUE when Master starts resting
  307. int bMasterIsResting = GetIsResting(GetMaster());
  308. return IsIntChanged(bMasterIsResting,"MasterIsResting") && bMasterIsResting;
  309. }
  310.  
  311. return FALSE;
  312. }
  313.  
  314. int IsSpawn()
  315. {
  316. object oCreature = OBJECT_SELF;
  317. if(!GetLocalInt(oCreature, "PRC_OnSpawn_Marker"))
  318. {
  319. SetLocalInt(oCreature, "PRC_OnSpawn_Marker", TRUE);
  320. return TRUE;
  321. }
  322.  
  323. return FALSE;
  324. }
  325.  
  326. int IsSpellCastAt()
  327. {
  328. object oCreature = OBJECT_SELF;
  329. if(LOCAL_DEBUG) DoDebug("default: IsSpellCastAt():\n"
  330. + "GetLastSpellCaster() = " + DebugObject2Str(GetLastSpellCaster()) + "\n"
  331. + "GetLastSpell() = " + IntToString(GetLastSpell()) + "\n"
  332. + "GetLastSpellHarmful() = " + IntToString(GetLastSpellHarmful()) + "\n"
  333. );
  334. // If the event data does not contain the fake value, a spell has been cast
  335. if(GetLastSpell() != -1)
  336. {
  337. // Reset the event data to the fake value
  338. DelayCommand(0.0f, SignalEvent(oCreature, EventSpellCastAt(oCreature, -1, FALSE)));
  339.  
  340. return TRUE;
  341. }
  342.  
  343. return FALSE;
  344. }
  345.  
  346. int IsUserDefined()
  347. {
  348. object oCreature = OBJECT_SELF;
  349. if(LOCAL_DEBUG) DoDebug("default: IsUserDefined():\n"
  350. + "GetUserDefinedEventNumber() = " + IntToString(GetUserDefinedEventNumber()) + "\n"
  351. );
  352.  
  353. if(GetUserDefinedEventNumber() != -1)
  354. {
  355. // Reset the event data to the fake value
  356. DelayCommand(0.0f, SignalEvent(oCreature, EventUserDefined(-1)));
  357.  
  358. return TRUE;
  359. }
  360.  
  361. return FALSE;
  362. }
  363.  
  364. /*********************/
  365. /* Utility Functions */
  366. /*********************/
  367.  
  368. int IsObjectChanged(object oTest, string sName)
  369. {
  370. if(oTest != GetLocalObject(OBJECT_SELF, "PRC_Event_" + sName))
  371. {
  372. SetLocalObject(OBJECT_SELF, "PRC_Event_" + sName, oTest);
  373. return TRUE;
  374. }
  375. else
  376. {
  377. return FALSE;
  378. }
  379. }
  380.  
  381. int IsIntChanged(int iTest, string sName)
  382. {
  383. if(iTest != GetLocalInt(OBJECT_SELF, "PRC_Event_" + sName))
  384. {
  385. SetLocalInt(OBJECT_SELF, "PRC_Event_" + sName, iTest);
  386. return TRUE;
  387. }
  388. else
  389. {
  390. return FALSE;
  391. }
  392. }
  393.  
  394. int IsStringChanged(string sTest, string sName)
  395. {
  396. if(sTest != GetLocalString(OBJECT_SELF, "PRC_Event_" + sName))
  397. {
  398. SetLocalString(OBJECT_SELF, "PRC_Event_" + sName, sTest);
  399. return TRUE;
  400. }
  401. else
  402. {
  403. return FALSE;
  404. }
  405. }
  406.  
  407. void RunScript(int nEvent)
  408. {
  409. object oSelf = OBJECT_SELF;
  410.  
  411. if(LOCAL_DEBUG) DoDebug("default, event = " + IntToString(nEvent));
  412.  
  413. if(nEvent == EVENT_VIRTUAL_ONDAMAGED)
  414. SignalEvent(oSelf, EventUserDefined(EVENT_DAMAGED));
  415. // Determine NPC script name and run generic eventhook
  416. ExecuteAllScriptsHookedToEvent(oSelf, nEvent);
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement