Advertisement
AlezM

SpatialGames

Mar 2nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class SpatialGame : MonoBehaviour {
  7.  
  8.     enum PlayerType { D, C };
  9.  
  10.     class Tile {
  11.         public PlayerType type;
  12.         public double score;
  13.         public Color color;
  14.         public Tile () {
  15.             type = PlayerType.C;
  16.             score = 0;
  17.             color = Color.blue;
  18.         }
  19.  
  20.         public Tile (PlayerType _type) {
  21.             type = _type;
  22.             score = 0;
  23.             if (type == PlayerType.C)
  24.                 color = Color.blue;
  25.             else
  26.                 color = Color.red;
  27.         }
  28.  
  29.         public void InteractWith (Tile member, double b) {
  30.             if (member.type == PlayerType.C) {
  31.                 if (type == PlayerType.C)
  32.                     score += 1;
  33.                 else
  34.                     score += b;
  35.             }
  36.         }
  37.  
  38.         public void SetType (PlayerType _type) {
  39.             if (type == PlayerType.C) {
  40.                 if (_type == PlayerType.C)
  41.                     color = Color.blue;
  42.                 else
  43.                     color = Color.yellow;
  44.             }
  45.             else {
  46.                 if (_type == PlayerType.C)
  47.                     color = Color.green;
  48.                 else
  49.                     color = Color.red;
  50.             }
  51.  
  52.             type = _type;
  53.         }
  54.     };
  55.  
  56.     struct Coord {
  57.         public int tileX;
  58.         public int tileY;
  59.  
  60.         public Coord(int x, int y) {
  61.             tileX = x;
  62.             tileY = y;
  63.         }
  64.     }
  65.  
  66.     [Range(1, 1024)]
  67.     public int mapSize;
  68.     [Range(0.01f, 100)]
  69.     public float defectorsPercentage = 5;
  70.     public double parameter = 1.8;
  71.     public bool regionsEnable = false;
  72.  
  73.     Tile[,] map;
  74.  
  75.     Texture2D mapTexture;
  76.     List<Texture2D> regionsTextures;
  77.  
  78.     void Start () {
  79.         map = new Tile[mapSize, mapSize];
  80.  
  81.         int size = mapSize;
  82.  
  83.         for (int i = 0; i < size; i++) {
  84.             for (int j = 0; j < size; j++) {
  85.                 if ( Random.Range(0, 10001) < defectorsPercentage * 100 )
  86.                     map [i, j] = new Tile (PlayerType.D);
  87.                 else
  88.                     map [i, j] = new Tile (PlayerType.C);
  89.             }
  90.         }
  91.  
  92.         MakeMapTexture ();
  93.     }
  94.  
  95.     void Update () {
  96.         if (Input.GetKeyDown (KeyCode.RightAlt)) {
  97.             MakeStep ();
  98.         }
  99.     }
  100.  
  101.     void FixedUpdate () {
  102.         if (Input.GetKey(KeyCode.Space)) {
  103.             MakeStep ();
  104.         }
  105.     }
  106.  
  107.     void MakeStep () {
  108.         CalculateAllScores ();
  109.         NextGeneration ();
  110.         ResetScore ();
  111.  
  112.         MakeMapTexture ();
  113.         if (regionsEnable)
  114.             MakeRegionTexture ();
  115.     }
  116.  
  117.     void CalculateAllScores () {
  118.         int size = mapSize;
  119.  
  120.         for (int i = 0; i < size; i++) {
  121.             for (int j = 0; j < size; j++) {
  122.  
  123.                 for (int x = -1; x <= 1; x++) { //Count score for each
  124.                     for (int y = -1; y <= 1; y++) {
  125.                         map [i, j].InteractWith (map [(i + x + size) % size, (j + y + size) % size], parameter);
  126.                     }
  127.                 }
  128.  
  129.             }
  130.         }
  131.     }
  132.  
  133.     void NextGeneration () {
  134.         Tile[,] newGrid = new Tile[mapSize, mapSize];
  135.  
  136.         for (int i = 0; i < mapSize; i++) {
  137.             for (int j = 0; j < mapSize; j++) {
  138.                
  139.                 //Search for the reachest member
  140.                 PlayerType newType = map [i, j].type;
  141.                 double maxMemberScore = 0;
  142.  
  143.                 //double cooperatorsScore = 0;
  144.                 //double defectorsScore = 0;
  145.                 for (int x = -1; x <= 1; x++) {
  146.                     for (int y = -1; y <= 1; y++) {
  147.                     /*  if (map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].type == PlayerType.C) {
  148.                             cooperatorsScore += map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].score;
  149.                         }
  150.                         else {
  151.                             defectorsScore += map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].score;
  152.                         }*/
  153.  
  154.                         double memberScore = map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].score;
  155.                         PlayerType memberType = map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].type;
  156.  
  157.                         if ( memberScore > maxMemberScore ) { // || (memberScore == maxMemberScore && memberType == PlayerType.D) ) {
  158.                             maxMemberScore = memberScore;
  159.                             newType = memberType;
  160.                         }
  161.                     }
  162.                 }
  163.                 newGrid [i, j] = new Tile (newType);
  164.  
  165.             }
  166.         }
  167.  
  168.         for (int i = 0; i < mapSize; i++) {
  169.             for (int j = 0; j < mapSize; j++) {
  170.                 map[i, j].SetType (newGrid[i, j].type);
  171.             }
  172.         }
  173.     }
  174.  
  175.     void ResetScore () {
  176.         string str = "";
  177.         for (int i = 0; i < mapSize; i++) {
  178.             for (int j = 0; j < mapSize; j++) {
  179.                 str += map [i, j].type.ToString () + map [i, j].score.ToString() + " ";
  180.                 map [i, j].score = 0;
  181.             }
  182.             str += "\n";
  183.         }
  184.     //  Debug.Log (str);
  185.     }
  186.  
  187.     List<List <Coord> > GetRegions(PlayerType tileType) {
  188.         List<List<Coord>> regions = new List<List<Coord>> ();
  189.         int[,] mapFlags = new int[mapSize, mapSize];
  190.  
  191.         for (int i = 0; i < mapSize; i++) {
  192.             for (int j = 0; j < mapSize; j++) {
  193.                 if (mapFlags[i, j] == 0 && map[i, j].type == tileType) {
  194.                     List<Coord> newRegion = GetRegionTiles(i, j);
  195.                     regions.Add(newRegion);
  196.  
  197.                     foreach (Coord tile in newRegion) {
  198.                         mapFlags[tile.tileX, tile.tileY] = 1;
  199.                     }
  200.                 }
  201.             }
  202.         }
  203.  
  204.         return regions;
  205.     }
  206.  
  207.     List<Coord> GetRegionTiles(int startX, int startY) {
  208.         int perimeter = 0;
  209.  
  210.         List<Coord> tiles = new List<Coord> ();
  211.         int[,] mapFlags = new int[mapSize, mapSize];
  212.         PlayerType tileType = map [startX, startY].type;
  213.  
  214.         Queue<Coord> queue = new Queue<Coord> ();
  215.         queue.Enqueue (new Coord (startX, startY));
  216.         mapFlags [startX, startY] = 1;
  217.  
  218.         while (queue.Count > 0) {
  219.             Coord tile = queue.Dequeue();
  220.             tiles.Add(tile);
  221.  
  222.             if (GetMembersCount(tile, tileType) < 8) {
  223.                 perimeter++;
  224.             }
  225.  
  226.             for (int x = -1; x <= 1; x++) {
  227.                 for (int y = -1; y <= 1; y++) {
  228.                     int memberX = (tile.tileX + x + mapSize) % mapSize;
  229.                     int memberY = (tile.tileY + y + mapSize) % mapSize;
  230.  
  231.                     if (mapFlags[memberX, memberY] == 0 && map[memberX, memberY].type == tileType) {
  232.                         mapFlags[memberX, memberY] = 1;
  233.                         queue.Enqueue(new Coord(memberX, memberY));
  234.                     }
  235.                 }
  236.             }
  237.         }
  238.  
  239.         return tiles;
  240.     }
  241.  
  242.     int GetMembersCount (Coord tile, PlayerType type) {
  243.         int count = 0;
  244.         for (int x = -1; x <= 1; x++) {
  245.             for (int y = -1; y <= 1; y++) {
  246.                 int memberX = (tile.tileX + x + mapSize) % mapSize;
  247.                 int memberY = (tile.tileY + y + mapSize) % mapSize;
  248.                 if (map [x, y].type == type) {
  249.                     count++;
  250.                 }
  251.             }
  252.         }
  253.         return count;
  254.     }
  255.  
  256.     void MakeMapTexture () {
  257.         Texture2D  t = new Texture2D (mapSize, mapSize);
  258.         t.filterMode = FilterMode.Point;
  259.         for (int i = 0; i < mapSize; i++) {
  260.             for (int j = 0; j < mapSize; j++) {
  261.                 t.SetPixel (i, mapSize - j - 1, map[i, j].color);
  262.             }
  263.         }
  264.         t.Apply ();
  265.         mapTexture = t;
  266.     }
  267.  
  268.     void MakeRegionTexture () {
  269.         List<List <Coord> > cooperatorsRegions = GetRegions (PlayerType.C);
  270.         regionsTextures = new List<Texture2D> ();
  271.  
  272.         foreach (List<Coord> cooperatorsRegion in cooperatorsRegions) {
  273.             Texture2D t = new Texture2D (mapSize, mapSize);
  274.             t.filterMode = FilterMode.Point;
  275.             for (int i = 0; i < mapSize; i++) {
  276.                 for (int j = 0; j < mapSize; j++) {
  277.                     t.SetPixel (i, mapSize - j - 1, Color.black);
  278.                 }
  279.             }
  280.             foreach (Coord tile in cooperatorsRegion) {
  281.                 t.SetPixel (tile.tileX, mapSize - tile.tileY - 1, Color.white);
  282.             }
  283.             t.Apply ();
  284.             regionsTextures.Add (t);
  285.         }
  286.     }
  287.  
  288.     void OnGUI () {
  289.         if (mapTexture != null)
  290.             GUI.DrawTexture (new Rect (0, 0, Mathf.Min (Screen.width/2, Screen.height), Mathf.Min (Screen.width/2, Screen.height)), mapTexture);
  291.  
  292.         if (regionsTextures != null) {
  293.             for (int i = 0; i < regionsTextures.Count; i++) {
  294.                 int textureSize = (int)(Mathf.Sqrt (regionsTextures.Count) + 1);
  295.                 GUI.DrawTexture (
  296.                     new Rect (
  297.                         Screen.width/2 + (i % textureSize) * Screen.width / (2*textureSize) + 1,
  298.                         (i / textureSize) * Screen.width / (2*textureSize) + 1,
  299.                         Screen.width / (2*textureSize) - 1, Screen.width / (2*textureSize) - 1
  300.                     ),
  301.                     regionsTextures [i]);
  302.             }
  303.         }
  304.  
  305.         if (GUI.Button (new Rect (0, 0, Screen.width/5, Screen.height/15), "Restart")) {
  306.             Start ();
  307.         }
  308.     }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement