# "script_find_mining_data"
# Finds dynamic vein information in resource storage troop and creates a new record if necessary.
# INPUT:
# arg1 = scene_id where mining takes place
# arg2 = vein_id for resource prop (2nd variation number)
# arg3 = default resource amount in vein
# arg4 = vein full recovery time (MUST be > 0)
# OUTPUT:
# reg0 = amount of resource in vein at current moment
# reg1 = vein data offset in "trp_mining_storage"
("find_mining_data", [
(store_script_param, ":scene_id", 1),
(store_script_param, ":vein_id", 2),
(store_script_param, ":resource_max", 3),
(store_script_param, ":recovery_time", 4),
(store_current_hours, ":current_time"),
(assign, ":found", 0),
(assign, ":max_range", "$g_mining_veins_count"),
(try_for_range, ":slot_id", 0, ":max_range"),
(val_lshift, ":slot_id", 2),
# Check it's the same scene
(troop_slot_eq, mining_storage, ":slot_id", ":scene_id"),
(val_add, ":slot_id", 1),
# Check it's the same vein
(troop_slot_eq, mining_storage, ":slot_id", ":vein_id"),
(val_add, ":slot_id", 1),
# Load data for recovery calculation
(troop_get_slot, ":previous_time", mining_storage, ":slot_id"),
(troop_set_slot, mining_storage, ":slot_id", ":current_time"),
# Remember current slot for script output
(assign, reg1, ":slot_id"),
# Load vein resource amount and apply recovery if necessary
(val_add, ":slot_id", 1),
(troop_get_slot, ":previous_qty", mining_storage, ":slot_id"),
# Calculate vein recovery, depending on how much time has passed
(try_begin),
# If instant recovery (recovery time < 0)
(lt, ":recovery_time", 0),
(assign, reg0, ":resource_max"),
(else_try),
# If gradual recovery (recovery time > 0)
(gt, ":recovery_time", 0),
(store_sub, ":recover_qty", ":current_time", ":previous_time"),
(val_mul, ":recover_qty", ":resource_max"),
(val_div, ":recover_qty", ":recovery_time"),
(store_add, reg0, ":previous_qty", ":recover_qty"),
(val_min, reg0, ":resource_max"),
(else_try),
# If no recovery (recovery time = 0)
(assign, reg0, ":previous_qty"),
(try_end),
(troop_set_slot, mining_storage, ":slot_id", reg0),
# Set found flag so we don't create a new record
(assign, ":found", 1),
# End try_for_range cycle immediately
(assign, ":max_range", 0),
(try_end),
(try_begin),
# If we haven't found vein record, create a new one
(eq, ":found", 0),
(store_mul, reg1, "$g_mining_veins_count", 4),
(val_add, "$g_mining_veins_count", 1),
(troop_set_slot, mining_storage, reg1, ":scene_id"),
(val_add, reg1, 1),
(troop_set_slot, mining_storage, reg1, ":vein_id"),
(val_add, reg1, 2),
(troop_set_slot, mining_storage, reg1, ":resource_max"),
(val_sub, reg1, 1),
(troop_set_slot, mining_storage, reg1, ":current_time"),
(assign, reg0, ":resource_max"),
(try_end),
]),
# "script_initialize_mining_prop"
# Initializes scene_prop instance as a mineable vein.
# INPUT:
# arg1 = scene prop instance id
# arg2 = resource item_id
# arg3 = number of successful hits to yield a single resource batch, 0 if resource cannot be mined with hits
# arg4 = amount of resource yielded in a single batch
# arg5 = vein size in resource units
# arg6 = vein full recovery time
# OUTPUT:
# None
("initialize_mining_prop", [
(store_script_param, ":instance_id", 1),
(store_script_param, ":resource_id", 2),
(store_script_param, ":hits_per_batch", 3),
(store_script_param, ":haul_per_batch", 4),
(store_script_param, ":resource_max", 5),
(store_script_param, ":recovery_time", 6),
(store_current_scene, ":scene_id"),
(prop_instance_get_variation_id_2, ":vein_id", ":instance_id"),
(call_script, "script_find_mining_data", ":scene_id", ":vein_id", ":resource_max", ":recovery_time"),
(scene_prop_set_slot, ":instance_id", slot_prop_data_offset, reg1),
(scene_prop_set_slot, ":instance_id", slot_prop_resource_id, ":resource_id"),
(scene_prop_set_slot, ":instance_id", slot_prop_hits_per_batch, ":hits_per_batch"),
(scene_prop_set_slot, ":instance_id", slot_prop_haul_per_batch, ":haul_per_batch"),
#(scene_prop_set_slot, ":instance_id", slot_prop_resource_max, ":resource_max"),
(scene_prop_set_slot, ":instance_id", slot_prop_recovery_time, ":recovery_time"),
(scene_prop_set_slot, ":instance_id", slot_prop_current_hits, 0),
(try_begin),
(eq, reg0, 0), # Vein is empty
(scene_prop_set_hit_points, ":instance_id", 0),
(else_try),
(gt, ":hits_per_batch", 0),
(store_mul, ":hp", ":hits_per_batch", 1000),
(scene_prop_set_hit_points, ":instance_id", ":hp"),
(else_try),
(scene_prop_set_hit_points, ":instance_id", 1000),
(try_end),
]),
# "script_cf_check_mining_tool"
#
# INPUT:
# arg1 = mining scene_prop instance_id
# arg2 = agent_id who is doing the mining
# OUTPUT:
# Fails or succeeds depending on check result
("cf_check_mining_tool", [
#(store_script_param, ":instance_id", 1),
#(store_script_param, ":agent_id", 2),
#(scene_prop_get_slot, ":resource_id", ":instance_id", slot_prop_resource_id),
#(agent_get_wielded_item, ":tool_id", ":agent_id", 0),
(assign, ":allowed", 0),
(try_begin),
# Insert any code to check for tool/resource match here and set ":allowed" to 1 if successful.
(assign, ":allowed", 1),
(try_end),
(eq, ":allowed", 1),
]),
# "script_process_resource_mining"
#
# INPUT:
# arg1 = mining scene_prop instance_id
# arg2 = agent_id who is doing the mining
# arg3 = damage delivered by agent when mining (pass 0 when mining by use)
# OUTPUT:
# None
("process_resource_mining", [
(store_script_param, ":instance_id", 1),
(store_script_param, ":agent_id", 2),
(store_script_param, ":damage", 3),
(scene_prop_get_slot, ":hits_per_batch", ":instance_id", slot_prop_hits_per_batch),
(assign, ":haul", 1),
(try_begin),
(gt, ":hits_per_batch", 0),
(scene_prop_get_slot, ":current_hits", ":instance_id", slot_prop_current_hits),
(val_add, ":current_hits", 1),
(try_begin),
(ge, ":current_hits", ":hits_per_batch"),
(scene_prop_set_slot, ":instance_id", slot_prop_current_hits, 0),
(store_mul, ":hp", ":hits_per_batch", 1000),
(else_try),
(scene_prop_set_slot, ":instance_id", slot_prop_current_hits, ":current_hits"),
(store_sub, ":hp", ":hits_per_batch", ":current_hits"),
(val_mul, ":hp", 1000),
(assign, ":haul", 0),
(try_end),
(else_try),
(assign, ":hp", 1000),
(try_end),
(val_add, ":hp", ":damage"), # Compensate for damage from current strike
(scene_prop_set_cur_hit_points, ":instance_id", ":hp"),
(try_begin),
(eq, ":haul", 1),
(scene_prop_get_slot, ":resource_id", ":instance_id", slot_prop_resource_id),
(scene_prop_get_slot, ":haul", ":instance_id", slot_prop_haul_per_batch),
(scene_prop_get_slot, ":slot_id", ":instance_id", slot_prop_data_offset),
(val_add, ":slot_id", 1),
(troop_get_slot, ":resource_qty", mining_storage, ":slot_id"),
(val_min, ":haul", ":resource_qty"), # We cannot mine more than what's currently there
(val_sub, ":resource_qty", ":haul"),
(troop_set_slot, mining_storage, ":slot_id", ":resource_qty"), # Write down what remains in the vein
(try_begin),
(gt, ":haul", 0),
(call_script, "script_resource_mined", ":agent_id", ":resource_id", ":haul"),
(try_end),
(try_begin),
(eq, ":resource_qty", 0),
(scene_prop_set_cur_hit_points, ":instance_id", 0),
(call_script, "script_resource_mined_out", ":instance_id", ":agent_id", ":resource_id"),
(try_end),
(try_end),
]),
# "script_resource_mined"
# Process actual mining success (give some resource to miner).
# INPUT:
# arg1 = agent_id who has mined some resource
# arg2 = item_id of resource being mined
# arg3 = amount of resource mined
# OUTPUT:
# None
("resource_mined", [
(store_script_param, ":agent_id", 1),
(store_script_param, ":resource_id", 2),
(store_script_param, ":amount", 3),
(assign, reg0, 0), # How much we managed to actually store (with inventory constraints in mind)
(agent_get_troop_id, ":troop_id", ":agent_id"),
(assign, ":empty_slot", 0),
(troop_get_inventory_capacity, ":capacity", ":troop_id"),
(try_for_range, ":slot_id", num_equipment_kinds, ":capacity"),
(troop_get_inventory_slot, ":item_id", ":troop_id", ":slot_id"),
(try_begin),
(eq, ":empty_slot", 0),
(eq, ":item_id", -1),
(assign, ":empty_slot", ":slot_id"),
(else_try),
# If item in this slot is the same as we have mined:
(eq, ":item_id", ":resource_id"),
(troop_inventory_slot_get_item_amount, ":qty", ":troop_id", ":slot_id"),
(troop_inventory_slot_get_item_max_amount, ":max", ":troop_id", ":slot_id"),
(store_sub, ":store", ":max", ":qty"),
(val_min, ":store", ":amount"),
# If we can store some of our resource here:
(gt, ":store", 0),
(val_add, ":qty", ":store"),
(val_add, reg0, ":store"),
(val_sub, ":amount", ":store"),
(troop_inventory_slot_set_item_amount, ":troop_id", ":slot_id", ":qty"),
# If we have nothing else to store, cancel the cycle:
(eq, ":amount", 0),
(assign, ":capacity", 0),
(try_end),
(try_end),
(try_begin),
# If we still have to store some resource and an empty slot to store it:
(gt, ":amount", 0),
(gt, ":empty_slot", 0),
(troop_set_inventory_slot, ":troop_id", ":empty_slot", ":resource_id"),
(troop_inventory_slot_set_item_amount, ":troop_id", ":empty_slot", ":amount"),
(val_add, reg0, ":amount"),
(try_end),
(str_store_item_name, s0, ":resource_id"),
(try_begin),
(eq, ":troop_id", "trp_player"),
(display_message, "@{!}You have mined {s0}[{reg0}]!"),
(else_try),
(str_store_agent_name, s1, ":agent_id"),
(display_message, "@{!}{s1} has mined {s0}[{reg0}]!"),
(try_end),
]),
# "script_resource_mined_out"
# Process mining failure (i.e. resource is mined out).
# INPUT:
# arg1 = instance_id of resource vein that's been mined out
# arg2 = agent_id who has mined out the vein
# arg3 = item_id of mined resource
# OUTPUT:
# None
("resource_mined_out", [
#(store_script_param, ":instance_id", 1),
#(store_script_param, ":agent_id", 2),
(store_script_param, ":resource_id", 3),
(str_store_item_name, s0, ":resource_id"),
(display_message, "@{!}{s0} has been mined out."),
]),