public void SpawnUnits() { #if CLIENT if (null == state) { InfoManager.Log(noWorldStateEntry); return; } if (null == unitPrefab) { InfoManager.Log(missingUnitPrefabEntry); return; } Vector3 spawnPos; GameObject instantiated; Unit unit; foreach (int unitPosition in state.positions) { spawnPos = GetPositionFromIndex(unitPosition); instantiated = (GameObject)Instantiate(unitPrefab, spawnPos, Quaternion.identity, instantiatedPrefabsCatalog); unit = instantiated.GetComponent <Unit>(); unit.position = unitPosition; } #endif // CLIENT }
protected override void OnMessageReceived(int _socket, int _connectionID, int _channelID, ref byte[] _buffer, int _bufferSize) { InfoManager.Log(identifier + sbName + _connectionID + sbSentAMessage); received = ProcessMessage(_buffer); ClientSide.instance.ProcessRespond(received); }
protected override void OnMessageReceived(int _socket, int _connectionID, int _channelID, ref byte[] _buffer, int _bufferSize) { InfoManager.Log(identifier + sbName + _connectionID + sbSentAMessage); received = ProcessMessage(_buffer); ServerSide.ProcessRequest(received, _connectionID); }
void Awake() { if (null == instances) { instances = new List <Unit>(); } instances.Add(this); if (!Mathf.Approximately(1.0f, moveCurve.Evaluate(1.0f))) { InfoManager.Log(invalidCurveEntry); } _selectionRotationPerSecond = new Vector3(.0f, .0f, selectionRotationPerSecond); if (0 > indicatorsCount) { indicatorsCount = selectionIndicators.Length; speedModifiers = new float[indicatorsCount]; for (int i = 0; i < indicatorsCount; i++) { speedModifiers[i] = Random.Range(1.0f, maxSpeedMultiplier); } } }
public bool Build() { #if CLIENT int size = state.size; #region // Error Handling if (0 >= size) { InfoManager.Log(invalidSizeErrorEntry); return(false); } if (null == wallPrefab || null == cornerPrefab) { InfoManager.Log(invalidPrefabsErrorEntry); return(false); } #endregion // Error Handling int wallsNum = size - 1; float halfSize = (float)size / 2.0f; Vector3 wallStartPos = new Vector3(-halfSize, .0f, -halfSize); Vector3 scalerPPN = new Vector3(1.0f, 1.0f, -1.0f); Vector3 scalerNPP = new Vector3(-1.0f, 1.0f, 1.0f); /* Corners */ Instantiate(cornerPrefab, wallStartPos, Quaternion.Euler(_90dRigth * 2.0f), instantiatedPrefabsCatalog); Instantiate(cornerPrefab, wallStartPos + Vector3.right * size, Quaternion.Euler(_90dRigth), instantiatedPrefabsCatalog); Instantiate(cornerPrefab, wallStartPos + Vector3.forward * size, Quaternion.Euler(_90dRigth * 3.0f), instantiatedPrefabsCatalog); Instantiate(cornerPrefab, wallStartPos + (Vector3.right + Vector3.forward) * size, Quaternion.identity, instantiatedPrefabsCatalog); /* Walls */ for (int i = 0; i < wallsNum; i++) { Instantiate(wallPrefab, Vector3.Scale(wallStartPos + Vector3.forward * (1.0f + i), scalerNPP), Quaternion.identity, instantiatedPrefabsCatalog); Instantiate(wallPrefab, wallStartPos + Vector3.forward * (1.0f + i), Quaternion.Euler(_90dRigth * 2.0f), instantiatedPrefabsCatalog); Instantiate(wallPrefab, wallStartPos + Vector3.right * (1.0f + i), Quaternion.Euler(_90dRigth), instantiatedPrefabsCatalog); Instantiate(wallPrefab, Vector3.Scale(wallStartPos + Vector3.right * (1.0f + i), scalerPPN), Quaternion.Euler(_90dRigth * 3.0f), instantiatedPrefabsCatalog); } return(true); #else return(false); #endif // CLIENT }
public void FormRequestAndSend() { #if CLIENT locked = true; if (null == clientScript) { InfoManager.Log(noNetClientSetEntry); return; } Request request = new Request(); clientScript.DoSend((object)request, true); StartCoroutine(ExpectTimeout()); #endif // CLIENT }
public static WorldState GenerateNewWorldState() { #if SERVER int size; int count; int[] positions; size = Random.Range(instance.minimumSize, instance.maximumSize + 1); count = Random.Range(instance.minimumUnitCount, instance.maximumUnitCount + 1); positions = new int[count]; /* area - площадь */ int area = size * size; int i, j; for (i = 0; i < count; i++) { positions[i] = Random.Range(0, area); for (j = 0; j < i; j++) { if (positions[i] == positions[j]) { --i; break; } } } WorldState result = new WorldState(size, count, ref positions); return(result); #else InfoManager.Log(impossibleToUseEntry); return(null); #endif // SERVER }
public void SpawnGridFloor() { #if CLIENT if (null == state) { InfoManager.Log(noWorldStateEntry); return; } if (null == gridPrefab) { InfoManager.Log(missingGridPrefabEntry); return; } int size = state.size; int area = size * size; float halfSize = (float)size / 2.0f; Vector3 startPos = new Vector3(-halfSize + .5f, .0f, -halfSize + .5f); Vector3 spawnPos; GameObject instantiated; Grid gridBehaviour; for (int i = 0; i < area; i++) { spawnPos = startPos + Vector3.right * (i % size) + Vector3.forward * (i / size); instantiated = (GameObject)Instantiate(gridPrefab, spawnPos, Quaternion.identity, instantiatedPrefabsCatalog); gridBehaviour = instantiated.GetComponent <Grid>(); gridBehaviour.index = i; } #endif // CLIENT }
/* Threads */ private void _findpath() { /* !!! no UnityEngine code must be used here !!! */ success = false; isReady = false; #region // Local Variables int size = worldState.size; int selectedUnitsCount = request.unitsPositions.Length; int overallUnitsCount = worldState.positions.Length; int commandsCount; int i, j; /* per node data */ int pos; int depth; int distance; int leftPos; int rightPos; int topPos; int downPos; TreeNode currentNode; TreeNode localDestinationNode; bool addedToTree; bool flagToContinue; /* used for distance calculation */ int minDistance; int[] destinationCoordinates = new int[2]; int[] nodeCoordinates = new int[2]; int[] delta = new int[2]; #endregion // Local Variables commands = new int[selectedUnitsCount][]; /* movement graphs associated to each of moving units */ Dictionary <int, List <TreeNode> > tree = new Dictionary <int, List <TreeNode> >(); Queue <TreeNode> nodesToCheck = new Queue <TreeNode>(); /* used for distance colculations */ destinationCoordinates[0] = request.destination % size; destinationCoordinates[1] = request.destination / size; #region // sorting moving units by distance to destination /* This action saves in half of collision scenarios */ for (i = 0; i < selectedUnitsCount; i++) { for (j = 0; j < i; j++) { delta[0] = destinationCoordinates[0] - request.unitsPositions[i] % size; delta[0] = (0 < delta[0])?(delta[0]):(-delta[0]); delta[1] = destinationCoordinates[1] - request.unitsPositions[i] / size; delta[1] = (0 < delta[1])?(delta[1]):(-delta[1]); distance = delta[0] + delta[1]; delta[0] = destinationCoordinates[0] - request.unitsPositions[j] % size; delta[0] = (0 < delta[0])?(delta[0]):(-delta[0]); delta[1] = destinationCoordinates[1] - request.unitsPositions[j] / size; delta[1] = (0 < delta[1])?(delta[1]):(-delta[1]); if (distance < delta[0] + delta[1]) { int buf = request.unitsPositions[i]; request.unitsPositions[i] = request.unitsPositions[j]; request.unitsPositions[j] = buf; } } } #endregion // sorting moving units by distance to destination for (i = 0; i < selectedUnitsCount; i++) { #region // movement graph building /* This is a BFS search of a destination node */ /* One grapgh is created per moving unit since destination node depth would vary */ InfoManager.Log(identifier + processingEntry + i); tree.Add(i, new List <TreeNode>()); nodesToCheck.Clear(); TreeNode startNode = new TreeNode(request.unitsPositions[i], 0, null); tree[i].Add(startNode); nodesToCheck.Enqueue(startNode); do { #region // prerequisites flagToContinue = false; addedToTree = false; currentNode = nodesToCheck.Dequeue(); pos = currentNode.position; depth = currentNode.depth; #endregion // prerequisites if (-1 == pos) { continue; } if (killswitch) { InfoManager.Log(threadEndedExternallyEntry); isReady = true; return; } #region // check for collisions /* if non-moving unit occupies this position */ for (j = 0; j < overallUnitsCount; j++) { if (pos == worldState.positions[j]) { flagToContinue = true; } } /* if moving units occupies this place at same depth */ for (j = 0; j < i; j++) { for (int k = 0; k < commands[j].Length; k++) { if (pos == commands[j][k] && k == depth) { flagToContinue = true; } } } if (flagToContinue) { continue; } #endregion // check for collisions #region // add node to tree or replace existing node int treeDepth = tree[i].Count; for (j = 0; j < treeDepth; j++) { if (tree[i][j].position == pos) { if (tree[i][j].depth >= depth) { /* replace other nodes back tracking links with current node */ for (int k = 0; k < treeDepth; k++) { if (tree[i][k].back == tree[i][j]) { tree[i][k].back = currentNode; } } tree[i][j] = currentNode; addedToTree = true; break; } else { /* tree already contains a better node */ flagToContinue = true; break; } } } if (flagToContinue) { continue; } if (!addedToTree) { addedToTree = true; tree[i].Add(currentNode); } #endregion // add node to tree or replace existing node #if !FULLBFS /* if destination node was reached */ if (addedToTree && request.destination == pos) { break; } #endif // !FULLBFS #region // stack adjacent positions if (addedToTree) { /* position of a node that leads back */ int rejected = (null != currentNode.back)?(currentNode.back.position):(-1); topPos = (0 < pos / size)?(pos - size):(-1); if (rejected != topPos) { nodesToCheck.Enqueue(new TreeNode(topPos, depth + 1, currentNode)); } rightPos = (size - 1 > pos % size)?(pos + 1):(-1); if (rejected != rightPos) { nodesToCheck.Enqueue(new TreeNode(rightPos, depth + 1, currentNode)); } downPos = (size - 1 > pos / size)?(pos + size):(-1); if (rejected != downPos) { nodesToCheck.Enqueue(new TreeNode(downPos, depth + 1, currentNode)); } leftPos = (0 < pos % size)?(pos - 1):(-1); if (rejected != leftPos) { nodesToCheck.Enqueue(new TreeNode(leftPos, depth + 1, currentNode)); } } #endregion // stack adjacent positions } while(0 != nodesToCheck.Count); #endregion // movement graph building #region // looking for local destination node and forming commands array localDestinationNode = tree[i][0]; minDistance = -1; foreach (TreeNode node in tree[i]) { flagToContinue = false; /* if not occupied by other commands later values */ for (j = 0; j < i; j++) { commandsCount = commands[j].Length; if (0 < commandsCount && commands[j][commandsCount - 1] == node.position) { flagToContinue = true; break; } } if (flagToContinue) { continue; } #region // computing distance (no squares for performance) nodeCoordinates[0] = node.position % size; nodeCoordinates[1] = node.position / size; delta[0] = nodeCoordinates[0] - destinationCoordinates[0]; delta[0] = (0 <= delta[0])?(delta[0]):(-delta[0]); delta[1] = nodeCoordinates[1] - destinationCoordinates[1]; delta[1] = (0 <= delta[1])?(delta[1]):(-delta[1]); distance = delta[0] + delta[1]; #endregion // computing distance (no squares for performance) if (distance < minDistance || 0 > minDistance) { minDistance = distance; localDestinationNode = node; } } /* Forming commands array */ int outCount = 0; currentNode = localDestinationNode; while (null != currentNode) { outCount++; currentNode = currentNode.back; } commands[i] = new int[outCount]; j = outCount - 1; currentNode = localDestinationNode; while (null != currentNode) { commands[i][j--] = currentNode.position; currentNode = currentNode.back; } #endregion // looking for local destination node and forming commands array } success = true; isReady = true; }
public void ProcessRespond(object receivedRespond) { #if CLIENT if (null == state && receivedRespond is WorldState) { StopAllCoroutines(); state = (WorldState)receivedRespond; startPos = new Vector3(-state.size / 2.0f + .5f, .0f, -state.size / 2.0f + .5f); bool built = Build(); /* Building walls failed */ if (null != instantiatedPrefabsCatalog && !built) { foreach (Transform instantiated in instantiatedPrefabsCatalog) { Destroy(instantiated.gameObject); } InfoManager.Log(failedToBuildEntry); } else if (built) { SpawnUnits(); SpawnGridFloor(); } return; } if (receivedRespond is Respond) { StopAllCoroutines(); Respond respond = (Respond)receivedRespond; if (!respond.success) { return; } int j = 0; foreach (int[] command in respond.commands) { Unit.GetInstance(command[0]).Receive(command); for (int i = j; i < state.positions.Length; i++) { if (command[0] == state.positions[i]) { j = i; state.positions[i] = command[command.Length - 1]; break; } } } return; } #endif // CLIENT }
protected override void OnDisconnected(int _socket, int _connectionID) { ServerSide.instance.OnDisconnected(_connectionID); InfoManager.Log(identifier + sbName + _connectionID + sbHasDisconnected); }