Capes 
Journeyman

Posts: 57
Likes Given: 0
Likes Received: 4 in 4 posts
Joined: Mar 2018
Reputation: 0
![]()
|
e_Carnivores - Making Carnivores hunt
Alright - since this community has been very helpful in answering my many questions regarding how sphere works and such, I am submitting my first script. Please feel free to critique!
0.56d nightly.
Info - I noticed events in sphere_event_npcs.scp like e_Carnivores,e_Carnivores2 & e_Carnivores3 were blank, and in the corresponding sphere_monsters.scp file all the code was already there for critters to be carnivores.
This is my attempt to make animals hunt when hungry, provided that they are carnivores (if they have TEVENTS in their CHARDEF defined as e_Carnivores). This only effects animals.
1)
Global variable (placed in sphere_serv_triggers.scp under [FUNCTION f_onserver_start])
VAR.HUNGER_RATE_DECAY_TIME = 120
/////////////////////////////////////////////////////
///// VAR.HUNGER_RATE_DECAY_TIME = 120
//// (in secs)
//// 120 = every 2 mins at 20 mins per day cycle or reduced 10 times a day
//// so critter with maxfood=10 will eat once a day
/////////////////////////////////////////////////////
2)
Modified sphere_events_npc.scp [EVENTS]e_Carnivores,e_Carnivores2,e_Carnivores3 just like below
[EVENTS e_Carnivores]
// weak carnivores.
ON=@CREATE
EVENTS +e_Hunger
f_setNeedsFood
[EVENTS e_Carnivores2]
// normal carnivores.
ON=@CREATE
EVENTS +e_Hunger
f_setNeedsFood
[EVENTS e_Carnivores3]
// strong carnivores.
ON=@CREATE
EVENTS +e_Hunger
f_setNeedsFood
3)
Added this to the bottom of sphere_events_npc.scp
/////////////////////////////////////////////////////
//// EVENT e_Hunger
//// -our event handler for ON=@ HUNGER/KILL/DEATH
//// -only executes when (hunger_in_effect) is set on animal
//// -1)sets a new brain to animal and possible buff/debuff based on e__Carnivores TEVENTS
//// -2)resets brain to animal after killing (eating) so it doesn't hunt anymore
//// -3)cleans up mem from tags
/////////////////////////////////////////////////////
[EVENTS e_Hunger]
ON=@HUNGER //<-- triggered from f_Hunger_handler
IF !(<eval <I.TAG.hunger_in_effect>>)
return //no hunger_in_effect so quit
ENDIF
//change brain and do something special for given e_Carnivore's type
IF ((STRCMPI(<I.TEVENTS>,e_Carnivores)==0) || (STRCMPI(<I.TEVENTS>,e_Carnivores)==1)))
//we are e_Carnivores (weak), do something
ELSEIF ((STRCMPI(<I.TEVENTS>,e_Carnivores2)==0) || (STRCMPI(<I.TEVENTS>,e_Carnivores2)==1)))
//we are e_Carnivores2 (normal), do something
ELSEIF ((STRCMPI(<I.TEVENTS>,e_Carnivores3)==0) || (STRCMPI(<I.TEVENTS>,e_Carnivores3)==1)))
//we are e_Carnivores3 (strong), do something
ENDIF
//we are hungry attack something (change brain)
I.NPC = brain_berserk
ON=@KILL //<-- triggered after any kill
IF !(<eval <I.TAG.hunger_in_effect>>)
return //no hunger_in_effect so quit
ENDIF
//change back to animal brain, reset hunger lvl and restart timer
I.NPC = brain_animal
I.TAG.hunger_lvl =<eval <I.FOOD>>
I.TIMERF <eval <VAR.HUNGER_RATE_DECAY_TIME>> f_Hunger_handler
ON=@DEATH
IF !(<eval <I.TAG.hunger_in_effect>>)
return //no hunger_in_effect so quit
ENDIF
//clear hunger lvl tag & hunger in effect from mem
I.TAG.hunger_lvl =
I.TAG.hunger_in_effect =
/////////////////////////////////////////////////////
//// FUNCTION f_setNeedsFood
//// -prevents usage if not brain_animal (must be an animal)
//// -set hunger lvl & hunger in effect tags
//// -starts our hunger handler
/////////////////////////////////////////////////////
[FUNCTION f_setNeedsFood]
//only do if this is an animal
//set variable to store hunger level & set var to show hunger is in effect & start timer
IF (<NPC> == brain_animal)
TAG.hunger_lvl = <eval <FOOD>>
//prevents other scripts from triggering our events
TAG.hunger_in_effect = 1
TIMERF <eval <VAR.HUNGER_RATE_DECAY_TIME>> f_Hunger_handler
ENDIF
/////////////////////////////////////////////////////
//// FUNCTION f_Hunger_handler
//// -recursive routine
//// -reduces hunger lvl by 1 each interval
//// -stops loop and fires @HUNGER event of e_Hunger (events) above
/////////////////////////////////////////////////////
[FUNCTION f_Hunger_handler]
//reduce hunger lvl
I.TAG.hunger_lvl -= 1
//if hungry fire hunger trigger and exit otherwise run timer-func again
IF (<eval <I.TAG.hunger_lvl>> <= 0)
//trigger hunger event
I.TRIGGER @HUNGER
return
ENDIF
TIMERF <eval <VAR.HUNGER_RATE_DECAY_TIME>> f_Hunger_handler
/////////////////////////////////////////////////////
I have left room for adding other behaviors in [EVENTS e_Hunger] if desired.
Now I never know if that dam bear is gunna attack me or not!
Thanks to everyone!
Capes
(This post was last modified: 03-17-2018 10:46 PM by Capes.)
|
|
03-17-2018 10:36 PM |
|
The following 1 user Likes Capes's post:1 user Likes Capes's post
Berkley (02-18-2020)
|