Example #1
0
    GameObject spawnOrb(Vector3 where, int poolSize, Unit_local pullFrom)
    {
        int payload;

        if (poolSize >= 70)
        {
            payload = Random.Range(5, 8);
        }
        else if (poolSize >= 20)
        {
            payload = Random.Range(3, 6);
        }
        else if (poolSize >= 5)
        {
            payload = Random.Range(2, 5);
        }
        else
        {
            payload = poolSize;
        }
// Meat deduction is done in the Orb's Awake function.
        GameObject newOrb = PhotonNetwork.Instantiate("Orb", where, Quaternion.identity, 0, new object[] { payload, pullFrom.photonView.ViewID });

        return(newOrb);
    }
Example #2
0
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.isTrigger == false)
     {
         Unit_local touchedUnit = other.GetComponent <Unit_local>();
         if (touchedUnit != null && other.name.Contains("sheep") == false)
         {
             if (Input.GetButton("modifier") == false)
             {
                 Cohort maybeOn  = touchedUnit.cohort;
                 bool   lightsOn = true;
                 foreach (Unit_local inQuestion in candidates)
                 {
                     if (inQuestion.cohort.Equals(maybeOn))
                     {
                         lightsOn = false;
                     }
                 }
                 if (lightsOn == true)
                 {
                     maybeOn.Highlight();
                 }
             }
             else
             {
                 touchedUnit.Highlight();
             }
             candidates.Add(touchedUnit);
         }
     }
 }
Example #3
0
 void OnTriggerExit2D(Collider2D other)
 {
     if (other.isTrigger == false)
     {
         Unit_local departedUnit = other.GetComponent <Unit_local>();
         if (departedUnit != null && other.name.Contains("sheep") == false)
         {
             candidates.Remove(departedUnit);
             if (Input.GetButton("modifier") == false)
             {
                 Cohort maybeExtingiush = departedUnit.cohort;
                 bool   lightsOut       = true;
                 foreach (Unit_local inQuestion in candidates)
                 {
                     if (inQuestion.cohort.Equals(maybeExtingiush))
                     {
                         lightsOut = false;
                     }
                 }
                 if (lightsOut == true)
                 {
                     maybeExtingiush.HighlightOff();
                 }
             }
             else
             {
                 departedUnit.Unhighlight();
             }
         }
     }
 }
Example #4
0
 public void removeFromPalette(Unit_local toRem)
 {
     if (rectManager.rectOn == false)
     {
         unitUnderMouse.cohort.HighlightOff();
     }
     unitUnderMouse = null;
 }
Example #5
0
 public void activateUnit(Unit_local toAdd)
 {
     //Debug.Log("Attempting to add object to activeUnits.");
     if (activeUnits.Contains(toAdd) == false)
     {
         activeUnits.Add(toAdd);
         activeUnitsChangedFlag = true;
     }
 }
Example #6
0
 public Task(Unit_local doneBy, actions doWhat, Vector2 where, Unit doneTo = null, int howMuch = 0, float extraData = 0)
 {
     subjectUnit = doneBy;
     nature      = doWhat;
     center      = where;
     objectUnit  = doneTo;
     quantity    = howMuch;
     dataA       = extraData;
 }
Example #7
0
 public void deactivateUnit(Unit_local toRem)
 {
     //Debug.Log("Attempting to remove object from activeUnits.");
     if (activeUnits.Contains(toRem))
     {
         activeUnits.Remove(toRem);
         activeUnitsChangedFlag = true;
     }
 }
Example #8
0
 public void removeMember(Unit_local reject)
 {
     members.Remove(reject);
     assignments.Remove(reject.task);
     armedMembers.Remove(reject);
     mobileMembers.Remove(reject);
     depotMembers.Remove(reject);
     shepherdMembers.Remove(reject);
     // Debug.Log("Removed " + reject.name + ". Now " + members.Count + " members.");
 }
