To Assign a random number for the agents inside a node - anylogic

I am building this Ped model , using the pedestrian library .
how can i assign a random number for number of agents inside a node and to call them out to do something based on that number ?

Let's imagine the following scenario, you have a node, with capacity restriction where you get access to the "on enter" action. There, any time a pedestrian enters the node, it will be added to the pedsInNode ArrayList.
then the button does something with those agents, for example freeing them from the pedWait block as follows:
int x=uniform_discr(1,4);//random number between 1 and 4
int count=0;
ArrayList <Pedestrian> peds=new ArrayList();
for(Pedestrian p : pedsInNode){
peds.add(p);
count++;
if(count==x)
break;
}
for(Pedestrian p : peds){
pedWait.free(p);
pedsInNode.remove(p);
}

Related

Calculated field affected the Grand Total in Tableau

Used a calculated field by using the window_max and window_min functions:
IF
([eCPM]) = WINDOW_MAX(([eCPM]))
THEN "Least efficient" ////Red
ELSEIF [eCPM] = WINDOW_MIN(([eCPM]))
THEN "Most efficient CPM" ///Green
ELSE "Neither" ///Gray
END
But this also affected my Grand Total. I don't want any coloring in totals. How to handle this? (Solved)
Since my calculation is based upon eCPM, can only this column be highlighted as green rather entire row, without highlighting media cost and visits green as well?
Since my calculation is based upon eCPM, can only this column be highlighted as green rather entire row, without highlighting media cost and visits green as well?
You just need to "wrap" you if statement with another one in order to handle the grand total using size which returns the number of rows in the partition.
Using the superstore you can create a calculated field like this:
// Handle Grand Total
IF SIZE() = 1 THEN "Neutral"
// Handle all other "rows"
ELSE
IF sum([Sales]) = WINDOW_MAX(sum([Sales]))
THEN "Green"
ELSEIF sum([Sales]) = WINDOW_MIN(sum([Sales]))
THEN "Red"
ELSE "Neutral"
END
END
The result could look like this:

Sprite Kit: Waiting to call a function until condition X is met

