OSP Code Optimisation System of array handling scripts/functions

Users who are viewing this thread

This is a mirror of a post of the OSP Author Fujiwara at the Python script/scheme/source exchange thread of the old MBX Forum. There is a thread with discussion alongside it, Fuijwara's Array Script. Additional I want to point here to the Large arrays script by Rongar at the PYTHON SCRIPT/SCHEME EXCHANGE thread which is based on Fujiwara's arrays idea and to the Prototype for creating dynamic arrays within the scripting system by sphere. The rest is now quoted of the original thread.

=====

Others may or may not have implemented a system of array handling scripts/functions, but I offer these up anyway. These came out of a conversation at Taleworlds with Neophyte. Arrays are 0-based, since they come out of the slot system. They're free to do with as you please, but a mention would be nice :smile:

The functions:
  • array_init: creates a new array of user-determined length, later stored in the array itself. Also initializes the array to the value array_value_null. Returns to $return. Takes a length value as input
  • array_clear: clears the input array of all values, returning them to array_value_null. Returns the array to $return. Takes an array as input
  • array_push: pushes the input value onto the top of the array (highest available, non-null index). Stores the pushed value to array_value_last_pushed. Takes an array and the value to be pushed as input
  • array_pop: pops the value off the top of array and removes it from the array (value replaced with array_value_null), Stores value at array_value_last_popped. Returns to $return. Takes an array as input
  • array_peek: returns the value at the given index without removing it from the array. Takes an array and an index as input
  • array_queue: returns and removes the value at the bottom of the array, then shifts all other values left/down one index. Assigns queued value to array_value_last_queued and $return. Takes an array as input
  • array_remove_if_ge: resets any values in the array to array_value_null that is >= the test value. Written as a proof of concept, since the whole suite of comparator functions can be easily implemented. Takes an array and a test value as input
Notes: in this implementation, the first five slots of the array are reserved, and inputted index values are offset by 5 . Obviously, if you need more reserved slots, the index offset should be changed.

Required:

module_constants:
Code:
array_value_null = -100
array_value_length = 0
array_value_last_popped = 1
array_value_last_pushed = 2
array_value_last_queued = 3
array_value_high_index = 4

module_party_templates:
Code:
('array','array',pf_disabled,0,fac_neutral,0,[]),

module_scripts:
Code:
("array_init",
   [
       (store_script_param_1,":array_length"),
       (store_random_in_range,reg1,cities_begin,cities_end),
       (set_spawn_radius,20),
       (spawn_around_party,reg1,'pt_array'),
       (val_add,':array_length',5),
       (call_script,'script_array_clear',reg0),
       ]),
  ('array_clear',
   [
       (store_script_param_1,':input_array'),
       (party_get_slot,':array_length',':input_array',array_value_length),
       (try_for_range,reg1,1,':array_length'),
           (party_set_slot,':input_array',reg1,array_value_null),
       (try_end),
       (party_set_slot,':input_array',array_value_length,':array_length'),
       (party_set_slot,':input_array',array_value_high_index,5),
       (assign,'$return',':input_array'),
       ]),
  ('array_push',
   [
       (store_script_param_1,':input_array'),
       (store_script_param_2,':input_value'),
       (party_get_slot,reg1,':input_array',array_value_high_index),
       (val_add,reg1,1),
       (party_set_slot,':input_array',reg1,':input_value'),
       (party_set_slot,':input_array',array_value_high_index,reg1),
       (party_set_slot,':input_array',array_value_last_pushed,':input_value'),
       ]),
  ('array_pop',
   [
       (store_script_param_1,':input_array'),
       (party_get_slot,reg1,':input_array',array_value_high_index),
       (party_get_slot,reg2,':input_array',reg1),
       (party_set_slot,':input_array',reg1,-1),
       (party_set_slot,':input_array',array_value_last_popped,reg2),
       (try_begin),
           (gt,reg1,5),
           (val_sub,reg1,1),
       (try_end),
       (party_set_slot,':input_array',array_value_high_index,reg1),
       (assign,'$return',reg2),
       ]),
  ('array_peek',
   [
       (store_script_param_1,':input_array'),
       (store_script_param_2,':input_index'),
       (val_add,':input_index',5),
       (party_get_slot,reg1,':input_array',':input_index'),
       (assign,'$return',reg1),
       ]),
  ('array_queue',
   [
       (store_script_param_1,':input_array'),
       (party_get_slot,reg1,':input_array',5),
       (assign,'$return',reg1),
       (party_set_slot,':input_array',array_value_last_queued,reg1),
       (call_script,'script_array_shift_left',':input_array'),
       ]),
  ('array_shift_left',
   [
       (store_script_param_1,':input_array'),
       (party_get_slot,':array_length',':input_array',array_value_length),
       (try_for_range,reg1,5,':array_length'),
           (store_add,reg2,reg1,1),
           (party_get_slot,reg3,':input_array',reg2),
           (party_set_slot,':input_array',reg1,reg3),
       (try_end),
       (party_set_slot,':input_array',':array_length',array_value_null),
       ]),
  ('array_remove_if_ge',
   [
       (store_script_param_1,':input_array'),
       (store_script_param_2,':test_value'),
       (party_get_slot,reg1,':input_array',array_value_high_index),
       (try_for_range,reg2,5,reg1),
           (party_get_slot,reg3,':input_array',reg2),
           (ge,reg3,':test_value'),
           (party_set_slot,':input_array',reg2,array_value_null),
       (try_end),
       ]),

If any notices any glaring errors, please let me know :smile:

Reminder: The OSP Author is
Fujiwara
 
Back
Top Bottom