openclonk/planet/Objects.ocd/Libraries.ocd/LiquidControl.ocd/LiquidContainer.ocd/Script.c

147 lines
3.5 KiB
C
Raw Normal View History

Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
/**
* Liquid Container
*
* Basic interface for anything that can contain liquids.
*
* Author: Marky
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
*/
func IsLiquidContainer() { return true;}
func GetLiquidContainerMaxFillLevel()
{
return 0;
}
func IsLiquidContainerForMaterial(string liquid_name)
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
{
return true;
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
}
func GetLiquidAmount(liquid_name)
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
{
var amount = 0;
var type = nil;
// in case that a value was supplied, try finding the type for that
if (liquid_name != nil)
{
type = GetLiquidDef(liquid_name);
if (type == nil) FatalError(Format("No such liquid: %s", liquid_name));
}
// return everything if 'nil' was passed, or a specific amount if a value was passed
for (var liquid in GetLiquidContents())
if (liquid_name == nil || liquid->GetLiquidType() == type->GetLiquidType())
{
amount += liquid->~GetLiquidAmount();
}
return amount;
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
}
func GetLiquidAmountRemaining()
{
return GetLiquidContainerMaxFillLevel() - GetLiquidAmount();
}
func GetLiquidContents()
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
{
return FindObjects(Find_Container(this), Find_Func("IsLiquid"));
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
}
// -------------- Interaction
//
// Interfaces for interaction with other objects
/**
Extracts liquid from the container.
@param liquid_name: Material to extract; Wildcardsupport
Defaults to the current liquid if 'nil' is passed.
@param amount: Max Amount of liquid being extracted;
Defaults to all contained liquid if 'nil' is passed.
@param destination: Object that extracts the liquid
@return [returned_liquid, returned_amount]
- returned_liquid: Material being extracted
- returned_amount: Amount being extracted
*/
func RemoveLiquid(liquid_name, int amount, object destination)
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
{
if (amount < 0)
{
FatalError(Format("You can remove positive amounts of liquid only, got %d", amount));
}
amount = amount ?? GetLiquidAmount();
var removed = 0;
for (var liquid in GetLiquidContents())
{
if (removed >= amount) break;
if (!liquid_name) liquid_name = liquid->GetLiquidType();
removed += liquid->RemoveLiquid(liquid_name, amount - removed, destination)[1];
}
return [liquid_name, removed];
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
}
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
/**
Inserts liquid into the container.
@param liquid_name: Material to insert
@param amount: Max amount of material being inserted.
Passing a nil parameter will fill the
container to its maximum.
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
@param source: Object which inserts the liquid
@return returned_amount: The inserted amount
*/
func PutLiquid(liquid_name, int amount, object source)
Library: Liquid Container Added a new library for liquid containers that is based on the original barrel script. Library_LiquidContainer - Extracted the interface from the barrel. The function names and argument types are not final yet. - Fixed an inconsistency in the script: There were two ways of determining whether a barrel is suited for a material. Either by name, or material index. - renamed argument names to "liquid_name" for liquids, "amount" for quantities, so that the code is more easily understandable - replaced comparison 'fill level > 0' with 'is not empty?' - moved storage variables to a local proplist, so that names do not clash - Ordered the methods by application and documented each section Barrel: - The original barrel functions delegate to Library_LiquidContainer-functions now, so that the original functionality is not disturbed. Obsolete functions are marked with "TODO: deprecated". - Moved barrel value change to the update function - Moved hit sound effect to separate function - Replaced uses of the magic number '3' with a runtime-overloadable call to GetBarrelIntakeY() - Replaced comparison 'fill level > 0' with 'is not empty?' MetalBarrel: - Adjusted the hit function. The difference between this and the normal barrel is the hit sound and material position. These should be constants or functions, instead of magic numbers - Moved hit sound effect to separate function - Removed the Hit()-function, because the hit sound is played in a separate function now - With this change the y-offset of the barrel checking for other liquids on a hit is now 3 instead of 7. This should be ok, because the barrels have the same dimensions, it seemed strange that they would have different offsets
2016-01-28 16:39:16 +00:00
{
amount = amount ?? this->GetLiquidAmountRemaining();
if (amount < 0)
{
FatalError(Format("You can insert positive amounts of liquid only, got %d", amount));
}
var type = GetLiquidDef(liquid_name);
var max = this->GetLiquidContainerMaxFillLevel();
var before = GetLiquidAmount(liquid_name);
if (max > 0 && before >= max) return 0;
2016-03-25 15:24:43 +00:00
if (type)
{
var liquid = type->~CreateLiquid(amount);
if (liquid) Collect(liquid, true);
// the check is necessary here, because the liquid may get removed if the barrel already
// has a stack inside
if (liquid && !(liquid->Contained())) liquid->RemoveObject();
}
var after = GetLiquidAmount(liquid_name);
return after - before;
}
func AcceptsLiquid(liquid_name, int amount)
{
return amount <= this->GetLiquidContainerMaxFillLevel() - GetLiquidAmount(liquid_name);
}
private func GetLiquidDef(liquid_name)
{
if (GetType(liquid_name) == C4V_String)
{
return GetDefinition(liquid_name);
}
else if (GetType(liquid_name) == C4V_Def)
{
return liquid_name;
}
else
{
FatalError(Format("The first parameter of GetLiquidDef() must either be a string or definition. You passed %v.", liquid_name));
}
}