Reactions done
This commit is contained in:
parent
44e39ded34
commit
bd87ccf871
7 changed files with 63 additions and 7 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -32,6 +32,13 @@ func add_substance(data: RuntimeSubstanceData) -> void:
|
|||
recalculate_temperature()
|
||||
inventory_changed.emit(inventory.values())
|
||||
|
||||
func add_substance_no_callback(data: RuntimeSubstanceData) -> void:
|
||||
if data.substance.scientific_name in inventory:
|
||||
inventory[data.substance.scientific_name].add(data)
|
||||
else:
|
||||
inventory[data.substance.scientific_name] = data
|
||||
|
||||
|
||||
func add_inventory(substances: Array[RuntimeSubstanceData]) -> void:
|
||||
for sub in substances:
|
||||
add_substance(sub)
|
||||
|
|
@ -84,6 +91,21 @@ func extract_solid(amount: float) -> Array[RuntimeSubstanceData]:
|
|||
inventory_changed.emit(inventory.values())
|
||||
return result
|
||||
|
||||
func extract_substance(key: String, extract_amount: float) -> RuntimeSubstanceData:
|
||||
var substance: RuntimeSubstanceData = inventory[key]
|
||||
var result_substance: RuntimeSubstanceData = substance.duplicate()
|
||||
if substance.amount >= extract_amount:
|
||||
result_substance.amount = extract_amount
|
||||
inventory[result_substance.substance.scientific_name].amount -= extract_amount
|
||||
else:
|
||||
inventory[result_substance.substance.scientific_name].amount = 0
|
||||
if inventory[result_substance.substance.scientific_name].amount == 0:
|
||||
inventory.erase(result_substance.substance.scientific_name)
|
||||
|
||||
inventory_changed.emit(inventory.values())
|
||||
|
||||
return result_substance
|
||||
|
||||
func sort_by_amount(a: RuntimeSubstanceData, b: RuntimeSubstanceData) -> bool:
|
||||
return a.amount < b.amount
|
||||
|
||||
|
|
@ -108,7 +130,35 @@ func recalculate_temperature() -> void:
|
|||
mean_temperature = temperature_sum / len(inventory)
|
||||
|
||||
func check_for_reactions() -> void:
|
||||
print(ReactionRegistry.find_appropriate_reaction(inventory.values().map(runtime_to_substance),mean_temperature))
|
||||
var substances: Array[Substance] = []
|
||||
substances.resize(len(inventory))
|
||||
var temp_inv: Array[RuntimeSubstanceData] = inventory.values()
|
||||
for i in range(len(temp_inv)):
|
||||
substances[i] = temp_inv[i].substance
|
||||
|
||||
func runtime_to_substance(sub: RuntimeSubstanceData) -> Substance:
|
||||
return sub.substance
|
||||
var found_reaction = ReactionRegistry.find_appropriate_reaction(substances,mean_temperature)
|
||||
|
||||
if found_reaction == null:
|
||||
return
|
||||
|
||||
var reagents: Dictionary[StringName, ReactionSubstance] = found_reaction.input_substances_dict()
|
||||
var reaction_mass: float = INF
|
||||
for key in reagents:
|
||||
var reagent_mass: float = inventory[key].amount/reagents[key].coefficient
|
||||
reaction_mass = min(reagent_mass,reaction_mass)
|
||||
|
||||
for key in reagents:
|
||||
extract_substance(key,reaction_mass * reagents[key].coefficient)
|
||||
|
||||
for sub in found_reaction.output_substances:
|
||||
var data: RuntimeSubstanceData = RuntimeSubstanceData.new()
|
||||
|
||||
data.amount = sub.coefficient * reaction_mass
|
||||
data.substance = sub.substance
|
||||
data.temperature = mean_temperature
|
||||
|
||||
add_substance_no_callback(data)
|
||||
check_for_reactions()
|
||||
recalculate_temperature()
|
||||
inventory_changed.emit(inventory.values())
|
||||
print(inventory)
|
||||
|
|
|
|||
|
|
@ -9,11 +9,17 @@ func _ready() -> void:
|
|||
reactions.append(load(REACTIONS_DIRECTORY + file))
|
||||
|
||||
func find_appropriate_reaction(input_substances: Array[Substance], temperature: float) -> Reaction:
|
||||
var has_all: Callable = func(compared_array: Array[ReactionSubstance]) -> bool:
|
||||
for el in compared_array:
|
||||
if not el.substance in input_substances:
|
||||
return false
|
||||
return true
|
||||
|
||||
var filter_func: Callable = func(reaction: Reaction) -> bool:
|
||||
if reaction.reaction_temperature != -1:
|
||||
return (reaction.input_substances == input_substances) and temperature >= reaction.reaction_temperature
|
||||
return (has_all.call(reaction.input_substances)) and temperature >= reaction.reaction_temperature
|
||||
else:
|
||||
return reaction.input_substances == input_substances
|
||||
return has_all.call(reaction.input_substances)
|
||||
|
||||
var filtered: Array[Reaction] = reactions.filter(filter_func)
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ class_name Reaction
|
|||
@export var reaction_temperature: float = -1
|
||||
|
||||
func input_substances_dict() -> Dictionary[StringName, ReactionSubstance]:
|
||||
var result = {}
|
||||
var result: Dictionary[StringName, ReactionSubstance] = {}
|
||||
|
||||
for el in input_substances:
|
||||
result[el.substance.scientific_name] = el
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue