Files
vgstation13/code/modules/unit_tests/reagent_recipe_collisions.dm
DamianX 191b694b95 Unit testing (#18329)
* Unit testing

* Fixes
2018-05-31 08:32:18 -03:00

68 lines
3.1 KiB
Plaintext

/datum/unit_test/reagent_recipe_collisions
/datum/unit_test/reagent_recipe_collisions/start()
var/datum/reagents/r = new // Builds chemical_reactions_list
qdel(r)
r = null
var/list/reactions = list()
for(var/V in global.chemical_reactions_list)
reactions += global.chemical_reactions_list[V]
for(var/i in 1 to (reactions.len-1))
for(var/i2 in (i+1) to reactions.len)
var/datum/chemical_reaction/r1 = reactions[i]
var/datum/chemical_reaction/r2 = reactions[i2]
if(recipes_do_conflict(r1, r2))
fail("Chemical recipe conflict between [r1.type] and [r2.type]")
/datum/unit_test/reagent_recipe_collisions/proc/recipes_do_conflict(datum/chemical_reaction/r1, datum/chemical_reaction/r2)
//do the non-list tests first, because they are cheaper
if(r1.required_container != r2.required_container)
return FALSE
if(r1.is_cold_recipe == r2.is_cold_recipe)
if(r1.required_temp != r2.required_temp)
//one reaction requires a more extreme temperature than the other, so there is no conflict
return FALSE
else
var/datum/chemical_reaction/cold_one = r1.is_cold_recipe ? r1 : r2
var/datum/chemical_reaction/warm_one = r1.is_cold_recipe ? r2 : r1
if(warm_one.required_temp == 0 || cold_one.required_temp < warm_one.required_temp)
// the warm reaction doesn't require any particular temperature or the range of temperatures does not overlap, so there is no conflict
return FALSE
//find the reactions with the shorter and longer required_reagents list
var/datum/chemical_reaction/long_req
var/datum/chemical_reaction/short_req
if(r1.required_reagents.len > r2.required_reagents.len)
long_req = r1
short_req = r2
else if(r1.required_reagents.len < r2.required_reagents.len)
long_req = r2
short_req = r1
else
//if they are the same length, sort instead by the length of the catalyst list
//this is important if the required_reagents lists are the same
if(r1.required_catalysts.len > r2.required_catalysts.len)
long_req = r1
short_req = r2
else
long_req = r2
short_req = r1
//check if the shorter reaction list is a subset of the longer one
var/list/overlap = r1.required_reagents & r2.required_reagents
if(overlap.len != short_req.required_reagents.len)
//there is at least one reagent in the short list that is not in the long list, so there is no conflict
return FALSE
//check to see if the shorter reaction's catalyst list is also a subset of the longer reaction's catalyst list
//if the longer reaction's catalyst list is a subset of the shorter ones, that is fine
//if the reaction lists are the same, the short reaction will have the shorter required_catalysts list, so it will register as a conflict
var/list/short_minus_long_catalysts = short_req.required_catalysts - long_req.required_catalysts
if(short_minus_long_catalysts.len)
//there is at least one unique catalyst for the short reaction, so there is no conflict
return FALSE
//if we got this far, the longer reaction will be impossible to create if the shorter one is earlier in global.chemical_reactions_list, and will require the reagents to be added in a particular order otherwise
return TRUE