8. Request mining and production
Once you can read the workshop state, entrust a golem with its first task. Check that it is free, then request ore. The spells in this chapter start real work in your workshop.
Select a golem
Workshop.Golems[0] selects your first golem. Numbering starts at 0, so the next golem is 1. This example uses only the first one.
golem.IsIdle checks whether that golem is free. It is false while mining or charging. A member that lets you read state this way is called a property.
Order only when it is free
public class Program : ArcaneProgram
{
public void OnStart()
{
var golem = Workshop.Golems[0];
if (golem.IsIdle)
{
var result = golem.Mine(Mineral.Ore, 20);
if (result.Ok)
{
Workshop.Log.Write("Mining accepted.");
}
else
{
Workshop.Log.Write("Mining was not accepted.");
}
}
}
}OnStart runs once when you start the script. Follow these steps through the spell.
- Store the first golem in golem.
- Check IsIdle. If it is false, skip the if block.
- If it is free, call Mine to request 20 ore.
- Store the result in result and check Ok to see whether the request was accepted.
The values inside Mine(Mineral.Ore, 20) are passed to the operation. These are arguments. Here they specify the mineral and quantity, separated by a comma. An operation you call this way is a method. Log.Write is also a method.
Ok being true means the request was accepted. Mining still takes time. Check progress in the workshop. Even a free golem may reject an order because of storage limits, so also check the result.
Request crafting
Workshop.Production[RecipeKind.Iron].Start(5) requests 5 iron. The number passed to Start is the quantity of output. Each iron needs 2 ore, so 5 iron needs 10 available ore.
Check the returned Ok value as you did for Mine. A busy facility cannot accept the request. See Equipment and research for recipes and facilities.
Replace the mining example with this spell to request one batch of iron. It skips the request when less than 10 ore is available.
public class Program : ArcaneProgram
{
public void OnStart()
{
if (Workshop.Resources[ResourceKind.Ore].Available >= 10)
{
var result = Workshop.Production[RecipeKind.Iron].Start(5);
if (result.Ok)
{
Workshop.Log.Write("Crafting accepted.");
}
else
{
Workshop.Log.Write("Crafting was not accepted.");
}
}
}
}Exercise
Change the example to request 10 ore only when the first golem is free.
Show example solutionHide example solution
Change 20 to 10 in golem.Mine(Mineral.Ore, 20). Keep the IsIdle check. If the golem is busy, the autonomous spell makes no request regardless of quantity.
