NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class AIRobotCard : AIBoosterCard
{
List<Vector2Int> triggeredPos = new List<Vector2Int>();

#region Abstracts
public override IEnumerator UseBoosterCard(int boosterCardId, int delayScale, ItemTypes exceptSpecified = ItemTypes.None)
{
List<CellSlot> matchList = new List<CellSlot>();
Dictionary<string, object> boosterCardData = new Dictionary<string, object>();

List<Vector2Int> availablePositionsOnBoard = new List<Vector2Int>();
List<Vector2Int> selectedPositionsOnBoard = new List<Vector2Int>();

List<CellSlot> cells = new List<CellSlot>();

for (int i = 0; i < aiController.aiBoardManager.boardBorders.y; i++)
{
for (int j = 0; j < aiController.aiBoardManager.boardBorders.x; j++)
{
CellSlot cellSlot = aiController.aiBoardManager.boardMatrix[j, i];
if (cellSlot.itemType == ItemTypes.Cell)
cells.Add(cellSlot);
}
}

object rawDronesOnBoard;
aiController.aiMoveHandler.aiGameData.TryGetValue("spawnedBCI", out rawDronesOnBoard);
List<CellSlot> dronesOnBoard = (rawDronesOnBoard != null) ? Utility.CastToType<List<CellSlot>>(rawDronesOnBoard) : new List<CellSlot>();

List<Vector2Int> latestPosOnBoard = new List<Vector2Int>();
for (int i = 0; i < dronesOnBoard.Count; i++)
{
//if drone is on board (id != "") AI target is cell which is below drone, if not this case drone is activated and staying position so drone is AI target
CellSlot targetCell = dronesOnBoard[i].id == "" ? dronesOnBoard[i] : cells.Find(cell => cell.id == dronesOnBoard[i].id);
latestPosOnBoard.Add(targetCell.positionOnBoard);
}

int cellCount = cells.Count;
for (int i = 0; i < cellCount; i++)
{
CellSlot cellSlot = cells[i];

if (!latestPosOnBoard.Contains(cellSlot.positionOnBoard))
availablePositionsOnBoard.Add(cellSlot.positionOnBoard);
}

availablePositionsOnBoard.Shuffle();

int upperBounds = dronesOnBoard.Count == 0 ? 4 : 2;
int maxLen = Mathf.Min(upperBounds, availablePositionsOnBoard.Count);

for (int j = 0; j < maxLen; j++)
{
Vector2Int posOnBoard = availablePositionsOnBoard[j];
selectedPositionsOnBoard.Add(posOnBoard);

int x = posOnBoard.x;
int y = posOnBoard.y;
for (int i = 0; i < aiController.aiBoardManager.boardBorders.x; i++)
{
CellSlot cellSlot = aiController.aiBoardManager.boardMatrix[i, y];
if (cellSlot.itemType != exceptSpecified && !matchList.Contains(cellSlot))
matchList.Add(cellSlot);
}

for (int i = 0; i < aiController.aiBoardManager.boardBorders.y; i++)
{
CellSlot cellSlot = aiController.aiBoardManager.boardMatrix[x, i];
if (cellSlot.itemType != exceptSpecified && !matchList.Contains(cellSlot))
matchList.Add(cellSlot);
}
}

boosterCardData.Add("selectedCells", selectedPositionsOnBoard);

int possibleDelay = Mathf.Min(2, delayScale);
int aiDelay = Random.Range(1, possibleDelay + 1);
yield return new WaitForSeconds(aiDelay);

float currentRoundTimer = aiController.matchController.GetRoundTimer();
if (currentRoundTimer <= 0f)
yield break;

aiController.aiMoveHandler.SendBoosterRequest(matchList, boosterCardData, boosterCardId);
}

public override void NullPositionOnMatrixByBoosterCard(Dictionary<string, object> move, ref float totalDelay, int whoMadeMove, Dictionary<ItemTypes, float> delays, List<string> instigators, string instigatorId, bool checkType, ItemTypes exceptSpecified)
{
int boosterEventId = Utility.ConvertToInt(Utility.GetPropertyByNameV2("boosterEventId", move));

List<Vector2Int> selectedCells = Utility.ConvertToListV2Int(Utility.GetPropertyByNameV2("selectedCells", move));
object rawDronesOnBoard;
aiController.aiMoveHandler.aiGameData.TryGetValue("spawnedBCI", out rawDronesOnBoard);
List<CellSlot> dronesOnBoard = (rawDronesOnBoard != null) ? Utility.CastToType<List<CellSlot>>(rawDronesOnBoard) : new List<CellSlot>();
float droneTriggerTime = 0;
for (int i = 0; i < selectedCells.Count; i++)
{
Vector2Int posOnBoard = selectedCells[i];
dronesOnBoard.Add(new CellSlot() { itemType = ItemTypes.DroneCell, positionOnBoard = posOnBoard, neighbours = new List<CellSlot>() { aiController.aiBoardManager.boardMatrix[posOnBoard.x, posOnBoard.y] }, id = "" });
float tempDroneTriggerTime = GetDroneTriggerTime(posOnBoard);
if (tempDroneTriggerTime > droneTriggerTime)
droneTriggerTime = tempDroneTriggerTime;

int x = posOnBoard.x;
int y = posOnBoard.y;

for (int j = 0; j < aiController.aiBoardManager.boardBorders.x; j++)
{
CellSlot cellSlot = aiController.aiBoardManager.boardMatrix[j, y];
if (cellSlot.itemType != exceptSpecified)
aiController.aiBoardManager.NullPositionOnMatrix(cellSlot, delays, ref totalDelay, whoMadeMove, boosterEventId, instigators, instigatorId, checkType, exceptSpecified);
}

for (int j = 0; j < aiController.aiBoardManager.boardBorders.y; j++)
{
CellSlot cellSlot = aiController.aiBoardManager.boardMatrix[x, j];
if (cellSlot.itemType != exceptSpecified)
aiController.aiBoardManager.NullPositionOnMatrix(cellSlot, delays, ref totalDelay, whoMadeMove, boosterEventId, instigators, instigatorId, checkType, exceptSpecified);
}
}

aiController.aiMoveHandler.aiGameData["spawnedBCI"] = dronesOnBoard;

float increaseRate = .2f;
float moveTime = 1f;
totalDelay += 1.1f + moveTime * 2f + (selectedCells.Count * increaseRate * 2) + droneTriggerTime; //1.1f -> .6(ui tween time) + .5f(ui hide time)
//movetime * 2f ->one is drone reveal from bottom to top ui, the other one is for replace position on the board
//(selectedCells.Count * increaseRate * 2) -> ui drone delay(sequential reveal, sequential replace)
}
#endregion

#region Virtuals
public override void CheckPossibleBoosterCardMatch(CellSlot cellItem, List<CellSlot> visitedCells, List<CellSlot> matchedFruitPlateCells, List<Vector2Int> mergedBoosterPos, MoveType moveType, ref int point, ref int boosterPoint, ItemTypes modeItemType)
{
object rawDronesOnBoard;
if (!aiController.aiMoveHandler.aiGameData.TryGetValue("spawnedBCI", out rawDronesOnBoard))
return;

List<CellSlot> dronesOnBoard = (rawDronesOnBoard != null) ? Utility.CastToType<List<CellSlot>>(rawDronesOnBoard) : new List<CellSlot>();

if (dronesOnBoard == null || dronesOnBoard.Count == 0)
return;

FindPossibleBoosterCardMatch(cellItem, visitedCells, matchedFruitPlateCells, mergedBoosterPos, dronesOnBoard, moveType, ref point, ref boosterPoint, modeItemType);
}

public override void CheckBCIOnCellTrigger(Vector2Int positionOnBoard, string cellId, int boosterEventId, ref float totalDelay, int whoMadeMove, Dictionary<ItemTypes, float> delays, List<string> instigators, string instigatorId, bool checkType, ItemTypes exceptSpecified)
{
object rawDronesOnBoard;
if (!aiController.aiMoveHandler.aiGameData.TryGetValue("spawnedBCI", out rawDronesOnBoard))
return;

List<CellSlot> dronesOnBoard = (rawDronesOnBoard != null) ? Utility.CastToType<List<CellSlot>>(rawDronesOnBoard) : new List<CellSlot>();

CellSlot targetDrone = new CellSlot() { positionOnBoard = new Vector2Int(-1, -1) };
int droneIndex = dronesOnBoard.FindIndex(drone => drone.id == cellId);

if (droneIndex < 0)
return;

targetDrone = dronesOnBoard[droneIndex];

targetDrone.positionOnBoard = positionOnBoard;
targetDrone.id = "";
dronesOnBoard[droneIndex] = targetDrone;

aiController.aiMoveHandler.aiGameData["spawnedBCI"] = dronesOnBoard;
TriggerDrone(positionOnBoard, boosterEventId, ref totalDelay, whoMadeMove, delays, instigators, instigatorId, checkType, exceptSpecified);
}

public override void ChangeBoosterCardItemsPos()
{
object rawDronesOnBoard;
aiController.aiMoveHandler.aiGameData.TryGetValue("spawnedBCI", out rawDronesOnBoard);
List<CellSlot> dronesOnBoard = (rawDronesOnBoard != null) ? Utility.CastToType<List<CellSlot>>(rawDronesOnBoard) : new List<CellSlot>();

List<CellSlot> activatedDrone = dronesOnBoard.FindAll(drone => drone.id == "");
List<CellSlot> cells = new List<CellSlot>();

for (int i = 0; i < aiController.aiBoardManager.boardBorders.y; i++)
{
for (int j = 0; j < aiController.aiBoardManager.boardBorders.x; j++)
{
CellSlot cellSlot = aiController.aiBoardManager.boardMatrix[j, i];
cells.Add(cellSlot);
}
}

List<Vector2Int> latestPosOnBoard = new List<Vector2Int>();
for (int i = 0; i < dronesOnBoard.Count; i++)
{
//if drone is on board (id != "") AI target is cell which is below drone, if not this case drone is activated and staying position so drone is AI target
CellSlot targetCell = dronesOnBoard[i].id == "" ? dronesOnBoard[i] : cells.Find(cell => cell.id == dronesOnBoard[i].id);
latestPosOnBoard.Add(targetCell.positionOnBoard);
}

List<Vector2Int> availablePositionsOnBoard = new List<Vector2Int>();
Dictionary<int, string> selectedPositionsOnBoard = new Dictionary<int, string>();

int cellCount = cells.Count;
for (int i = 0; i < cellCount; i++)
{
CellSlot cellSlot = cells[i];

if (cellSlot.itemType == ItemTypes.Cell && !latestPosOnBoard.Contains(cellSlot.positionOnBoard))
availablePositionsOnBoard.Add(cellSlot.positionOnBoard);
}

availablePositionsOnBoard.Shuffle();

for (int i = 0; i < activatedDrone.Count; i++)
{
int droneIndex = GetDroneIndex(dronesOnBoard, activatedDrone[i]);
if (droneIndex == -1)
continue;

Vector2Int posOnBoard = availablePositionsOnBoard[i];
selectedPositionsOnBoard.Add(droneIndex, $"{posOnBoard.x},{posOnBoard.y}");
}

Dictionary<string, object> data = new Dictionary<string, object>();
data.Add("bciFound", true);
data.Add("bciPos", selectedPositionsOnBoard);
data.Add("cells", aiController.GetCellList());

string jsonMove = Utility.ConvertDataToJson(data);
aiController.matchController.CmdChangeBoosterCardItemsPos(jsonMove);
}

#region BestMove
public override void OnBoosterCardItemFoundInBestMove(CellSlot cell, List<CellSlot> visitedCells, List<CellSlot> matchList, ref int point)
{
if (cell.itemType != ItemTypes.Cell)
return;

object rawDronesOnBoard;
if (!aiController.aiMoveHandler.aiGameData.TryGetValue("spawnedBCI", out rawDronesOnBoard))
return;

List<CellSlot> dronesOnBoard = (rawDronesOnBoard != null) ? Utility.CastToType<List<CellSlot>>(rawDronesOnBoard) : new List<CellSlot>();
int droneIndex = dronesOnBoard.FindIndex(drone => drone.id == cell.id);

if (droneIndex < 0)
return;

point += aiController.aiBoardManager.boardBorders.x * 2 - 1; //Estimated point of type of plus blast -> board borders x + board borders y - 1
}
#endregion

#region BoosterCardItems
public override void CheckSpawnedBoosterCardItem()
{
object rawDronesOnBoard;
aiController.aiMoveHandler.aiGameData.TryGetValue("spawnedBCI", out rawDronesOnBoard);
List<CellSlot> dronesOnBoard = (rawDronesOnBoard != null) ? Utility.CastToType<List<CellSlot>>(rawDronesOnBoard) : new List<CellSlot>();

Dictionary<string, object> data = new Dictionary<string, object>();
data.Add("bciFound", dronesOnBoard.Count > 0);

if (dronesOnBoard.Count > 0)
data.Add("cells", aiController.GetCellList());

string jsonMove = Utility.ConvertDataToJson(data);
aiController.matchController.CmdChangeBoosterCardItems(jsonMove);
}

public override IEnumerator ChangeSpawnedBoosterCardItems(int currentPlayerId, int whoMadeMove, Dictionary<string, object> move)
{
object rawDronesOnBoard;
aiController.aiMoveHandler.aiGameData.TryGetValue("spawnedBCI", out rawDronesOnBoard);
List<CellSlot> dronesOnBoard = (rawDronesOnBoard != null) ? Utility.CastToType<List<CellSlot>>(rawDronesOnBoard) : new List<CellSlot>();

if (dronesOnBoard.Count == 0)
yield break;

Dictionary<int, string> newBCIPos = Utility.ConvertToDictIntStr(Utility.GetPropertyByNameV2("bciPos", move)); //new booster card items pos before new move

if (newBCIPos != null)
{
float moveTime = .8f;

foreach (KeyValuePair<int, string> kvp in newBCIPos)
{
string[] rawPosOnBoard = kvp.Value.Split(',');
Vector2Int posOnBoard = new Vector2Int(int.Parse(rawPosOnBoard[0]), int.Parse(rawPosOnBoard[1]));
CellSlot droneCell = dronesOnBoard[kvp.Key];
droneCell.positionOnBoard = posOnBoard;
CellSlot targetCell = aiController.aiBoardManager.boardMatrix[posOnBoard.x, posOnBoard.y];
droneCell.id = targetCell.id;
dronesOnBoard[kvp.Key] = droneCell;
}

aiController.aiMoveHandler.aiGameData["spawnedBCI"] = dronesOnBoard;
yield return new WaitForSeconds(moveTime);
aiController.aiMoveHandler.SendRequestsOnMoveDone(currentPlayerId, whoMadeMove);
yield break;
}

ClearBoosterCardItems("spawnedBCI");
float tweenTime = .5f;
yield return new WaitForSeconds(tweenTime);
aiController.aiMoveHandler.SendCardItemsChanged(currentPlayerId, whoMadeMove);
}

public override void AddBoosterCardItemDataOnMove(Dictionary<string, object> data)
{
//the codes below actually irrelevant to this booster card, it was added to indicate if match list contains booster card item(touch manager->HasMatchedListContainsBoosterCell)
if (triggeredPos.Count > 0)
data.Add("keyList", new List<string>());

triggeredPos.Clear();
}
#endregion

public override int GetAverageBoosterPoint()
{
return 60;
}

public override List<Vector2Int> GetBCIPosOnBoard()
{
List<Vector2Int> posOnBoard = new List<Vector2Int>();

object rawDronesOnBoard;
aiController.aiMoveHandler.aiGameData.TryGetValue("spawnedBCI", out rawDronesOnBoard);
List<CellSlot> dronesOnBoard = (rawDronesOnBoard != null) ? Utility.CastToType<List<CellSlot>>(rawDronesOnBoard) : new List<CellSlot>();

if (dronesOnBoard.Count > 0)
{
List<CellSlot> cells = new List<CellSlot>();

for (int i = 0; i < aiController.aiBoardManager.boardBorders.y; i++)
{
for (int j = 0; j < aiController.aiBoardManager.boardBorders.x; j++)
{
CellSlot cellSlot = aiController.aiBoardManager.boardMatrix[j, i];
if (cellSlot.itemType == ItemTypes.Cell)
cells.Add(cellSlot);
}
}

for (int i = 0; i < dronesOnBoard.Count; i++)
{
//if drone is on board (id != "") AI target is cell which is below drone, if not this case drone is activated and staying position so drone is AI target
CellSlot targetCell = dronesOnBoard[i].id == "" ? dronesOnBoard[i] : cells.Find(cell => cell.id == dronesOnBoard[i].id);
posOnBoard.Add(targetCell.positionOnBoard);
}
}

return posOnBoard;
}

#endregion

void FindPossibleBoosterCardMatch(CellSlot cellItem, List<CellSlot> visitedCells, List<CellSlot> matchedFruitPlateCells, List<Vector2Int> mergedBoosterPos, List<CellSlot> dronesOnBoard, MoveType moveType, ref int point, ref int boosterPoint, ItemTypes modeItemType)
{
List<CellSlot> findMatchList = new List<CellSlot>();

List<string> droneIds = new List<string>();

for (int i = 0; i < dronesOnBoard.Count; i++)
{
droneIds.Add(dronesOnBoard[i].id);
}

if (!droneIds.Contains(cellItem.id) && triggeredPos.Contains(cellItem.positionOnBoard))
return;

triggeredPos.Add(cellItem.positionOnBoard);


CellSlot droneCell = new CellSlot() { id = "" };
droneCell = dronesOnBoard.Find(drone => drone.id == cellItem.id);

if (string.IsNullOrEmpty(droneCell.id))
return;

droneCell.positionOnBoard = cellItem.positionOnBoard;

Vector2Int posOnBoard = droneCell.positionOnBoard;
int x = posOnBoard.x;
int y = posOnBoard.y;
for (int j = 0; j < aiController.aiBoardManager.boardBorders.x; j++)
{
CellSlot cell = aiController.aiBoardManager.boardMatrix[j, y];
if (!findMatchList.Contains(cell) && cell.itemType != ItemTypes.MushroomCell)
findMatchList.Add(cell);
}

for (int j = 0; j < aiController.aiBoardManager.boardBorders.y; j++)
{
CellSlot cell = aiController.aiBoardManager.boardMatrix[x, j];
if (!findMatchList.Contains(cell) && cell.itemType != ItemTypes.MushroomCell)
findMatchList.Add(cell);
}

aiController.aiMatchFinder.FindPossibleMatch(findMatchList, visitedCells, matchedFruitPlateCells, mergedBoosterPos, moveType, ref point, ref boosterPoint, modeItemType);
}

void TriggerDrone(Vector2Int positionOnBoard, int boosterEventId, ref float totalDelay, int whoMadeMove, Dictionary<ItemTypes, float> delays, List<string> instigators, string instigatorId, bool checkType, ItemTypes exceptSpecified)
{
int x = positionOnBoard.x;
int y = positionOnBoard.y;
for (int j = 0; j < aiController.aiBoardManager.boardBorders.x; j++)
{
CellSlot cellSlot = aiController.aiBoardManager.boardMatrix[j, y];
if (cellSlot.itemType != exceptSpecified)
aiController.aiBoardManager.NullPositionOnMatrix(cellSlot, delays, ref totalDelay, whoMadeMove, boosterEventId, instigators, instigatorId, checkType, exceptSpecified);
}

for (int j = 0; j < aiController.aiBoardManager.boardBorders.y; j++)
{
CellSlot cellSlot = aiController.aiBoardManager.boardMatrix[x, j];
if (cellSlot.itemType != exceptSpecified)
aiController.aiBoardManager.NullPositionOnMatrix(cellSlot, delays, ref totalDelay, whoMadeMove, boosterEventId, instigators, instigatorId, checkType, exceptSpecified);
}

aiController.aiMoveHandler.FindCellDelay(delays, new CellSlot() { itemType = ItemTypes.DroneCell, positionOnBoard = positionOnBoard }, whoMadeMove, boosterEventId);
}

int GetDroneIndex(List<CellSlot> list, CellSlot drone)
{
int index = -1;
for (int i = 0; i < list.Count; i++)
{
if (list[i] == drone)
{
index = i;
break;
}
}

return index;
}

public float GetDroneTriggerTime(Vector2Int positionOnBoard)
{
Vector2Int boardBorders = aiController.aiBoardManager.boardBorders - Vector2Int.one;

int affectedCellsHorizontal = Math.Max(positionOnBoard.x, boardBorders.x - positionOnBoard.x);
int affectedCellsVertical = Math.Max(positionOnBoard.y, boardBorders.y - positionOnBoard.y);

int maxCellCount = Math.Max(affectedCellsHorizontal, affectedCellsVertical);
float tempDelay = maxCellCount * Utility.rocketBlastUnitTime;

return tempDelay;
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.