Example #9
0
    public void taskCompleted(Task completedTask)
    {
        assignments.Remove(completedTask);
        Unit_local worker = completedTask.subjectUnit.GetComponent <Unit_local>();

        switch (completedTask.nature)
        {
        case Task.actions.give:
            if (worker.meat > 0 && remainingToAccept.Count > 0)
            {
                if (remainingToProvide.Contains(worker) == false)
                {
                    remainingToProvide.Add(worker, worker.meat);
                }
                assignTransactionWork(worker);
            }
            break;

        case Task.actions.take:
            if (worker.roomForMeat() > 0 && remainingToProvide.Count > 0)
            {
                if (remainingToAccept.Contains(worker) == false)
                {
                    remainingToAccept.Add(worker, worker.roomForMeat());
                }
                assignTransactionWork(worker);
            }
            break;

        case Task.actions.move:
            if (completedTask.objectUnit == null)
            {
                Brake();
            }
            break;

        case Task.actions.attack:
            remainingToPerish.Remove(completedTask.objectUnit);
            if (remainingToPerish.Count > 0)
            {
                assignViolentWork(completedTask.subjectUnit);
            }
            else
            {
                Stop();
            }
            break;

        default:
            break;
        }
    }
Example #10
0
    void assignViolentWork(Unit_local worker)
    {
        UnitRelativePositionSorter comparer = new UnitRelativePositionSorter(worker.transform.position);

        comparer.DistanceMode();
        remainingToPerish.Sort(comparer);
        List <Unit> inRange        = new List <Unit>();
        Unit        closestHoplite = null;
        Unit        closestDog     = null;
        Unit        closestCourier = null;

        for (int i = 0; i < remainingToPerish.Count && comparer.DistanceOf(remainingToPerish[i]) <= worker.stats.weapon_range; ++i)
        {
            inRange.Add(remainingToPerish[i]);
            if (closestHoplite == null && remainingToPerish[i].name.Contains("hoplite"))
            {
                closestHoplite = remainingToPerish[i];
            }
            else if (closestDog == null && remainingToPerish[i].name.Contains("dog"))
            {
                closestDog = remainingToPerish[i];
            }
            else if (closestCourier == null && remainingToPerish[i].name.Contains("courier"))
            {
                closestCourier = remainingToPerish[i];
            }
        }
        Unit targetUnit;

        if (closestHoplite != null)
        {
            targetUnit = closestHoplite;
        }
        else if (closestDog != null)
        {
            targetUnit = closestDog;
        }
        else if (closestCourier != null)
        {
            targetUnit = closestCourier;
        }
        else
        {
            targetUnit = remainingToPerish[0];
        }
        Task result = new Task(worker, Task.actions.attack, targetUnit.transform.position, targetUnit);

        worker.work(result);
        assignments.Add(result);
    }
Example #11
0
    public virtual IEnumerator Fire()
    {
        // Debug.Log("firing on " + target.GetPhotonView().ViewID);
        while (target != null)
        {
            if (thisUnit.meat >= shotCost)
            {
                visualizer.Show();
// THE ACTUAL STRIKE IS CARRIED OUT BY THE REMOTE INSTANCES.
                yield return(new WaitForSeconds(reloadTime));
            }
            else
            {
                Unit_local provider     = null;
                int        halfAdjusted = 0;
                foreach (Unit_local comrade in thisUnit.cohort.armedMembers)
                {
                    halfAdjusted = (comrade.meat - (comrade.meat % shotCost)) / 2;
                    if (comrade.task.nature == Task.actions.attack &&
                        Vector2.Distance(transform.position, comrade.transform.position) < 10 &&
                        halfAdjusted >= shotCost)
                    {
                        provider = comrade;
                        break;
                    }
                }
                if (provider != null)
                {
                    ((Unit_local)thisUnit).temporaryOverrideTask = new Task(((Unit_local)thisUnit), Task.actions.take, Vector2.zero, provider, halfAdjusted);
                    Coroutine dispenseRoutine = thisUnit.StartCoroutine("dispense");
                    float     mark            = Time.time;
                    while (Time.time - mark < 8 && thisUnit.meat < shotCost)
                    {
                        yield return(new WaitForSeconds(0.1f));
                    }
                    StopCoroutine(dispenseRoutine);
                    ((Unit_local)thisUnit).temporaryOverrideTask = null;
                    if (thisUnit.meat >= shotCost)
                    {
                        continue;
                    }
                }
                photonView.RPC("CeaseFire", RpcTarget.All);
                yield return(null);
            }
        }
        Disengage();
        yield return(null);
    }
