RedKnight91

create_affinities()

Jul 23rd, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. ///@function create_affinity();
  2. ///@description Creates a new ds_map in global.AFFINITIES which has the instance ID as a key
  3. /// and contains all other creatures as id-affinity tuples.
  4. /// Also adds an id-affinity tuple for the main instance in every other creature's affinity ds_map
  5.  
  6. var _inst_first = id;
  7. var _inst_first_affinities = ds_map_create();
  8.  
  9. var _inst_second;
  10. var _inst_second_affinities;
  11.  
  12. var _affinity;
  13. var _devote = global.FRIEND_VALUE[friendship.devote];
  14. var _friendly = global.FRIEND_VALUE[friendship.friendly];
  15.  
  16. var _size;
  17.  
  18. //ADD MAIN INSTANCE TO OTHER CREATURES' AFFINITY DS_MAP
  19. //NOTE: Only runs through creatures which already have an entry in global.AFFINITIES
  20. _inst_second = ds_map_find_first(global.AFFINITIES);
  21. _size = ds_map_size(global.AFFINITIES);
  22.  
  23. for (var i = 0; i < _size; i++){
  24. if (_inst_second != _inst_first) {
  25. _inst_second_affinities = ds_map_find_value(global.AFFINITIES,_inst_second);
  26.  
  27. _max_affinity = (_inst_second.groupID != noone) ? _friendly : _devote;
  28. _affinity = (_inst_first.trueNeutral) ? _devote/2 : irandom_range(0, _max_affinity);
  29.  
  30. ds_map_replace(_inst_second_affinities, _inst_first, _affinity);
  31. ds_map_replace(global.AFFINITIES, _inst_second, _inst_second_affinities);
  32. }
  33.  
  34. _inst_second = ds_map_find_next(global.AFFINITIES, _inst_second);
  35. }
  36.  
  37. //CREATE CREATURE'S AFFINITIES DS_MAP
  38. with (obj_creature) {
  39. _inst_second = id;
  40. if (_inst_second != _inst_first) {
  41. //ADD OTHER CREATURES TO MAIN INSTANCE'S AFFINITY
  42. _max_affinity = (_inst_first.groupID != noone) ? _friendly : _devote;
  43. _affinity = (_inst_first.trueNeutral) ? _devote/2 : irandom_range(0, _max_affinity);
  44.  
  45. ds_map_replace(_inst_first_affinities,_inst_second,_affinity);
  46.  
  47. if (_affinity > _friendly) _inst_first.groupID = 1;
  48. }
  49. }
  50.  
  51. ds_map_add_map(global.AFFINITIES, _inst_first, _inst_first_affinities);
  52.  
  53. ds_map_destroy(_inst_first_affinities);
  54. _inst_first_affinities = -1;
  55.  
  56. if (_size > 1) {
  57. ds_map_destroy(_inst_second_affinities);
  58. _inst_second_affinities = -1;
  59. }
  60.  
  61. _inst_first.groupID = noone;
Advertisement
Add Comment
Please, Sign In to add comment