void Ghost::calcPath(Point ninman) { int dist, shortest = -1; int x = this->x; int y = this->y; Point bestMove; while (shortest != 0 && shortest != INT_MAX) { shortest = INT_MAX; if (map->isValidCoordinate(x,y+1)) { bestMove.x = x; bestMove.y = y + 1; if (!this->alreadyInList(Point(x, y + 1))){ shortest = ManhattanDist(Point(x, y + 1),ninman); } } if (map->isValidCoordinate(x+1,y)) { dist = ManhattanDist(Point(x + 1, y),ninman); if (dist < shortest && !this->alreadyInList(Point(x + 1, y))) { bestMove.x = x + 1; bestMove.y = y; shortest = dist; } } if (map->isValidCoordinate(x,y-1)) { dist = ManhattanDist(Point(x, y - 1),ninman); if (dist < shortest && !this->alreadyInList(Point(x, y-1))) { bestMove.x = x; bestMove.y = y - 1; shortest = dist; } } if (map->isValidCoordinate(x-1,y)) { dist = ManhattanDist(Point(x - 1, y),ninman); if (dist < shortest && !this->alreadyInList(Point(x -1, y))) { bestMove.x = x - 1; bestMove.y = y; shortest = dist; } } ghost_path.push_back(bestMove); x = bestMove.x; y = bestMove.y; printf("%d %d %d\n",x,y,dist); } }
float WorleyNoise::Noise(Vector3 pos, int seed, int distanceFunc, vector<float> distances, int combine) { unsigned int lRand, numberfeatPts; Vector3 rDiff, featPt; float cubeX, cubeY, cubeZ; //Initialize values in distance array to large values for (int i = 0; i < distances.size(); i++) { distances[i] = 6666; } //1. Determine which cube the evaluation point is in int evalCubeX = (int)floor(pos.x); int evalCubeY = (int)floor(pos.y); int evalCubeZ = (int)floor(pos.z); for (int i = -1; i < 2; ++i) for (int j = -1; j < 2; ++j) for (int k = -1; k < 2; ++k) { cubeX = evalCubeX + i; cubeY = evalCubeY + j; cubeZ = evalCubeZ + k; //2. Generate a reproducible random number generator for the cube lRand = lcgRandom(hash((unsigned int)(cubeX+seed), (unsigned int)(cubeY), (unsigned int)(cubeZ))); //3. Determine how many feature points are in the cube numberfeatPts = probLookup(lRand); //4. Randomly place the feature points in the cube for (unsigned int l = 0; l < numberfeatPts; ++l) { lRand = lcgRandom(lRand); rDiff.x = (float)lRand / 0x100000000; lRand = lcgRandom(lRand); rDiff.y = (float)lRand / 0x100000000; lRand = lcgRandom(lRand); rDiff.z = (float)lRand / 0x100000000; featPt = Vector3(rDiff.x+cubeX, rDiff.y+cubeY, rDiff.z+cubeZ); //5. Find the feature point closest to the evaluation point. //This is done by inserting the distances to the feature points into a sorted list float dist = 0; if (distanceFunc == EUCLIDIAN) dist = EuclidianDist(pos, featPt); if (distanceFunc == MANHATTAN) dist = ManhattanDist(pos, featPt); if (distanceFunc == CHEBYSHEV) dist = ChebyshevDist(pos, featPt); float temp; //sorted insertion for (int i = (int)distances.size()-1; i >= 0; i--) { if (dist > distances[i]) break; temp = distances[i]; distances[i] = dist; if (i+1 < (int)distances.size()) distances[i+1] = temp; } } //6. Check the neighboring cubes to ensure their are no closer evaluation points. // This is done by repeating steps 1 through 5 above for each neighboring cube } float color = 0.0f; if (combine == D1) color = distances[0]; if (combine == D2) color = distances[1] - distances[0]; if (combine == D3) color = distances[2] - distances[0]; if(color < 0.0f) color = 0.0f; if(color > 1.0f) color = 1.0f; return color; }