Example #1
0
    }//end HasGreen property

    /* drive function
     * pulls a car out of the waiting queue, queries it for next direction, then sends it down the road */
    public bool drive()
    {
        if (this.waitingVehicles.Empty())
        {
            return(false);
        }

        Vehicle v = waitingVehicles.pop();
        int     d = v.getDirection((int)this.dir);

        /* handle preemptive light switching for ambulances */
        if (v.GetType().ToString() == "Ambulance")
        {//clear future intersections and reset current intesection
            Ambulance a = (Ambulance)v;

            for (int i = 0; i < Simulation.NUM_PREEMPTIVE_GREENS; i++)
            {
                if (a.pathDirs.Count > i)
                {
                    to.outgoing[a.getFuturePath(i)].resetLights();
                }
            }
        }//end if vehicle is an ambulance

        /* drive the car through */
        if (0 <= d && d < 4)                                                                   //ambulances will return -1 if they are done
        {
            Road r = this.to.outgoing[d];                                                      //get the road to drive onto
            Simulation.futureEvents.Add(new EndOfRoadEvent(Simulation.time + r.length, r, v)); //send it down that road
            this.to.block(Simulation.CLEARING_TIME);                                           //block the intersection briefly
            return(true);
        }
        return(false);
    }