public void Start() { if (world == null) { world = World.mainWorld; } thingDoing = new ThingDoing(TypeOfThingDoing.Standing, null); genome = new Genome(GetBaseGenome()); food = genome["baseHunger"]; stamina = genome["baseStamina"]; maxStamina = genome["maxStamina"]; maxFood = genome["maxFood"]; for (int i = 0; i < actionWeights.Length; i++) { TypeOfThingDoing typeOfThing = (TypeOfThingDoing)i; float maxValue = 10.0f; if (IsImportant(typeOfThing, ref maxValue)) { isImportant[i] = true; importantMaxValues[i] = maxValue; } else { isImportant[i] = false; } actionBaseWeights[i] = GetBaseWeight(typeOfThing); } }
public override bool IsImportant(TypeOfThingDoing thingDoing, ref float maxValue) { if (thingDoing == TypeOfThingDoing.GettingFood) { maxValue = 30.0f; return(true); } else if (thingDoing == TypeOfThingDoing.RunningAway) { maxValue = 30.0f; return(true); } else if (thingDoing == TypeOfThingDoing.Gathering) { maxValue = 30.0f; return(true); } else if (thingDoing == TypeOfThingDoing.GoingTo) { maxValue = 30.0f; return(true); } //else if (thingDoing == TypeOfThingDoing.Standing) return 1.0f - genome["curiosity"]; //else if (thingDoing == TypeOfThingDoing.Socializing) return genome["extrovertednes"]; //else if (thingDoing == TypeOfThingDoing.Wandering) return genome["curiosity"]; return(false); }
/* * public override void UpdateBehavior(ref ThingDoing curDoing) * { * * if (curDoing == null) * { * curDoing = new ThingDoing(TypeOfThingDoing.Searching, null); * } * * if (food < wantToFindFoodThresh && curDoing.typeOfThing != TypeOfThingDoing.Chasing && curDoing.typeOfThing != TypeOfThingDoing.Searching) * { * curDoing = new ThingDoing(TypeOfThingDoing.Searching); * } * * if (food >= wantToFindFoodThresh && curDoing.typeOfThing != TypeOfThingDoing.RunningAway) * { * curDoing = new ThingDoing(TypeOfThingDoing.Standing); * } * * * BlocksPlayer[] players = FindObjectsOfType<BlocksPlayer>(); * * foreach (BlocksPlayer player in players) * { * if (Vector3.Distance(player.transform.position, transform.position) < 10.0f) * { * curDoing = new ThingDoing(TypeOfThingDoing.RunningAway, new ThingDoingTarget(player.GetComponent<MovingEntity>())); * } * } * * MovingEntity self = GetComponent<MovingEntity>(); * if (curDoing.typeOfThing == TypeOfThingDoing.Chasing) * { * if (curDoing.target.entity == null) * { * pathingTarget = curDoing.target.block; * } * else * { * pathingTarget = LVector3.FromUnityVector3(curDoing.target.entity.transform.position); * } * * * if (pathingTarget.BlockV != Example.FlowerWithNectar) * { * curDoing = new ThingDoing(TypeOfThingDoing.Searching, null); * } * else * { * float myDist = LVector3.CityBlockDistance(LVector3.FromUnityVector3(transform.position), pathingTarget); * // found it, eat it * if (myDist < 4) * { * DidEatObject(pathingTarget, 1.0f); * world[pathingTarget] = (int)Example.Flower; * curDoing = new ThingDoing(TypeOfThingDoing.Searching, null); * } * // still pathing to it * else * { * UpdatePathing(); * } * } * //pathingTarget = LVector3.FromUnityVector3(FindObjectOfType<BlocksPlayer>().transform.position); * } * else if (curDoing.typeOfThing == TypeOfThingDoing.RunningAway) * { * if (curDoing.target.entity == null) * { * pathingTarget = curDoing.target.block; * } * else * { * pathingTarget = LVector3.FromUnityVector3(curDoing.target.entity.transform.position); * } * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Searching) * { * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Sitting) * { * * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Socializing) * { * * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Standing) * { * GetComponent<CrocodileDoggy>().SetSpeed(0.0f); * self.desiredMove = Vector3.zero; * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Wandering) * { * * } * } * */ public override ThingDoing UpdateBehavior(TypeOfThingDoing newTypeOfThingDoing) { if (newTypeOfThingDoing == TypeOfThingDoing.RunningAway) { newTypeOfThingDoing = TypeOfThingDoing.GettingFood; } return(new ThingDoing(newTypeOfThingDoing, null)); }
/* * public override void UpdateBehavior(ref ThingDoing curDoing) * { * * if (curDoing == null) * { * curDoing = new ThingDoing(TypeOfThingDoing.Searching, null); * } * * if (food < wantToFindFoodThresh && curDoing.typeOfThing != TypeOfThingDoing.Chasing && curDoing.typeOfThing != TypeOfThingDoing.Searching) * { * curDoing = new ThingDoing(TypeOfThingDoing.Searching); * } * * if (food >= wantToFindFoodThresh && curDoing.typeOfThing != TypeOfThingDoing.RunningAway) * { * curDoing = new ThingDoing(TypeOfThingDoing.Standing); * } * * * BlocksPlayer[] players = FindObjectsOfType<BlocksPlayer>(); * * foreach (BlocksPlayer player in players) * { * if (Vector3.Distance(player.transform.position, transform.position) < 10.0f) * { * curDoing = new ThingDoing(TypeOfThingDoing.RunningAway, new ThingDoingTarget(player.GetComponent<MovingEntity>())); * } * } * * MovingEntity self = GetComponent<MovingEntity>(); * if (curDoing.typeOfThing == TypeOfThingDoing.Chasing) * { * if (curDoing.target.entity == null) * { * pathingTarget = curDoing.target.block; * } * else * { * pathingTarget = LVector3.FromUnityVector3(curDoing.target.entity.transform.position); * } * * * if (pathingTarget.BlockV != Example.FlowerWithNectar) * { * curDoing = new ThingDoing(TypeOfThingDoing.Searching, null); * } * else * { * float myDist = LVector3.CityBlockDistance(LVector3.FromUnityVector3(transform.position), pathingTarget); * // found it, eat it * if (myDist < 4) * { * DidEatObject(pathingTarget, 1.0f); * world[pathingTarget] = (int)Example.Flower; * curDoing = new ThingDoing(TypeOfThingDoing.Searching, null); * } * // still pathing to it * else * { * UpdatePathing(); * } * } * //pathingTarget = LVector3.FromUnityVector3(FindObjectOfType<BlocksPlayer>().transform.position); * } * else if (curDoing.typeOfThing == TypeOfThingDoing.RunningAway) * { * if (curDoing.target.entity == null) * { * pathingTarget = curDoing.target.block; * } * else * { * pathingTarget = LVector3.FromUnityVector3(curDoing.target.entity.transform.position); * } * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Searching) * { * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Sitting) * { * * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Socializing) * { * * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Standing) * { * GetComponent<CrocodileDoggy>().SetSpeed(0.0f); * self.desiredMove = Vector3.zero; * } * else if (curDoing.typeOfThing == TypeOfThingDoing.Wandering) * { * * } * } * */ public override ThingDoing UpdateBehavior(TypeOfThingDoing newTypeOfThingDoing) { ThingDoing result = null; if (assignment == SisterAssignment.StayWithMother) { result = new ThingDoing(TypeOfThingDoing.GoingTo, new ThingDoingTarget(mother.GetComponent <MovingEntity>())); } else if (assignment == SisterAssignment.StayWithChampion) { result = new ThingDoing(TypeOfThingDoing.GoingTo, new ThingDoingTarget(champion.GetComponent <MovingEntity>())); } else if (assignment == SisterAssignment.Forage) { result = new ThingDoing(TypeOfThingDoing.Gathering, new ThingDoingTarget(new BlockValue[] { Example.Leaf, Example.Trunk, Example.Flower })); } else if (assignment == SisterAssignment.KeepWatch) { result = new ThingDoing(TypeOfThingDoing.Standing, null); } else { Debug.LogWarning("unknown sister assignment " + assignment); result = new ThingDoing(newTypeOfThingDoing, null); } if (result.typeOfThing == thingDoing.typeOfThing) { // don't modify it if valid blocks matches (this keeps a found position if there is one when foraging) if (result.target != null && result.target.validBlocks != null) { if (thingDoing.target == null || thingDoing.target.validBlocks == null || thingDoing.target.validBlocks.Length != result.target.validBlocks.Length) { return(result); } else { for (int i = 0; i < result.target.validBlocks.Length; i++) { if (thingDoing.target.validBlocks[i] != result.target.validBlocks[i]) { return(result); } } return(thingDoing); } } return(thingDoing); } else { return(result); } }
public override float GetWeight(TypeOfThingDoing thingDoing) { if (thingDoing == TypeOfThingDoing.GettingFood) { return(1.0f - food / maxFood); } //else if (thingDoing == TypeOfThingDoing.RunningAway) return genome["easilySpooked"]; //else if (thingDoing == TypeOfThingDoing.Standing) return 1.0f - genome["curiosity"]; //else if (thingDoing == TypeOfThingDoing.Socializing) return genome["extrovertednes"]; //else if (thingDoing == TypeOfThingDoing.Wandering) return genome["curiosity"]; return(0.0f); }
public override float GetBaseWeight(TypeOfThingDoing thingDoing) { if (thingDoing == TypeOfThingDoing.GettingFood) { return(0.0f); } else if (thingDoing == TypeOfThingDoing.RunningAway) { return(0.0f); } else if (thingDoing == TypeOfThingDoing.Standing) { return(1.0f - genome["curiosity"]); } else if (thingDoing == TypeOfThingDoing.Socializing) { return(genome["extrovertednes"]); } else if (thingDoing == TypeOfThingDoing.Wandering) { return(genome["curiosity"]); } return(0.0f); }
public void Update() { this.typeOfThingDoing = this.thingDoing.typeOfThing; if (world == null) { world = World.mainWorld; if (world == null) // not initialized yet, wait { return; } } if (updateAction.Do()) { //Debug.Log("updating action"); float totalWeight = 0.0f; for (int i = 0; i < actionWeights.Length; i++) { actionWeights[i] = actionBaseWeights[i] + GetWeight((TypeOfThingDoing)i); if (isImportant[i]) { actionWeights[i] = zeroOneThing(actionWeights[i], importantMaxValues[i]); } totalWeight += actionWeights[i]; } if (totalWeight <= 0.0f) { totalWeight = 1.0f; } // randomly sample proportional to weights float randVal = Random.value; float cumulSum = 0.0f; int chosenThing = 0; for (int i = 0; i < (int)TypeOfThingDoing.MaxValue; i++) { cumulSum += actionWeights[i] / totalWeight; if (randVal <= cumulSum) { chosenThing = i; break; } } int curAct = (int)thingDoing.typeOfThing; float curWeight = actionWeights[curAct]; float chosenWeight = actionWeights[chosenThing]; float diff = curWeight - chosenWeight; // if current is much more likely, don't change if (diff > 1.0f) { } // otherwise, change with diff pr (if diff < 0 this means always change) else/* if (diff < 0) * { * TypeOfThingDoing chosenThingDoing = (TypeOfThingDoing)chosenThing; * thingDoing = UpdateBehavior(chosenThingDoing); * if (thingDoing.typeOfThing == TypeOfThingDoing.GettingFood) * { * OnSearchForFood(out lookingForBlock, out blockLookingFor); * } * } * else if (Random.value < diff)*/ { TypeOfThingDoing chosenThingDoing = (TypeOfThingDoing)chosenThing; ThingDoing prevThingDoing = thingDoing; thingDoing = UpdateBehavior(chosenThingDoing); if (thingDoing != prevThingDoing) { iNeedToPathfindAgain = true; } if (thingDoing.typeOfThing == TypeOfThingDoing.GettingFood) { OnSearchForFood(out lookingForBlock, out blockLookingFor); } } } if (thingDoing.typeOfThing == TypeOfThingDoing.GettingFood || thingDoing.typeOfThing == TypeOfThingDoing.RunningAway || thingDoing.typeOfThing == TypeOfThingDoing.Gathering || thingDoing.typeOfThing == TypeOfThingDoing.GoingTo) { UpdatePathing(); } else if (thingDoing.typeOfThing == TypeOfThingDoing.Socializing) { thingDoing.typeOfThing = TypeOfThingDoing.Wandering; } else if (thingDoing.typeOfThing == TypeOfThingDoing.Standing) { GetComponent <MovingEntity>().desiredMove = Vector3.zero; } else if (thingDoing.typeOfThing == TypeOfThingDoing.Wandering) { if (updateWander.Do()) { //int randVal = Random.Range(0, 5); Vector3[] options = new Vector3[] { Vector3.zero, Vector3.forward, Vector3.right, Vector3.left, Vector3.back }; Vector3 desiredMove = options[Random.Range(0, options.Length)]; MovingEntity me = GetComponent <MovingEntity>(); me.desiredMove = Vector3.zero; me.jumping = false; RaycastResults blockStandingOn = me.BlockStandingOn(); if (blockStandingOn != null && desiredMove != Vector3.zero) { LVector3 myPos = blockStandingOn.hitBlock + new LVector3(0, 1, 0); LVector3 nextPos = myPos + new LVector3((long)desiredMove.x, (long)desiredMove.y, (long)desiredMove.z); LVector3 belowNextPos = nextPos + new LVector3(0, -1, 0); LVector3 aboveNextPos = nextPos + new LVector3(0, 1, 0); bool needsToJump = false; bool okayToMove = false; bool needsToShift = false; // we need to hop up if (nextPos.Block != Example.Air) { // we can hop up if (aboveNextPos.Block == Example.Air) { okayToMove = true; needsToJump = true; } // we can't hop up else { } } // we won't run into anything and can walk there else { // going down if (belowNextPos.Block == Example.Air) { LVector3 belowBelowNextPos = belowNextPos + new LVector3(0, -1, 0); // too far to fall, don't do it if (belowBelowNextPos.Block == Example.Air) { } // down stairs, that's fine else { okayToMove = true; } } // just horizontal, that's fine else { okayToMove = true; } } me.jumping = needsToJump; me.usingShift = needsToShift; if (okayToMove) { me.desiredMove = desiredMove; } } } } }
public abstract bool IsImportant(TypeOfThingDoing thingDoing, ref float maxValue);
public abstract float GetWeight(TypeOfThingDoing thingDoing);
public abstract ThingDoing UpdateBehavior(TypeOfThingDoing newTypeOfThingDoing);
public ThingDoing(TypeOfThingDoing typeOfThing, ThingDoingTarget target = null) { this.target = target; this.typeOfThing = typeOfThing; }