Advertisement
skroton

ZDoom Factions Infighting Version

May 29th, 2016
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.90 KB | None | 0 0
  1. #library "FACTION"
  2.  
  3. #include "zcommon.acs"
  4.  
  5. #libdefine MAX_SIGNED_LONG 2147483647
  6.  
  7. #libdefine NUM_FACTIONS 50
  8.  
  9. global int 51:faction_a[];  /* 0-63 = Player TIDs, 100 onwards = faction indexes and tids */
  10.  
  11. int total_faction = (NUM_FACTIONS - 1); /* counts 0 as first faction, so for instance if there are 10 total factions this should be 9 */
  12.  
  13. int faction_indexes[NUM_FACTIONS] = { 0 }; /* index locations for global array management */
  14.                            
  15. int targets[200][2]; /* array of targets to be used for targets in script; map level because compatibility and really no difference */
  16.  
  17. str faction_s[50] = { "NULL" }; /* string array to store class names for assigning factions based on class names */
  18.  
  19. /* below function from ijon tichy's commonFuncs.h */
  20. function int magnitudeThree_f(int x, int y, int z)
  21. {
  22.     int len, ang;
  23.  
  24.     ang = VectorAngle(x, y);
  25.     if (((ang + 0.125) % 0.5) > 0.25) { len = FixedDiv(y, sin(ang)); }
  26.     else { len = FixedDiv(x, cos(ang)); }
  27.  
  28.     ang = VectorAngle(len, z);
  29.     if (((ang + 0.125) % 0.5) > 0.25) { len = FixedDiv(z, sin(ang)); }
  30.     else { len = FixedDiv(len, cos(ang)); }
  31.  
  32.     return len;
  33. }
  34.  
  35. /* below function from ijon tichy's commonFuncs.h */
  36. function int distance_tid(int tid1, int tid2)
  37. {
  38.     int x1 = GetActorX(tid1);
  39.     int y1 = GetActorY(tid1);
  40.     int z1 = GetActorZ(tid1);
  41.  
  42.     int x2 = GetActorX(tid2);
  43.     int y2 = GetActorY(tid2);
  44.     int z2 = GetActorZ(tid2);
  45.  
  46.     return magnitudeThree_f(x2-x1, y2-y1, z2-z1);
  47. }
  48.  
  49. function int setup_array_index (void) /* function to set up array index before being used */
  50. {
  51.   if(faction_indexes[0] == 100){ return 0; } /* if faction array is already set up, don't set it up */
  52.  
  53.   if(GetCVar("faction_debug_msg")){ log(s:"initializing faction index array"); } /* debugging log */
  54.  
  55.   for(int i = 0; i <= total_faction; i++){ /* for all faction array elements */
  56.     if(GetCVar("faction_debug_msg")){ log(s:"index for faction ", i:i, s:" set to ", i:100 + (i * 5)); } /* debugging log */
  57.     faction_indexes[i] = 100 + (i * 5); } /* create index locations starting at 100, 5 apart */
  58.   return 0;
  59. }
  60.  
  61. function int store_faction_tid (int faction, int tid) /* faction to place tid, tid to place */
  62. {
  63.   int index, index_loc; /* index for faction global array, location of faction index in faction tid global array */
  64.   index_loc = faction_indexes[faction]; /* grab index location for faction from map index array */
  65.   index = faction_a[index_loc]; /* grab index for faction from map array */
  66.  
  67.   if(GetCVar("faction_debug_msg")){ log(s:"storing tid ", i: tid, s:" for faction ", i: faction, s:" in location ", i:(index_loc + index)); } /* debugging log */
  68.  
  69.   faction_a[index_loc + index + 1] = tid; /* store the tid in the proper location in the faction tid array */
  70.   faction_a[index_loc] = index + 1; /* increment the faction index by one to track that we added new tid to faction tid array */
  71.  
  72.   if(GetCVar("faction_debug_msg")){ log(s:"faction ", i: faction, s:" index updated to ", i: faction_a[index_loc]); } /* debugging log */
  73.  
  74.   return 0;
  75. }
  76.  
  77. function int get_faction_tid (int faction, int loc) /* faction to get tid from, location in array */
  78. {
  79.   int index_loc; /* location of faction index in faction tid global array */
  80.   index_loc = faction_indexes[faction]; /* grab index location for faction from map index array */
  81.   return faction_a[index_loc + loc]; /* return the tid stored in the specified location */
  82. }
  83.  
  84. function int get_array_location (int faction, int loc) /* faction, location in faction array */
  85. {
  86.   return faction_indexes[faction] + loc; /* return the specified location */
  87. }
  88.  
  89. function int manage_faction_array (int faction) /* faction in array to manage */
  90. {
  91.   /* index for faction global array, location of faction index in faction tid global array, location of index after this one */
  92.   int index, index_loc, index_loc_next;
  93.  
  94.   /* loop variable, end of faction array, location of last faction index */
  95.   int i, faction_end, last_faction;
  96.  
  97.   index_loc = faction_indexes[faction]; /* grab index location for faction from map index array */
  98.   index_loc_next = faction_indexes[faction + 1]; /* grab next index location for faction from map index array */
  99.  
  100.   index = faction_a[index_loc]; /* grab index for faction from map array */
  101.   if(index > /* if the number of tids stored in the specified faction array */
  102.    (index_loc_next - index_loc - 3 )){ /* is within 2 elements of encroaching on next array */
  103.     last_faction = faction_indexes[total_faction]; /* get the location of the index for the last faction */
  104.    
  105.     if(GetCVar("faction_debug_msg")){ log(s:"faction ", i:faction, s:" is full so factions ", i:faction+1, s:" and onward are moving up 5 spaces in the array."); } /* debugging log */
  106.  
  107.     faction_end = last_faction + faction_a[last_faction] + 1; /* get the last element locaction in the array */
  108.  
  109.     for(i = faction_end; i >= index_loc_next; i-- ){  /* working backwards, move everything in array forward by 5 */
  110.       faction_a[i + 5] = faction_a[i];
  111.       faction_a[i] = 0; } /* zero out array element onced moved */
  112.      
  113.     for(i = (faction + 1); i <= total_faction; i++ ){  /* update all faction indexes, starting with one after the managed faction */
  114.       faction_indexes[i] += 5; }}/* increase faction index at i by 5 to make room for managed faction */
  115.  
  116.   return 0;
  117. }
  118.  
  119. function int get_faction(int tid)
  120. {
  121.   if(tid){ /* if the tid is not 0 */
  122.     return (CheckActorInventory(tid,"faction_counter") - 1); } /* return how many faction counters - 1 the actor with tid has (what their faction is)  */
  123.   return (CheckInventory("faction_counter") - 1); /* return how many faction counters - 1 the activator has (what their faction is) */
  124. }
  125.  
  126. function int set_faction(int tid, int faction)
  127. {
  128.   TakeInventory("faction_counter", MAX_SIGNED_LONG); /* remove any faction counters the actor might have, even though they shouldn't */
  129.   GiveInventory("faction_counter", faction + 1); /* give them a number of counters corresponding to their faction + 1, so we can know if they have a faction set even if faction is 0 */
  130.   return (CheckActorInventory(tid,"faction_counter") - 1);
  131. }
  132.  
  133. function int clear_targets (void) /* function for clearing out targets in target array */
  134. {
  135.   for(int i = 0; i < 200; i++){ /* for all target positions in array */
  136.     targets[i][0] = 0; /* set all tids in array to 0 */
  137.     targets[i][1] = 0;} /* set all distances in array to 0 */
  138.   return 0;
  139. }
  140.  
  141. function int store_target (int tid, int dist) /* function for clearing out targets in target array */
  142. {
  143.   int index; /* index for the target array */
  144.   index = targets[0][0]; /* get the index */
  145.   targets[0][0] += 1; /* update index to reflect that we added another target to array */
  146.   targets[index + 1][0] = tid; /* store the tid for the target in the array */
  147.   targets[index + 1][1] = dist; /* store the distance for the target in the array */
  148.   return targets[index + 1][0] == tid && targets[index +1][1] == dist; /* return if target is stored in array */
  149. }
  150.  
  151. script "start_fights" OPEN /* script to give every monster an item to get them to have factions */
  152. {
  153.   int xCount, yCount;
  154.  
  155.   /* from http://forum.zdoom.org/viewtopic.php?p=845858#p845858 */
  156.   for(xCount=-6; xCount<7; xCount++){
  157.    for(yCount=-6; yCount<7; yCount++){
  158.       SpawnForced("RadiusGiveActorFaction", 335544320*xCount, 335544320*yCount, 0, 0, 0); }}
  159.  
  160.   Delay(5);
  161.  
  162.   for(xCount=-6; xCount<7; xCount++){
  163.    for(yCount=-6; yCount<7; yCount++){
  164.       SpawnForced("RadiusGiveActorFactionLook", 335544320*xCount, 335544320*yCount, 0, 0, 0); }}
  165.  
  166.   Delay(105);
  167.   restart;
  168. }
  169.  
  170. script "player_tid" ENTER /* player tid script */
  171. {
  172.   int player_num, cur_tid, new_tid; /* player number, current tid, new tid */
  173.  
  174.   while(true){ /* script will loop continuously */
  175.     player_num = PlayerNumber(); /* get player number */
  176.     cur_tid = ActivatorTID(); /* get player current tid */
  177.     if(!faction_a[player_num] /* IF there's no value for the player tid in the array */
  178.      ||  !ActivatorTID() /* OR the player currently has no tid or tid is 0 */
  179.      || ThingCount(T_NONE,cur_tid) > 1){ /* OR more than one thing has the same tid as the player */
  180.       new_tid = UniqueTID(); /* get a new unique tid */
  181.       faction_a[player_num] = new_tid; /* put it in the array */
  182.       Thing_ChangeTid(0, new_tid); } /* give the player the new tid */
  183.     delay(1); } /* 1 tic delay for loop, means we can be somewhat sure players tids won't have issues */
  184. }
  185.  
  186. script "get_faction_start" (void) /* script to sort out what faction actor should belong to */
  187. {
  188.   int faction, i; /* faction to put actor into, loop variable */
  189.   str class; /* actor class */
  190.  
  191.   class = GetActorClass(0); /* get actor class */
  192.  
  193.   faction = -1; /* set faction to -1 for loop below */
  194.  
  195.   for(i = 0; i < 50 && faction < 0; i++){ /* for all strings in array, so long as a match or NULL string hasn't been found */
  196.     if(StrCmp(class, faction_s[i]) == 0){ /* if found a match with class name */
  197.       faction = i; } /* set faction to i */
  198.     else if(StrCmp("NULL", faction_s[i]) == 0){ /* if found a NULL string */
  199.       faction = i; /* set faction to i */
  200.       faction_s[i] = class; }} /* store class name to array so others with same class name will be grouped to faction */
  201.  
  202.   ACS_NamedExecuteWithResult("faction_tid_start", faction);
  203. }
  204.  
  205. script "faction_tid_start" (int faction) /* script to give actor unique tid and store it in faction array and on actor */
  206. {
  207.   int new_tid; /* new tid */
  208.  
  209.   if(CheckInventory("faction_counter")){ terminate; } /* if they have already run this script, stop */
  210.  
  211.   setup_array_index(); /* make sure the array index has already been set up */
  212.  
  213.   if(faction < total_faction){ /* if faction isn't last faction, which doesn't need to be managed really */
  214.     manage_faction_array(faction); } /* make sure there's enough room in the faction */
  215.   new_tid = UniqueTID(); /* get a new unique tid */
  216.   store_faction_tid(faction, new_tid); /* put it in the faction array */
  217.   Thing_ChangeTid(0, new_tid); /* give the activator the new tid */
  218.   set_faction(0, faction); /* set the faction on the actor with inventory counter items */
  219. }
  220.  
  221. script "faction_look" (int enemy_faction, int flags, int max_dist, int min_dist_wakeup) /* script for actor to look for members of enemy factions */
  222. {
  223.   /* activator tid, target tid, location in faction array, faction index location, faction index */
  224.   int act_tid, tar_tid, loc, index_loc, index;
  225.  
  226.   /* faction of activator, faction currently checking, if found target */
  227.   int faction, check_faction, found_target;
  228.  
  229.   /* loop variable, distance for loops, flag for player target finding being done */
  230.   int i, dist, play_tar;
  231.  
  232.   /* player and target flags */
  233.   int play_flag, tar_flag;
  234.  
  235.   if(flags){ /* if any flag has been set */
  236.     play_flag = flags % 10; /* extract play_flag from flags */
  237.     tar_flag = flags - play_flag; } /* extract tar_flag from flags */
  238.  
  239.   /*
  240.   --player flags
  241.   0 won't target players
  242.   1 process players like other targets
  243.   2 prioritize player targetsd
  244.  
  245.   --target processsing flags
  246.   0 pick first valid target
  247.   1 pick random target from valid targets
  248.   2 pick closest target
  249.   */
  250.  
  251.   clear_targets(); /* clear targets in target array prior to use */
  252.  
  253.   max_dist = max_dist << 16; /* convert to fixed point */
  254.   min_dist_wakeup = min_dist_wakeup << 16; /* convert to fixed point */
  255.  
  256.   faction = get_faction(0); /* get faction of activator */
  257.  
  258.   act_tid = ActivatorTID(); /* get activator tid */
  259.  
  260.   /* minimum distance to player check */
  261.  
  262.   if(min_dist_wakeup){ /* if a minimum distance to a player to wake up is set */
  263.     dist = 0; /* set dist to 0 */
  264.     /* for all player tids in array, and while no player close enough has been found */
  265.     for(i = 0; i < 64 && !dist; i++){
  266.       if(faction_a[i]){ /* if there is actually a tid stored in the the noted location */
  267.         if(GetCVar("faction_debug_msg")){ log(s:"player ", i:i, s:" with tid ", i:faction_a[i], s:" is ", f:distance_tid(0, faction_a[i]), s:" map units away "); } /* debugging log */
  268.         dist = distance_tid(0, faction_a[i]) <= min_dist_wakeup; /* check if player is at least as close as min distance, will exit loop if true */
  269.         if(dist){ /* if the player is at least as close to activator as min distance */
  270.           if(GetCVar("faction_debug_msg")){ log(s:"player ", i:i, s:" with tid ", i:faction_a[i], s:" is close enough."); }}}}} /* debugging log */
  271.  
  272.   if(!dist && min_dist_wakeup){ /* if min distance to player check was unsuccessful */
  273.     terminate; } /* stop looking */
  274.  
  275.  
  276.   dist = 0; /* reset dist for next loop */
  277.  
  278.   if(play_flag){ /* if player flags instruct to look for player targets as well */
  279.     for(i = 0; i < 64 && /* for all player tids stored in the array, and so long as */
  280.      ( (!found_target && !tar_flag) || /* no target has been found and we are going with the first target OR */
  281.      (tar_flag) ) ; i++){ /* we are storing targets to sort through later */
  282.       tar_tid = faction_a[i]; /* grab tid stored in the the noted location */
  283.       if( tar_tid && /* if actually retrieved target tid AND */
  284.        CheckSight(0, tar_tid, 0) ){ /* if activator can see this member of enemy faction */
  285.         dist = distance_tid(0, tar_tid); /* get distance between the activator and the target */
  286.         if(!max_dist || /* there is no check for max distance OR */
  287.          (dist <= max_dist) ){ /* the target is within max distance */
  288.           store_target(tar_tid, dist); /* store the target in the array */
  289.           if(play_flag == 2){ /* if prioritizing player flags */
  290.             play_tar = 1; } /* note that a player target has been found */
  291.           found_target = 1; }}}} /* flag that a target has been found */
  292.  
  293.  
  294.   while( (check_faction <= total_faction) && /* while all factions haven't been checked yet AND */
  295.    (!play_tar) && /* players targetting hasn't resulted in final target AND */
  296.    ( (!found_target && !tar_flag) || /* no target has been found and we are going with the first target OR */
  297.    (tar_flag) ) ){ /* we are storing targets to sort through later */
  298.     if( (check_faction != faction) && /* don't look for targets in the monster's own faction AND */
  299.     ( (check_faction == enemy_faction) || /* if the faction being checked is the same as the enemy_faction OR */
  300.     (enemy_faction < 0) ) ){ /* no enemy faction was specified */
  301.       loc = 1; /* set faction location to 1, normally 0 would be first in array but faction index takes up [0] spot */
  302.       index_loc = faction_indexes[check_faction]; /* grab index location for faction from map index array */
  303.       index = faction_a[index_loc]; /* grab index for faction from map array */
  304.       while(loc <= index /* while we haven't checked all entries in faction */
  305.        && ( (!found_target && !tar_flag) || /* no target has been found and we are going with the first target OR */
  306.        (tar_flag) ) ){ /* we are storing targets to sort through later */
  307.         tar_tid = get_faction_tid(check_faction, loc); /* get next faction tid */
  308.         if( tar_tid && /* if actually retrieved target tid AND */
  309.          CheckSight(0, tar_tid, 0) ){ /* if activator can see this member of enemy faction */
  310.           dist = distance_tid(0, tar_tid); /* get distance between the activator and the target */
  311.           if(!max_dist || /* there is no check for max distance OR */
  312.            (dist <= max_dist) ){ /* the target is within max distance */
  313.             store_target(tar_tid, dist); /* store the target in the array */
  314.             found_target = 1; }} /* flag that a target has been found */
  315.         ++loc; }} /* check next location in array */
  316.     ++check_faction; } /* check next faction in array */
  317.  
  318.  
  319.   if(found_target){ /* if a target was successfully found */
  320.  
  321.     if(!tar_flag){ /* if we are going with the first target found */
  322.       tar_tid = targets[1][0]; } /* set the target tid to the first tid in the array */
  323.      
  324.     else if(tar_flag == 1){ /* if we are picking a random target from available targets */
  325.       tar_tid = targets[random(1, targets[0][0])][0]; } /* randomly select from 1 to the total number of targets in the array */
  326.      
  327.     else if(tar_flag == 2){ /* if we are picking the closest to attck */
  328.       dist = 0; /* set distance variable to 0 for the folloing loop */
  329.       for(i = 1; i <= targets[0][0]; i++){ /* for all tids stored in target array, up to the last stored */
  330.         if(targets[i][1] < dist || dist == 0){ /* if distance of potential target is smaller than current smallest, or first checked */
  331.           tar_tid = i; /* store current i, using tar_tid as temp */
  332.           dist = targets[i][1]; }} /* store distance for selection as new smallest distance */
  333.       tar_tid = targets[tar_tid][0]; } /* set tid for target with lowest distance to final target */
  334.          
  335.     if(GetCVar("faction_debug_msg")){ log(s:GetActorClass(0), s:" in faction ", i: faction, s:" found a target:", i:tar_tid, s:" which is a ", s:GetActorClass(tar_tid), s:" in faction " , i: check_faction - 1); } /* debugging log */
  336.     Thing_Hate(0, tar_tid, 0); } /* make activator go after target */
  337. }
  338.  
  339.  
  340. //script "faction_look_decorate" (void) /* script to check if monster already has target before making it hate new one to */
  341. //{
  342. //  if(GetActorProperty(0, APROP_TargetTID)){ /* if activator has a target already */
  343. //    ACS_NamedExecuteWithResult("faction_look"); } /* start the faction_look script */
  344. //}
  345.  
  346. script "clear_faction_array" UNLOADING /* script to clear faction array for next map */
  347. {
  348.  
  349.   /* loop variable, start of faction array, end of faction array, location of last faction index */
  350.   int i, faction_start, faction_end, last_faction;
  351.  
  352.   last_faction = faction_indexes[total_faction]; /* get the location of the index for the last faction */
  353.   faction_start = faction_indexes[0]; /* get the location of the index for the first faction, which is also beginning of array */
  354.   faction_end = last_faction + faction_a[last_faction] + 1; /* get the last element locaction in the array */
  355.  
  356.   for(i = faction_start; i <= faction_end; i++ ){ faction_a[i] = 0; }  /* clear all values used for faction in the global array */
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement