include "GenericIndustry.gs"
//
// MultipleIndustry industry
//
class MultipleIndustry isclass GenericIndustry
{
    ProductQueue cattleQueue;
    Asset cattleAsset;
    bool animating = false;
    bool processing = false;
    bool scriptletEnabled = true;
    // Track if they only supply some of the logs that were requested in the waybill.
    int cattleWBRemain = 0;
    thread void ViewDetails(void)
    {
    if (!info)
    {
        info = Constructors.NewBrowser();
        info.LoadHTMLString(HTMLWindow.GetCompleteIndustryViewDetailsHTMLCode(me, scriptletEnabled));
        info.SetWindowRect(100, 80, 500, 545);
    }
    }
    bool TriggerSupportsStoppedLoad(Vehicle vehicle, string triggerName)
    {
    bool vehicleToTrain = vehicle.GetFacingRelativeToTrain();
    int direction;
    direction = vehicle.GetRelationToTrack(me,"cattle-export");
    if (!vehicleToTrain) direction = -direction;
    // Are we up to the furthest trigger away from the side we entered for goods?
    if (direction == Vehicle.DIRECTION_BACKWARD and triggerName == "cattle-exporttrig1") return true;
    else if (direction == Vehicle.DIRECTION_FORWARD and triggerName == "cattle-exporttrig1") return true;
    // If the train has already stopped, then fall thru and allow this load as well
    if (triggerName == "cattle-exporttrig1")
    {
        if (vehicle.GetMyTrain().IsStopped()) return true;
    }
    return false;
    }
    void PerformStoppedLoad(Vehicle vehicle, string triggerName)
    {
    bool cattleWBModified = false;
    int cattlespaceAvailable;
    LoadingReport cattlereport;
    int cattledirection;
    if (triggerName == "cattle-exporttrig1" )
    {
        if (itc.IsTrainCommand(vehicle.GetMyTrain(), Industry.UNLOAD_COMMAND))
        {
        cattlespaceAvailable = cattleQueue.GetQueueSpace();
        cattlereport = CreateUnloadingReport(cattleQueue,cattlespaceAvailable);
        cattledirection = vehicle.GetRelationToTrack(me,"cattle-export");
        if (cattledirection == Vehicle.DIRECTION_FORWARD) cattlereport.sideFlags = LoadingReport.RIGHT_SIDE;
        else if (cattledirection == Vehicle.DIRECTION_BACKWARD) cattlereport.sideFlags = LoadingReport.LEFT_SIDE;
        vehicle.UnloadProduct(cattlereport);
        //Already done something to the queue? if so, set flag so that we don't unload it again.
        if (cattlereport.amount > 0)
        {
            cattleWBRemain = cattleWBRemain - cattlereport.amount;
            if (cattleWBRemain < 0) cattleWBRemain = 0;
            cattleWBModified = true;
        }
        }
    }
    }
    thread void MultipleMain(void)
    {
    Message msg;
    Vehicle vehicle;
    string triggerName;
    wait()
    {
        on "Scriptlet-Enabled", "1":
        {
        if (!scriptletEnabled)
        {
            scriptletEnabled = true;
            SetProcessEnabled("multi_consumer_producer", true);
        }
        continue;
        }
        on "Scriptlet-Enabled", "0":
        {
        if (scriptletEnabled)
        {
            scriptletEnabled = false;
            SetProcessEnabled("multi_consumer_producer", false);
        }
        continue;
        }
    }
    }
    // called by the game once when a process is ready to start (see Industry.gs)
    void NotifyProcessStarted(string processName)
    {
    if (PerformProcessInput(processName))
    {
        // we are making the assumption that there is only one process, "multi_consumer_producer"
        PerformProcessStarted(processName);
    }
    else PerformProcessCancelled(processName);
    }
    //
    // Called by the game once when a process is ready to stop (see Industry.gs)
    //
    void NotifyProcessFinished(string processName)
    {
    processing = false;
    if (!animating)
    {
        PerformProcessOutput(processName);
        PerformProcessFinished(processName);
    }
    }
    public void Init(void)
    {
    inherited();
    usePipeAnimation = false;
    // initialize queues
    cattleQueue = GetQueue("cattle_q");
    cattleAsset = GetAsset().FindAsset("cattle");
    AddAssetToIndustryProductInfo("cattle", "cattle_q", "multi_consumer_producer", true);
    MultipleMain();
    }
    public Requirement[] GetRequirements(void)
    {
    Requirement[] ret = new Requirement[0];
    if (cattleQueue.GetQueueSpace() > (cattleQueue.GetQueueSize() / 2) or cattleWBRemain > 0)
    {
        ResourceRequirement req = new ResourceRequirement();
        req.resource = cattleQueue.GetProductFilter().GetProducts()[0];
        req.amount = cattleQueue.GetQueueSpace();
        req.dst = me;
        req.dstQueue = cattleQueue;
        cattleWBRemain = req.amount;
        ret[ret.size()] = req;
    }
     return ret;
    }
    public void AppendDriverDestinations(string[] destNames,string[] destTracks)
    {
    destNames[destNames.size()] = "cattle-unloading";
    destTracks[destTracks.size()] = "cattle-export";
    }
};