Example #12
0
 public void addToPalette(Unit_local underMouse)
 {
     unitUnderMouse = underMouse;
     if (rectManager.rectOn == false)
     {
         if (Input.GetButton("modifier") == false)
         {
             unitUnderMouse.cohort.Highlight();
         }
         else
         {
             unitUnderMouse.Highlight();
         }
     }
 }
Example #13
0
// This should not be called except by ChangeCohort() in Unit_local
    public void addMember(Unit_local recruit)
    {
        members.Add(recruit);
        if (recruit.stats.isArmed)
        {
            armedMembers.Add(recruit);
        }
        else if (recruit.name.Contains("depot"))
        {
            depotMembers.Add(recruit);
        }
        else if (recruit.name.Contains("shepherd"))
        {
            shepherdMembers.Add(recruit);
        }
        if (recruit.stats.isMobile)
        {
            mobileMembers.Add(recruit);
        }
        // Debug.Log("Added " + recruit.name + ". Now " + members.Count + " members.");
    }
Example #14
0
    public bool assignTransactionWork(Unit_local needsCounterparty, bool assignByTarget = false)
    {
        // Debug.Log("AssignTransactionWork");
        Hashtable  counterParties      = null;
        Unit_local closestCounterparty = null;
// We have to get a little weird with these, because closestCounterparty, which one of these two things will reference, can't be decided until we determine what the counterparties are,
// which is done in the same IF statement that determines what the provider is. I know it's weird.
        Func <Unit_local> provider;
        Func <Unit_local> reciever;

        if (masterTask.nature == Task.actions.give ^ assignByTarget == true)
        {
            counterParties = remainingToAccept;
            provider       = () => needsCounterparty;
            reciever       = () => closestCounterparty;
        }
        else
        {
            counterParties = remainingToProvide;
            provider       = () => closestCounterparty;
            reciever       = () => needsCounterparty;
        }
        float bestDistance = 999999;

        foreach (Unit_local recieverCandidate in counterParties.Keys)
        {
            float distanceTo = Vector2.Distance(recieverCandidate.transform.position, needsCounterparty.transform.position);
            if (distanceTo < bestDistance)
            {
                closestCounterparty = recieverCandidate;
                bestDistance        = distanceTo;
            }
        }
        Hashtable  leftoverFactor;
        Hashtable  exhaustedFactor;
        Unit_local unitWithLeftovers;
        Unit_local limitingUnit;
        int        quantityToPass = 0;

        if (reciever().roomForMeat() == provider().meat)
        {
            quantityToPass = provider().meat;
            remainingToAccept.Remove(reciever);
            remainingToProvide.Remove(provider());
        }
        else
        {
            if (reciever().roomForMeat() < provider().meat)
            {
                leftoverFactor    = remainingToProvide;
                exhaustedFactor   = remainingToAccept;
                limitingUnit      = reciever();
                unitWithLeftovers = provider();
                quantityToPass    = reciever().roomForMeat();
            }
            else
            {
                leftoverFactor    = remainingToAccept;
                exhaustedFactor   = remainingToProvide;
                limitingUnit      = provider();
                unitWithLeftovers = reciever();
                quantityToPass    = provider().meat;
            }
            leftoverFactor[unitWithLeftovers] = (int)leftoverFactor[unitWithLeftovers] - (int)exhaustedFactor[limitingUnit];
            exhaustedFactor.Remove(limitingUnit);
        }
        Task newTask;

        if (assignByTarget == false)
        {
            newTask = new Task(needsCounterparty, masterTask.nature, Vector2.zero, closestCounterparty, quantityToPass);
            needsCounterparty.work(newTask);
        }
        else
        {
            newTask = new Task(closestCounterparty, masterTask.nature, Vector2.zero, needsCounterparty, quantityToPass);
            closestCounterparty.work(newTask);
        }
        assignments.Add(newTask);
        return(true);
    }
