all repos — uStrat @ 9c9f67ae9fbec0cf1faf3b364eb028507642612f

simple turn-based strategy game inspired by uCity, Super Robot Wars, C&C, Fire Emblem

AI.js (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
AI = {};
AI.threats = {};
AI.morale = {};
AI.losses = 0;
AI.mode = "frugal";
AI.modes = [ "catchup", "frugal", "aggressive", "exploratory", "endgame" ];

AI.rand = function(i)
{
  return Math.floor(Math.random()*i);
}

AI.getMorale = function()
{
  var x, y, z;
  x = teams.cpu.energy - teams.p1.energy;
  y = teams.cpu.units.length - teams.p1.units.length;
  z = teams.cpu.structs.length - teams.p1.structs.length;
  w = new Object();
  w.energy = x;
  w.units = y;
  w.structs = z;
  AI.morale = w;
}

AI.deliberatePriority = function()
{
  if (AI.morale.energy < teams.p1.energy/4)
  {
    AI.mode = "catchup";
  }
  else if (AI.morale.structs < 8)
  {
    AI.mode = "frugal";
  }
  if (AI.mode != "catchup")
  {
    if (AI.morale.units >= 0 )
    {
      AI.mode = "exploratory";
    }
    else { AI.mode = "aggressive" };
    if (AI.morale.units > 20 && AI.morale.structs > 20)
    {
      AI.mode = "endgame";
    }
  }
}

AI.findNewStruct = function()
{
  var origin = {};
  origin.x = teams.cpu.structs[0].x;
  origin.y = teams.cpu.structs[0].y;
  var i, j, w;
  w = AI.rand(4);
  switch(w)
  {
    case 0:
      for (i = 0; i < 64; i++)
      {
        for (j = 0; j < 64; j++)
        {
          if (dist(origin, map.data[i][j]) < teams.cpu.energy/15 && isEmptyObject(map.data[i][j].structure))
          {
            console.log(i + ", " + j); return map.data[i][j];
          }
        }
      }
      break;
    case 1:
      for (i = 63; i >= 0; i--)
      {
        for (j = 63; j >= 0; j--)
        {
          if (dist(origin, map.data[i][j]) < teams.cpu.energy/15 && isEmptyObject(map.data[i][j].structure))
          {
            console.log(i + ", " + j); return map.data[i][j];
          }
        }
      }
      break;
    case 2:
      for (i = 63; i >= 0; i--)
      {
        for (j = 0; j < 64; j++)
        {
          if (dist(origin, map.data[i][j]) < teams.cpu.energy/15 && isEmptyObject(map.data[i][j].structure))
          {
            console.log(i + ", " + j); return map.data[i][j];
          }
        }
      }
      break;
    case 3:
      for (i = 0; i < 64; i++)
      {
        for (j = 63; j >= 0; j--)
        {
          if (dist(origin, map.data[i][j]) < teams.cpu.energy/15 && isEmptyObject(map.data[i][j].structure))
          {
            console.log(i + ", " + j); return map.data[i][j];
          }
        }
      }
      break;
  }
}

AI.buildStruct = function()
{
  var cell = this.findNewStruct();
  mapCursor.x = cell.x;
  mapCursor.y = cell.y;
  switch (cell.type)
  {
    case "forest":
    case "city":
      map.degradeCell(map.data[cell.x][cell.y]);
    case "plain":
      if (teams.cpu.energy >= 40)
      {
        if (AI.rand(4) < 2)
        {
          if (AI.rand(4) < 2)
          {
            buildFactory();
          }
          else buildArmory();
        }
        else buildSolar();
      }
      else if (teams.cpu.energy >= 20) { buildSolar(); }
      break;
    case "water":
      if (teams.cpu.energy >= 30)
      {
        buildTidal();
      }
      break;
    case "mountain":
      if (teams.cpu.energy >= 30)
      {
        buildMine();
      }
      else if (teams.cpu.energy >= 20)
      {
        buildSolar();
      }
      break;
  }
}

AI.main = function()
{
//  this.deliberatePriority();
  this.buildStruct();
  endPhase();
}