I'm working on a game (top-down shooter) and have run into a bit of a snag. Up to this point, I've spawned enemies with functions that just work with delays:
Wave One Function - Delay 3, spawn enemies //
Wave Two Function - Delay 6, spawn enemies
I do this because I haven't found a way to wait for all actions in a given function to complete before calling the next - as things stand, functionWaveOne calls functionWaveTwo, which calls functionWaveThree (etc).
This has worked until now. What's changed is I want two enemies to remain on-screen and until they're dead, I don't want the next wave to come. My initial solution to this was to have a running count of how many enemies died in wave four:
Detect collision -> Apply Damage -> Check if dead -> If yes, deadWaveFourEnemies++
Here's my problem: I have a primary weapon that's two parallel lasers, so they have potential to collide with an enemy at exactly the same time. This results in false positives, making the dead enemy count go higher than it should. I even tried adding an "am I alive" function to the enemy class, but to no avail.
Have any of you got a recommendation on how to either call functions in a better way or to get around these false positives? Thanks!
In case it helps:
if([enemySprite.name isEqual: #"shooter"]){
enemyShooter *enemyClass = (enemyShooter *)enemySprite;
itDied = [enemyClass doDamageWithAmount:secondaryWeaponDamage];
scoreIncrease = [enemyClass getScoreIncrease];
stillAlive = [enemyClass amIAlive];
}
[weaponSprite removeFromParent];
if(itDied){
[self increasePlayerScoreBy:scoreIncrease];
if(inWaveFour == 1 && stillAlive == 1){
waveFourKilled++;
NSLog(#"Seconday / Number killed: %i", waveFourKilled);
}
}

Multiple for loops executing at the same time? Swift

Basically I am creating a game that spawns a ball after a certain amount of time has passed. However, as time goes on and the player score increases, balls that are more difficult to kill will spawn.
Prior to this, all the spawn methods I have set in place function as I would like them to, but they were contained in a switch statement that calculated the probability a ball would spawn.
//pseudo:
let spawnProbability = arc4random_uniform(100)
switch spawnProbability {
case 0…60: //60% spawn chance
//spawn small ball
case 61…90: //~30% spawn chance
//spawn medium ball
case 91…100: //~9% chance
//spawn large ball
default:
break
}
This worked as a plan B, but I'd like to implement what I originally had designed which was the method described above.
My thought process to accomplish what I originally had in mind is:
for loop that spawns small balls until the score counter reached a certain amount
after the score counter reached, say, 100pts, another for loop would be executed in addition to the first loop. So, medium and small balls spawn after a delay.
after the score counter reached another mark, large balls would begin to spawn on top of medium balls and small balls.
Here's what I mean: (pseudocode again)
for(var i = 0; i < 10; i++) {
//delay
//spawn small ball
}
if score >= 100 {
for (var x = 0; x < 10; x++) {
//delay
//spawn medium ball
}
}
// . . . and so on
I've put alot of thought into how I could accomplish what I want but I can't come up with a better approach to this problem. I understand why this isn't working and why multiple for loops can't be executed with delay in the same class at the same time (or maybe they can and I'm just going about it in the wrong way) but I'm stuck in the sense that I don't know a better way to implement the spawn technique that I want.. Any help is greatly appreciated!!

MoveTowards not working properly

I want to move toward each object from my array, I mean move first to [0] then [1] and so on. I'm having problems with animations.
function exit( path : Array){
var node : Node;
var go : GameObject;
for(var i=1 ; i < path.length ; i++){
node = path[i] as Node;
go = node.getContent() as GameObject;
nodeS.getContent().transform.position = Vector3.MoveTowards(nodeS.getContent().transform.position, go.transform.position ,1);
}
}
I call exit function inside Update(). As a result, I'm getting weird movements, and I understand that the problem is between for loop and Update function.
You get the weird movements, because in each Update() (each call of your exit()), you go over all of the nodes in the path and move just one step towards them. It's because Vector3.MoveTowards only calculates the next step of the movement from current to target. The size of your step is "1" (the third parameter of Vector3.MoveTowards())
So basically, in each Update(), this happens:
move 1 unit closer to the first node of path
move 1 unit closer to the second node of path
move 1 unit closer to the third node of path
and so on
So, I'd do it somehow like this:
function exit(path : Array) {
var node : Node;
var go : GameObject;
node = path[CurrentNodeIndex] as Node;
go : GameObject = node.getContent() as GameObject;
if (!Mathf.Approximately(
Vector3.Distance(nodeS.getContent().transform.position, go.transform.position),
0
)) {
nodeS.getContent().transform.position = Vector3.MoveTowards(nodeS.getContent().transform.position, go.transform.position, 1);
} else {
CurrentNodeIndex++;
Debug.Log("Starting to move towards next node: " + CurrentNodeIndex);
if (CurrentNodeIndex >= path.length) {
Debug.Log("The final node was reached!");
return;
}
}
}
What this does:
Get the first node from the path (CurrentNodeIndex is 1 at the beginning)
if the current distance between this node and nodeS (using Vector3.Distance) is not approximately 0 (using Mathf.Approximately), move one step closer to the node (using Vector3.MoveTowards)
this is called every Update until the distance between the two nodes is approximately 0. Then the CurrentNodeIndex is increased.
in next Update, select the next node from path and repeat the process - start moving towards it
when the last node from path was reached, just return. You can also have some global private bool flag indicating this and optimize
Please forgive any compilation errors; I don't use Unityscript, but C#, and have no means to test it right now.
BTW, is there any reason why you start taking notes from path with index 1 and not 0? If you meant to start with the actual first node of path, initialize CurrentNodeIndex to 0 instead of 1.

Compare value of property of all instances of class in python

I'm trying to write an object oriented program (as a learning exercise, I know there may be simpler ways to do it) in which beads bounce around a 2D plane bounded by a ring. Each bead is an object defined by a class ball. In setting the initial positions of the balls I need to check that no other ball has already been placed at the same x and y coordinates.
#Class for the beads
class ball:
NumbBalls = 0
#Constructor
def __init__(self,Beads):
self.ball = sphere(pos = vector(0,0,0), radius = radiusBall,color=color.red)
ball.NumbBalls += 1
self.ball.pos = self.createInitialPosition(Beads)
#Assign ball its initial position
def createInitialPosition(self,other):
#CODE to compare self.ball.pos and other.ball.pos which returns a unique position coordinate
#Main program
NumbBeads = 100
Beads = []#Create empty list for all the beads
#create balls in random initial positions
Beads = [ball(Beads) for item in range(NumbBeads)]
I can get this to work if I create two objects bead1 and bead2 and then pass bead1 and bead2 as arguments ie bead2 = ball(bead1) but how do I do this if I am generating a list of beads and want all of them to be compared with self. Hopefully that makes sense.
Perhaps rather than the approach you are currently taking, you should consider something along these lines (of course, with the necessary changes to your class definition/methods):
N = 100
initialPositions = []
while len(initialPositions) <= N:
newPosition = generateRandomPosition()
if newPosition in initialPositions:
pass
else:
initialPositions.append(newPosition)
ballList = [ ball(p) for p in initialPositions ]
In other words, generate a list of initial positions outside of the creation of your objects, and do whatever validation/restriction you need during that creation. Then just create your objects from that list. If N is really large, you might want to consider a dictionary (or a mutable set of some sort) instead of a list for initialPositions, to help speed up the membership testing, but it's still the same concept...