Example #15
0
    public void MoveCohort(Vector2 goTo, Unit_local toFollow)
    {
        Stop();
        masterTask = new Task(null, Task.actions.move, goTo, toFollow);
        List <Unit_local> thisIsToSupressWarnings = new List <Unit_local>(members);
        float             weakLinkETA             = 0;

        foreach (Unit_local toMove in thisIsToSupressWarnings)
        {
            if (toMove.stats.isMobile == false)
            {
                throw new InvalidOperationException("Cannot move a cohort that includes immobile members. Units should be sorted out in Gamestate.CombineActiveCohorts.");
            }
            else
            {
                float thisMoversETA = Vector2.Distance(toMove.transform.position, goTo) / toMove.stats.speed;
                if (thisMoversETA > weakLinkETA)
                {
                    weakLinkETA = thisMoversETA;
                }
            }
        }
        UnitRelativePositionSorter vsGoTo = new UnitRelativePositionSorter(goTo);

        vsGoTo.DirectionMode();
        List <Unit_local> unitsByDirection = new List <Unit_local>(members);

// This is a list of units sorted by their compass-direction from the destination point.
        (unitsByDirection).Sort(vsGoTo);
        vsGoTo.DistanceMode();
        List <Unit_local> unitsByDistance = new List <Unit_local>(members);

        unitsByDistance.Sort(vsGoTo);
// Units are removed from unitsByDirection (and unitsByDistance) as they are assigned to groups, so this is a way of saying "while there are unassigned units."
        while (unitsByDirection.Count > 0)
        {
// We take the current closest unit to the distination... (previous cycles of grouping will have removed closer units)
            Unit_local        sliceLeader                   = unitsByDistance[0];
            int               leaderDirectionIndex          = unitsByDirection.IndexOf(sliceLeader);
            float             leaderDistanceFromDestination = vsGoTo.DistanceOf(sliceLeader);
            float             leaderRadius                  = sliceLeader.bodyCircle.radius;
            int               totalUnaccounted              = unitsByDirection.Count;
            List <Unit_local> slice = new List <Unit_local> {
                sliceLeader
            };
// That unit will be the group leader. We check the units clockwise and counter-clockwise from its' position on the imaginary circle of unit positions aronud the destination.
            for (int sign = -1; sign <= 1 && slice.Count < unitsByDirection.Count; sign = sign + 2)
            {
                // this is a loop-breaker variable
                for (int indexOffset = sign; indexOffset < 1000; indexOffset += sign)
                {
                    Unit_local inQuestion = unitsByDirection[(leaderDirectionIndex + indexOffset + totalUnaccounted) % totalUnaccounted];
// These are all in radians...
                    float directionOfLeader       = vsGoTo.DirectionOf(sliceLeader);
                    float directionInQuestion     = vsGoTo.DirectionOf(inQuestion);
                    float circumferencialDistance = Mathf.Abs(directionOfLeader - directionInQuestion);
                    circumferencialDistance = Mathf.Min(circumferencialDistance, 2 * Mathf.PI - circumferencialDistance);
// ...until here, when circumferentialDistance becomes a measure of real distance.
                    circumferencialDistance *= leaderDistanceFromDestination;
// In both directions, we stop checking when the next-closest (by direction) unit doesn't fall within the shadow cast by the leader, if you imagine the destination as a light source.
                    if (circumferencialDistance < leaderRadius && inQuestion != sliceLeader)
                    {
                        slice.Add(inQuestion);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            slice.Sort(vsGoTo);
// This line makes it so that all groups arive at the same time.
// PROBLEM: this will make fast units move slugishly for very short journeys.
            float leaderSpeed = Mathf.Clamp(Vector2.Distance(sliceLeader.transform.position, goTo) / weakLinkETA, 0, sliceLeader.stats.speed);
            sliceLeader.work(new Task(sliceLeader, Task.actions.move, goTo, toFollow, -1, leaderSpeed));
            for (int followerIndex = 1; followerIndex < slice.Count; ++followerIndex)
            {
                slice[followerIndex].work(new Task(slice[followerIndex], Task.actions.move, goTo, sliceLeader));
            }
            foreach (MobileUnit_local sliceMember in slice)
            {
                assignments.Add(sliceMember.task);
                unitsByDirection.Remove(sliceMember);
                unitsByDistance.Remove(sliceMember);
            }
        }
    }
Example #16
0
    public IEnumerator makeUnit(string unitType)
    {
        string unitAddress = "Units/" + unitType;
        int    expense     = ((GameObject)Resources.Load(unitAddress)).GetComponent <UnitBlueprint>().costToBuild;
        int    purse       = collectiveMeat();
        int    orderSize;

        if (Input.GetButton("modifier") == true)
        {
            orderSize = 6;
        }
        else
        {
            orderSize = 1;
        }
        int batchSize = Mathf.Clamp(purse / expense, 0, orderSize);

        expense *= batchSize;
        int share = Mathf.CeilToInt(expense / (float)members.Count);

        Debug.Log(share);
        int covered     = 0;
        int loopBreaker = 0;
        List <Unit_local> thisIsToSupressWarnings = new List <Unit_local>(members);

        for (int index = 0; covered < expense; index = index % thisIsToSupressWarnings.Count)
        {
            int ask = Mathf.Clamp(expense - covered, 0, share);
            if (thisIsToSupressWarnings[index].meat > ask)
            {
                thisIsToSupressWarnings[index].photonView.RPC("deductMeat", RpcTarget.All, share);
                covered += share;
                // Debug.Log("Covering " + share + " out of " + expense);
                ++index;
            }
            else
            {
                int scrapeBottom = thisIsToSupressWarnings[index].meat;
                if (scrapeBottom > 0)
                {
                    thisIsToSupressWarnings[index].photonView.RPC("deductMeat", RpcTarget.All, scrapeBottom);
                    covered += scrapeBottom;
                }
                thisIsToSupressWarnings.RemoveAt(index);
            }
            if (++loopBreaker > 1000)
            {
                throw new System.Exception("Loop broken with " + thisIsToSupressWarnings.Count + " in the list.");
            }
        }
        Vector3 []        spots = spawnLocations(unitAddress);
        List <GameObject> batch = new List <GameObject>();

        for (int i = 0; i < batchSize; ++i)
        {
//In the future, there needs to be a mechanism to detect whether the space around the factory is obstructed, and probably to move those obstructing units. I'd suggest making makeUnit return a boolean
//which will be false as long as the space is obstructed, and then have the ordering method handle the subsiquent calls and the moving of units.
            GameObject justCreated = PhotonNetwork.Instantiate(unitAddress, spots[i], Quaternion.identity);
            batch.Add(justCreated);
            spawnLocationCycler = (spawnLocationCycler + 1) % 6;
            if (++loopBreaker > 1000)
            {
                throw new System.Exception("Loop broken.");
            }
        }
        yield return(new WaitForSeconds(0));

        if (unitType != "sheep")
        {
            Cohort cohortToJoin;
            if (this == members[0].soloCohort)
            {
                cohortToJoin = new Cohort(new List <Unit_local>(new Unit_local[] { members[0] }));
            }
            else
            {
                cohortToJoin = this;
            }
            for (int i = 0; i < batchSize; ++i)
            {
                Unit_local justMade = batch[i].GetComponent <Unit_local>();
                justMade.changeCohort(cohortToJoin);
                justMade.activate();
                if (++loopBreaker > 1000)
                {
                    throw new System.Exception("Loop broken.");
                }
            }
        }
        yield return(null);
    }
Example #17
0
    // public void CommenceHelp () {
// Units should be allocated to tasks with the following order of priorities (1) Attack master's target, (2) Defend master from attackers, (3) Refill ranged units with attack orders, (4) Herd sheep, (5) Move with master.
// Commenceattack(), CommenceTransact(), and MoveCohort() will need slight rewrites to allow different actions within the same cohort.
    // }

    public void commenceTransact(Task transaction)
    {
        // Debug.Log("CommenceTransact");
        Stop();
        masterTask = transaction;
        Cohort from;
        Cohort to;

        if (transaction.nature == Task.actions.give)
        {
            from = this;
            to   = transaction.objectUnit.GetComponent <Unit>().cohort;
        }
        else
        {
            from = transaction.objectUnit.GetComponent <Unit>().cohort;
            to   = this;
        }
        List <Unit_local> providerCandidates = null;
        List <Unit_local> recieverCandidates = null;

        if (to == from)
        {
            Unit_local singledOut = (Unit_local)transaction.objectUnit;
            if (transaction.nature == Task.actions.take)
            {
                providerCandidates = new List <Unit_local>(new Unit_local[] { singledOut });
                recieverCandidates = new List <Unit_local>(members);
                recieverCandidates.Remove(singledOut);
            }
            else if (transaction.nature == Task.actions.give)
            {
                recieverCandidates = new List <Unit_local>(new Unit_local[] { singledOut });
                providerCandidates = new List <Unit_local>(members);
                providerCandidates.Remove(singledOut);
            }
        }
        else
        {
            providerCandidates = from.members;
            recieverCandidates = to.members;
        }
        foreach (Unit_local giver in providerCandidates)
        {
// Watch out for malfunctions: I can't remember why I had that second criteria, so I'm removing it to see what breaks. Make a note if you find out.
            if (giver.meat > 0)   // && remainingToAccept.ContainsKey(giver) == false) {
            {
                remainingToProvide.Add(giver, giver.meat);
            }
        }
        foreach (Unit_local recipient in recieverCandidates)
        {
            if (recipient.roomForMeat() > 0)
            {
                remainingToAccept.Add(recipient, recipient.roomForMeat());
            }
        }
        Hashtable workers;

        if (transaction.nature == Task.actions.give)
        {
            workers = new Hashtable(remainingToProvide);
        }
        else
        {
            workers = new Hashtable(remainingToAccept);
        }
        foreach (Unit_local worker in workers.Keys)
        {
            if (remainingToProvide.Count <= 0 || remainingToAccept.Count <= 0)
            {
                break;
            }
            assignTransactionWork(worker);
        }
    }
Example #18
0
    void thingLeftClicked(GameObject thingClicked)
    {
        switch (thingClicked.tag)
        {
        case "unit":
        case "unit icon":
            if (thingClicked.name == "Icon")
            {
                thingClicked = thingClicked.transform.parent.gameObject;
            }
            Unit_local unit = thingClicked.GetComponent <Unit_local>();
            if (unit != null && thingClicked.name.Contains("sheep") == false)
            {
                if (commandMode == commands.neutral)
                {
                    if (Input.GetButton("modifier") == true)
                    {
                        if (gameState.activeUnits.Contains(unit))
                        {
                            unit.deactivate();
                        }
                        else
                        {
                            unit.soloCohort.activate();
                        }
                    }
                    else
                    {
                        gameState.clearActive();
                        unit.cohort.activate();
                    }
                }
                else
                {
                    if (commandMode == commands.take)
                    {
                        Cohort takingCohort = combineActiveUnits(Task.actions.take);
                        takingCohort.commenceTransact(new Task(null, Task.actions.take, Vector2.zero, unit));
                    }
                    else if (commandMode == commands.give)
                    {
                        Cohort givingCohort = combineActiveUnits(Task.actions.give);
                        givingCohort.commenceTransact(new Task(null, Task.actions.give, Vector2.zero, unit));
                    }
                }
            }
            break;

        case "ground":
            gameState.clearActive();
            break;

        case "UI":
            break;

        case "out of bounds":
            break;

        default:
            Debug.Log("PROBLEM: nothing with a valid tag hit by raycast.");
            break;
        }
    }
 void Start()
 {
     thisUnit = transform.parent.GetComponent <Unit_local>();
 }