/// <summary> /// Sets the value of the byte in position <c>v3ToIndx(v)</c> /// </summary> /// <param name="v"></param> /// <param name="val"></param> /// <returns>Returns TRUE if the value has been changed, FALSE otherwise</returns> public bool setValue(Vector3 v, byte val) { indx i = v3ToIndx(v); if (dataArray[i.x, i.z] != val && dataArray[i.x, i.z] != 1) { if (val == 1) { dirtyValues.AddLast(i); } dataArray[i.x, i.z] = val; return(true); } return(false); }
/// <summary> /// Each drone has to call this function to obtain a ticket used for updating their position in the drawer /// <para>The first drone that calls this function determine the zeroPointPosition</para> /// </summary> /// <param name="dronePosition">The actual position of the drone</param> /// <returns>Returns a ticket that will be used for updating the position</returns> public int initialize(Vector3 dronePosition) { if (!initialized) { // the first drone that register itself determine the zeroPointPosition, that correspond to the center of the map dronesPositions = new indx[10]; indx actualPosition = new indx(dataMaxSize / 2, dataMaxSize / 2); data = new Data(dataMaxSize, maxExplorableDistance, dronePosition, actualPosition); dW.setData(data); initialized = true; } // other drones just takes a ticket to actualize their position dronesPositions[ticket] = new indx(dronePosition); return(ticket++); }
/// <summary> /// Constructor /// <para>Associates 'Vector3 zeroPoint' to 'Indx zeroPointInData' </para> /// </summary> /// <param name="dataArraySize">Size of the matrix</param> /// <param name="maxExplorableDistance">Size of the area we can explore</param> /// <param name="zeroPoint">Initial point represented by world-coordinates </param> /// <param name="zeroPointInData">Initial point represented by an indx</param> public Data(int dataArraySize, float maxExplorableDistance, Vector3 zeroPoint, indx zeroPointInData) { dirtyValues = new LinkedList <indx>(); arraySize = dataArraySize; dataArray = new byte[dataArraySize, dataArraySize]; for (int i = 0; i < dataArraySize; i++) { for (int j = 0; j < dataArraySize; j++) { dataArray[i, j] = 2; } } this.zeroPoint = zeroPoint; this.zeroPointInData = zeroPointInData; this.maxExplorableDistance = maxExplorableDistance; }
/// <summary> /// Returns true if there are no walls or unknown cells in the square with center 'v' and specified radius /// </summary> /// <param name="v">Center taken in consideration to explore if the cells are all free</param> /// <param name="radius">Radius of the search</param> /// <returns>0 -> areAllFree; 1 -> there are walls; 2 -> no walls but unknown cells</returns> public int areAllFree(Vector3 v, int radius) { bool thereAreUnknown = false; indx center = v3ToIndx(v); for (int i = center.x - radius / 2; i <= center.x + radius / 2; i++) { for (int j = center.z - radius / 2; j <= center.z + radius / 2; j++) { if (dataArray[i, j] == 1) { return(1); } else if (dataArray[i, j] == 2) { thereAreUnknown = true; } } } return(thereAreUnknown ? 2 : 0); }
/// <summary> /// Subtraction of Indxs /// </summary> /// <param name="i">Other Indx we'll subtract to this</param> /// <returns>Returns the indx resulting from the subtraction of the Indx passed as argument to the this Indx</returns> public indx subtract(indx i) { return(new indx(x - i.x, z - i.z)); }
/// <summary> /// Sum of Indxs /// </summary> /// <param name="i">Other Indx we'll sum to this</param> /// <returns>Returns the indx resulting from the sum of the current Indx and the one passed as argument</returns> public indx add(indx i) { return(new indx(x + i.x, z + i.z)); }
/// <summary> /// Gets the value in the matrix, corresponding to the real-world coordinates specified by the Vector3 /// </summary> /// <param name="v">Vector3 specifying the real-world coordinates of the point we are interested</param> /// <returns>Returns the value in the matrix at position <c>v3ToIndx(v)</c></returns> public int getValue(Vector3 v) { indx i = v3ToIndx(v); return(getValue(i.x, i.z)); }
/// <summary> /// Converts the Indx in the matrix to a real-world coordinate /// </summary> /// <param name="i">Indx we want to convert</param> public Vector3 indxToV3(indx i) { return(i.subtract(zeroPointInData).multiplyFloat(maxExplorableDistance / arraySize) + zeroPoint); }
/// <summary> /// Returns true if the two objects are equals /// </summary> public static bool Equals(indx i1, indx i2) { return(i1.x == i2.x && i1.z == i2.z); }