Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class SpatialGame : MonoBehaviour {
- enum PlayerType { D, C };
- class Tile {
- public PlayerType type;
- public double score;
- public Color color;
- public Tile () {
- type = PlayerType.C;
- score = 0;
- color = Color.blue;
- }
- public Tile (PlayerType _type) {
- type = _type;
- score = 0;
- if (type == PlayerType.C)
- color = Color.blue;
- else
- color = Color.red;
- }
- public void InteractWith (Tile member, double b) {
- if (member.type == PlayerType.C) {
- if (type == PlayerType.C)
- score += 1;
- else
- score += b;
- }
- }
- public void SetType (PlayerType _type) {
- if (type == PlayerType.C) {
- if (_type == PlayerType.C)
- color = Color.blue;
- else
- color = Color.yellow;
- }
- else {
- if (_type == PlayerType.C)
- color = Color.green;
- else
- color = Color.red;
- }
- type = _type;
- }
- };
- struct Coord {
- public int tileX;
- public int tileY;
- public Coord(int x, int y) {
- tileX = x;
- tileY = y;
- }
- }
- [Range(1, 1024)]
- public int mapSize;
- [Range(0.01f, 100)]
- public float defectorsPercentage = 5;
- public double parameter = 1.8;
- public bool regionsEnable = false;
- Tile[,] map;
- Texture2D mapTexture;
- List<Texture2D> regionsTextures;
- void Start () {
- map = new Tile[mapSize, mapSize];
- int size = mapSize;
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- if ( Random.Range(0, 10001) < defectorsPercentage * 100 )
- map [i, j] = new Tile (PlayerType.D);
- else
- map [i, j] = new Tile (PlayerType.C);
- }
- }
- MakeMapTexture ();
- }
- void Update () {
- if (Input.GetKeyDown (KeyCode.RightAlt)) {
- MakeStep ();
- }
- }
- void FixedUpdate () {
- if (Input.GetKey(KeyCode.Space)) {
- MakeStep ();
- }
- }
- void MakeStep () {
- CalculateAllScores ();
- NextGeneration ();
- ResetScore ();
- MakeMapTexture ();
- if (regionsEnable)
- MakeRegionTexture ();
- }
- void CalculateAllScores () {
- int size = mapSize;
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- for (int x = -1; x <= 1; x++) { //Count score for each
- for (int y = -1; y <= 1; y++) {
- map [i, j].InteractWith (map [(i + x + size) % size, (j + y + size) % size], parameter);
- }
- }
- }
- }
- }
- void NextGeneration () {
- Tile[,] newGrid = new Tile[mapSize, mapSize];
- for (int i = 0; i < mapSize; i++) {
- for (int j = 0; j < mapSize; j++) {
- //Search for the reachest member
- PlayerType newType = map [i, j].type;
- double maxMemberScore = 0;
- //double cooperatorsScore = 0;
- //double defectorsScore = 0;
- for (int x = -1; x <= 1; x++) {
- for (int y = -1; y <= 1; y++) {
- /* if (map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].type == PlayerType.C) {
- cooperatorsScore += map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].score;
- }
- else {
- defectorsScore += map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].score;
- }*/
- double memberScore = map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].score;
- PlayerType memberType = map [(i + x + mapSize) % mapSize, (j + y + mapSize) % mapSize].type;
- if ( memberScore > maxMemberScore ) { // || (memberScore == maxMemberScore && memberType == PlayerType.D) ) {
- maxMemberScore = memberScore;
- newType = memberType;
- }
- }
- }
- newGrid [i, j] = new Tile (newType);
- }
- }
- for (int i = 0; i < mapSize; i++) {
- for (int j = 0; j < mapSize; j++) {
- map[i, j].SetType (newGrid[i, j].type);
- }
- }
- }
- void ResetScore () {
- string str = "";
- for (int i = 0; i < mapSize; i++) {
- for (int j = 0; j < mapSize; j++) {
- str += map [i, j].type.ToString () + map [i, j].score.ToString() + " ";
- map [i, j].score = 0;
- }
- str += "\n";
- }
- // Debug.Log (str);
- }
- List<List <Coord> > GetRegions(PlayerType tileType) {
- List<List<Coord>> regions = new List<List<Coord>> ();
- int[,] mapFlags = new int[mapSize, mapSize];
- for (int i = 0; i < mapSize; i++) {
- for (int j = 0; j < mapSize; j++) {
- if (mapFlags[i, j] == 0 && map[i, j].type == tileType) {
- List<Coord> newRegion = GetRegionTiles(i, j);
- regions.Add(newRegion);
- foreach (Coord tile in newRegion) {
- mapFlags[tile.tileX, tile.tileY] = 1;
- }
- }
- }
- }
- return regions;
- }
- List<Coord> GetRegionTiles(int startX, int startY) {
- int perimeter = 0;
- List<Coord> tiles = new List<Coord> ();
- int[,] mapFlags = new int[mapSize, mapSize];
- PlayerType tileType = map [startX, startY].type;
- Queue<Coord> queue = new Queue<Coord> ();
- queue.Enqueue (new Coord (startX, startY));
- mapFlags [startX, startY] = 1;
- while (queue.Count > 0) {
- Coord tile = queue.Dequeue();
- tiles.Add(tile);
- if (GetMembersCount(tile, tileType) < 8) {
- perimeter++;
- }
- for (int x = -1; x <= 1; x++) {
- for (int y = -1; y <= 1; y++) {
- int memberX = (tile.tileX + x + mapSize) % mapSize;
- int memberY = (tile.tileY + y + mapSize) % mapSize;
- if (mapFlags[memberX, memberY] == 0 && map[memberX, memberY].type == tileType) {
- mapFlags[memberX, memberY] = 1;
- queue.Enqueue(new Coord(memberX, memberY));
- }
- }
- }
- }
- return tiles;
- }
- int GetMembersCount (Coord tile, PlayerType type) {
- int count = 0;
- for (int x = -1; x <= 1; x++) {
- for (int y = -1; y <= 1; y++) {
- int memberX = (tile.tileX + x + mapSize) % mapSize;
- int memberY = (tile.tileY + y + mapSize) % mapSize;
- if (map [x, y].type == type) {
- count++;
- }
- }
- }
- return count;
- }
- void MakeMapTexture () {
- Texture2D t = new Texture2D (mapSize, mapSize);
- t.filterMode = FilterMode.Point;
- for (int i = 0; i < mapSize; i++) {
- for (int j = 0; j < mapSize; j++) {
- t.SetPixel (i, mapSize - j - 1, map[i, j].color);
- }
- }
- t.Apply ();
- mapTexture = t;
- }
- void MakeRegionTexture () {
- List<List <Coord> > cooperatorsRegions = GetRegions (PlayerType.C);
- regionsTextures = new List<Texture2D> ();
- foreach (List<Coord> cooperatorsRegion in cooperatorsRegions) {
- Texture2D t = new Texture2D (mapSize, mapSize);
- t.filterMode = FilterMode.Point;
- for (int i = 0; i < mapSize; i++) {
- for (int j = 0; j < mapSize; j++) {
- t.SetPixel (i, mapSize - j - 1, Color.black);
- }
- }
- foreach (Coord tile in cooperatorsRegion) {
- t.SetPixel (tile.tileX, mapSize - tile.tileY - 1, Color.white);
- }
- t.Apply ();
- regionsTextures.Add (t);
- }
- }
- void OnGUI () {
- if (mapTexture != null)
- GUI.DrawTexture (new Rect (0, 0, Mathf.Min (Screen.width/2, Screen.height), Mathf.Min (Screen.width/2, Screen.height)), mapTexture);
- if (regionsTextures != null) {
- for (int i = 0; i < regionsTextures.Count; i++) {
- int textureSize = (int)(Mathf.Sqrt (regionsTextures.Count) + 1);
- GUI.DrawTexture (
- new Rect (
- Screen.width/2 + (i % textureSize) * Screen.width / (2*textureSize) + 1,
- (i / textureSize) * Screen.width / (2*textureSize) + 1,
- Screen.width / (2*textureSize) - 1, Screen.width / (2*textureSize) - 1
- ),
- regionsTextures [i]);
- }
- }
- if (GUI.Button (new Rect (0, 0, Screen.width/5, Screen.height/15), "Restart")) {
- Start ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement