Sys_Resources_Divide error... |
So, I've been wracking my brains about a problem I've been having.
In my mod, the AI builds units on planets. I've been having a pecuiliar problem with this. Namely, that it will only build satellites one at a time...EVER...
After messing with numbers, changing various things (and swearing a lot) I tracked the issue's origination to the following function:
ITEM_TO_CONSTRUCT_UNIT_DESIGN:
set usage_rate := Sys_Get_Construction_Queue_Resource_Usage_Rate(queue_id)
if (max_per_loc > 0) then
set item_cost := Sys_Get_Vehicle_Design_Cost(item_id)
set item_number := Sys_Max_Long(Sys_Trunc(Sys_Resources_Divide(usage_rate, item_cost)), 1)
set item_amount := Sys_Min_Long(item_number, max_per_loc)
return Sys_Add_Vehicle_Design_To_Construction_Queue(queue_id, item_id, item_amount)
endif
And from there, to this:
set item_number := Sys_Max_Long(Sys_Trunc(Sys_Resources_Divide(usage_rate, item_cost)), 1)
set item_amount := Sys_Min_Long(item_number, max_per_loc)
However, the formula still seemed corret. I changed values, plugged various things in, and it STILL didn't work right. It would only build ONE satellite at a time.
I remembered that there is a DebugSettings.txt file, and decided to use it to track this down. This showed me the problem.
Here is the bit from above:
Script_AI_Construction.txt(0787): Case ((item_type(long[3]))) Script_AI_Construction.txt(0809): long[3]: TRUE Script_AI_Construction.txt(0832): Call Sys_Get_Construction_Queue_Resource_Usage_Rate(long[2870]) Script_AI_Construction.txt(0832): Set usage_rate([]) := resources[(5,060, 5,060, 5,060)] Script_AI_Construction.txt(0834): long[200] > long[0] = boolean[TRUE] Script_AI_Construction.txt(0834): If ((max_per_loc(long[200]) > long[0])) = TRUE Script_AI_Construction.txt(0835): Call Sys_Get_Vehicle_Design_Cost(long[6]) Script_AI_Construction.txt(0835): Set item_cost([]) := resources[(750, 0, 100)] Script_AI_Construction.txt(0836): Call Sys_Resources_Divide(resources[(5,060, 5,060, 5,060)], resources[(750, 0, 100)]) Script_AI_Construction.txt(0836): Call Sys_Trunc(real[0]) Script_AI_Construction.txt(0836): Call Sys_Max_Long(long[0], long[1]) Script_AI_Construction.txt(0836): Set item_number(long[0]) := long[1] Script_AI_Construction.txt(0837): Call Sys_Min_Long(long[1], long[200]) Script_AI_Construction.txt(0837): Set item_amount(long[0]) := long[1] Script_AI_Construction.txt(0838): Call Sys_Add_Vehicle_Design_To_Construction_Queue(long[2870], long[6], long[1]) Script_AI_Construction.txt(0838): Return boolean[TRUE]
And zoomed in on the specific culperate:
Script_AI_Construction.txt(0836): Call Sys_Resources_Divide(resources[(5,060, 5,060, 5,060)], resources[(750, 0, 100)]) Script_AI_Construction.txt(0836): Call Sys_Trunc(real[0]) Script_AI_Construction.txt(0836): Call Sys_Max_Long(long[0], long[1])
You see, it seems that if you use Sys_Resources_Divide, and any of the resource values in either of the operators is zero, then Sys_Resources_Divide will always return zero. ALWAYS...
After changing the small satellite (the vehicle size being used for this experiment) to have an organics value of 1, the above culperate became:
Script_AI_Construction.txt(0836): Call Sys_Resources_Divide(resources[(5,060, 5,060, 5,060)], resources[(750, 1, 100)]) Script_AI_Construction.txt(0836): Call Sys_Trunc(real[6.74666656995213]) Script_AI_Construction.txt(0836): Call Sys_Max_Long(long[6], long[1])
And so returned a correct number of constructions of 6.
The lessons:
First, if any of the values going in to Sys_Resources_Divide are zero, it will always return zero. It will not default to another resource value in the resources variable, ignoring the division by zero.
Second, until this error is corrected, make sure everything you do that is used in a Sys_Resources_Divide has a resource value of at least 1 in each resource.
"Only by being constantly at war with evil in all things, including yourself, can you truely know peace."
Download my mod here: GidMod



