import(Module_Defines)
import(Module_PopScript)
import(Module_Commands)
import(Module_Game)
import(Module_Objects)
import(Module_Map)
import(Module_MapWho)
import(Module_Math)
import(Module_Helpers)
import(Module_System)
import(Module_Features)
import(Module_String)
import(Module_Building)
import(Module_DataTypes)
import(Module_Draw)
import(Module_Globals)
import(Module_ImGui)
import(Module_Level)
import(Module_Network)
import(Module_Person)
import(Module_Players)
import(Module_Shapes)
import(Module_Spells)
import(Module_Table)

include("UtilPThings.lua")
include("UtilRefs.lua")

local STurn = GetTurn()

_c.MaxManaValue = 2500000
_c.ShamenDeadManaPer256Gained = 16

goodT = {}
evilT = {}
lightT = {}
darkT = {}

--set_players_allied(0,1)
--set_players_allied(1,0)

wilds = {}
spell_delay = {0,0,0,0,0,0,0}
spell_ms_delay = {720,720,720,720,720,720,720}
spell_ms_used = {5,5,5,5,5,5,5}
spell_ms_radius = 3
spell_ms_max_use = 5
spell_ms_charge_time = 720
l_charges = {0,0,0,0,0,0,0}
h_charges = {0,0,0,0,0,0,0}
g_charges = {0,0,0,0,0,0,0}
b_charges = {0,0,0,0,0,0,0}
blast_charges = {0,0,0,0,0,0,0}
s_charges = {0,0,0,0,0,0,0}
l_charge_time = {540, 540, 540, 540, 540, 540, 540}
h_charge_time = {720, 720, 720, 720, 720, 720, 720}
g_charge_time = {360, 360, 360, 360, 360, 360, 360}
b_charge_time = {2160, 2160, 2160, 2160, 2160, 2160, 2160}
blast_charge_time = {240, 240, 240, 240, 240, 240, 240}
s_charge_time = {480, 480, 480, 480, 480, 480, 480}
t_charge_time = {6000 + G_RANDOM(5000), 6000 + G_RANDOM(5000), 6000 + G_RANDOM(5000), 6000 + G_RANDOM(5000), 6000 + G_RANDOM(5000), 6000 + G_RANDOM(5000), 6000 + G_RANDOM(5000)}
num_temp = {1,2,3,4,5,6,7}
spell_tribe_numbers = {}
current_number = 1
for i = 1,7 do
  selected = G_RANDOM(tablelength(num_temp))+1
  table.insert(spell_tribe_numbers, num_temp[selected])
  table.remove(num_temp, selected)
end
prayer = nil
cast_arma = 0
index = 1
availableNums = {4,5,6,7,2,3,1}
position_in_table = {7,5,6,1,2,3,4}
AITribes = {0,0,0,0,0,0,0}
numthings = 16
srate = 5
uprate=9
landbridge_skill_level = {--1 for always saves themself. Higher the number, the less likely they are to save themselves when in water
G_RANDOM(5)+1,
G_RANDOM(5)+1,
G_RANDOM(5)+1,
G_RANDOM(5)+1,
G_RANDOM(5)+1,
G_RANDOM(5)+1,
G_RANDOM(5)+1
}
bridge_attack = {
  GetTurn() + (5400 + G_RANDOM(3600)),
  GetTurn() + (5400 + G_RANDOM(3600)),
  GetTurn() + (5400 + G_RANDOM(3600)),
  GetTurn() + (5400 + G_RANDOM(3600)),
  GetTurn() + (5400 + G_RANDOM(3600)),
  GetTurn() + (5400 + G_RANDOM(3600)),
  GetTurn() + (5400 + G_RANDOM(3600))
}
lite_attack = {
  GetTurn() + (512 + G_RANDOM(512)),
  GetTurn() + (512 + G_RANDOM(512)),
  GetTurn() + (512 + G_RANDOM(512)),
  GetTurn() + (512 + G_RANDOM(512)),
  GetTurn() + (512 + G_RANDOM(512)),
  GetTurn() + (512 + G_RANDOM(512)),
  GetTurn() + (512 + G_RANDOM(512))
}
heavy_attack = {
  GetTurn() + (2048 + G_RANDOM(1024)),
  GetTurn() + (2048 + G_RANDOM(1024)),
  GetTurn() + (2048 + G_RANDOM(1024)),
  GetTurn() + (2048 + G_RANDOM(1024)),
  GetTurn() + (2048 + G_RANDOM(1024)),
  GetTurn() + (2048 + G_RANDOM(1024)),
  GetTurn() + (2048 + G_RANDOM(1024))
}
shaman_attack = {
  GetTurn() + (1024 + G_RANDOM(512)),
  GetTurn() + (1024 + G_RANDOM(512)),
  GetTurn() + (1024 + G_RANDOM(512)),
  GetTurn() + (1024 + G_RANDOM(512)),
  GetTurn() + (1024 + G_RANDOM(512)),
  GetTurn() + (1024 + G_RANDOM(512)),
  GetTurn() + (1024 + G_RANDOM(512))
}
armageddon_timer = {
  GetTurn() + (21600 + G_RANDOM(43200)),
  GetTurn() + (21600 + G_RANDOM(43200)),
  GetTurn() + (21600 + G_RANDOM(43200)),
  GetTurn() + (21600 + G_RANDOM(43200)),
  GetTurn() + (21600 + G_RANDOM(43200)),
  GetTurn() + (21600 + G_RANDOM(43200)),
  GetTurn() + (21600 + G_RANDOM(43200))
}

blueBaseLandbridges = {}
redBaseLandbridges = {}
yellowBaseLandbridges = {}
greenBaseLandbridges = {}
cyanBaseLandbridges = {}
magentaBaseLandbridges = {}
blackBaseLandbridges = {}
orangeBaseLandbridges = {}

redLandbridges = {}
yellowLandbridges = {}
greenLandbridges = {}
cyanLandbridges = {}
magentaLandbridges = {}
blackLandbridges = {}
orangeLandbridges = {}

blueBaseTowers = {MAP_XZ_2_WORLD_XYZ(38, 158),
                MAP_XZ_2_WORLD_XYZ(46, 158),
                MAP_XZ_2_WORLD_XYZ(76, 130),
                MAP_XZ_2_WORLD_XYZ(76, 122),
                MAP_XZ_2_WORLD_XYZ(46, 94),
                MAP_XZ_2_WORLD_XYZ(38, 94),
                MAP_XZ_2_WORLD_XYZ(8, 122),
                MAP_XZ_2_WORLD_XYZ(8, 130)
}
redBaseTowers = {MAP_XZ_2_WORLD_XYZ(46, 174),
                MAP_XZ_2_WORLD_XYZ(38, 174),
                MAP_XZ_2_WORLD_XYZ(8, 202),
                MAP_XZ_2_WORLD_XYZ(8, 210),
                MAP_XZ_2_WORLD_XYZ(38, 238),
                MAP_XZ_2_WORLD_XYZ(46, 238),
                MAP_XZ_2_WORLD_XYZ(76, 210),
                MAP_XZ_2_WORLD_XYZ(76, 202)
}
yellowBaseTowers = {MAP_XZ_2_WORLD_XYZ(176, 122),
                MAP_XZ_2_WORLD_XYZ(176, 130),
                MAP_XZ_2_WORLD_XYZ(206, 158),
                MAP_XZ_2_WORLD_XYZ(214, 158),
                MAP_XZ_2_WORLD_XYZ(244, 130),
                MAP_XZ_2_WORLD_XYZ(244, 122),
                MAP_XZ_2_WORLD_XYZ(214, 94),
                MAP_XZ_2_WORLD_XYZ(206, 94)
}
greenBaseTowers = {MAP_XZ_2_WORLD_XYZ(206, 78),
                MAP_XZ_2_WORLD_XYZ(214, 78),
                MAP_XZ_2_WORLD_XYZ(244, 50),
                MAP_XZ_2_WORLD_XYZ(244, 42),
                MAP_XZ_2_WORLD_XYZ(214, 14),
                MAP_XZ_2_WORLD_XYZ(206, 14),
                MAP_XZ_2_WORLD_XYZ(176, 42),
                MAP_XZ_2_WORLD_XYZ(176, 50)
}
cyanBaseTowers = {MAP_XZ_2_WORLD_XYZ(8, 42),
                MAP_XZ_2_WORLD_XYZ(8, 50),
                MAP_XZ_2_WORLD_XYZ(38, 78),
                MAP_XZ_2_WORLD_XYZ(46, 78),
                MAP_XZ_2_WORLD_XYZ(76, 50),
                MAP_XZ_2_WORLD_XYZ(76, 42),
                MAP_XZ_2_WORLD_XYZ(46, 14),
                MAP_XZ_2_WORLD_XYZ(38, 14)
}
magentaBaseTowers = {MAP_XZ_2_WORLD_XYZ(92, 42),
                MAP_XZ_2_WORLD_XYZ(92, 50),
                MAP_XZ_2_WORLD_XYZ(122, 78),
                MAP_XZ_2_WORLD_XYZ(130, 78),
                MAP_XZ_2_WORLD_XYZ(160, 50),
                MAP_XZ_2_WORLD_XYZ(160, 42),
                MAP_XZ_2_WORLD_XYZ(130, 14),
                MAP_XZ_2_WORLD_XYZ(122, 14)
}
blackBaseTowers = {MAP_XZ_2_WORLD_XYZ(176, 202),
                MAP_XZ_2_WORLD_XYZ(176, 210),
                MAP_XZ_2_WORLD_XYZ(206, 238),
                MAP_XZ_2_WORLD_XYZ(214, 238),
                MAP_XZ_2_WORLD_XYZ(244, 210),
                MAP_XZ_2_WORLD_XYZ(244, 202),
                MAP_XZ_2_WORLD_XYZ(214, 174),
                MAP_XZ_2_WORLD_XYZ(206, 174)
}
orangeBaseTowers = {MAP_XZ_2_WORLD_XYZ(160, 210),
                MAP_XZ_2_WORLD_XYZ(160, 202),
                MAP_XZ_2_WORLD_XYZ(130, 174),
                MAP_XZ_2_WORLD_XYZ(122, 174),
                MAP_XZ_2_WORLD_XYZ(92, 202),
                MAP_XZ_2_WORLD_XYZ(92, 210),
                MAP_XZ_2_WORLD_XYZ(122, 238),
                MAP_XZ_2_WORLD_XYZ(130, 238)
}

redTowers = {}
yellowTowers = {}
greenTowers = {}
cyanTowers = {}
magentaTowers = {}
blackTowers = {}
orangeTowers = {}

--conv_center_pos = {
--MAP_XZ_2_WORLD_XYZ(218, 18),
--MAP_XZ_2_WORLD_XYZ(158, 222),
--MAP_XZ_2_WORLD_XYZ(212, 116), 
--MAP_XZ_2_WORLD_XYZ(140, 72),
--MAP_XZ_2_WORLD_XYZ(56, 178),
--MAP_XZ_2_WORLD_XYZ(74, 236)
--}
conv_center_pos = {0,0,0,0,0,0,0}

ReincPos = {
MAP_XZ_2_WORLD_XYZ(42, 126), -- blue base
MAP_XZ_2_WORLD_XYZ(42, 206), -- red base
MAP_XZ_2_WORLD_XYZ(210, 126), -- yellow base
MAP_XZ_2_WORLD_XYZ(210, 46), -- green base
MAP_XZ_2_WORLD_XYZ(42, 46), -- cyan base
MAP_XZ_2_WORLD_XYZ(126, 46), -- magenta base
MAP_XZ_2_WORLD_XYZ(210, 206), -- black base
MAP_XZ_2_WORLD_XYZ(126, 206), -- orange base
}

spells_list = {M_SPELL_BLOODLUST,
               M_SPELL_TELEPORT,
               M_SPELL_ARMAGEDDON,
               M_SPELL_BLAST,
               M_SPELL_CONVERT_WILD,
               M_SPELL_GHOST_ARMY,
               M_SPELL_INSECT_PLAGUE,
               M_SPELL_INVISIBILITY,
               M_SPELL_SHIELD,
               M_SPELL_LAND_BRIDGE,
               M_SPELL_LIGHTNING_BOLT,
               M_SPELL_HYPNOTISM,
               M_SPELL_WHIRLWIND,
               M_SPELL_SWAMP,
               M_SPELL_FLATTEN,
               M_SPELL_EARTHQUAKE,
               M_SPELL_EROSION,
               M_SPELL_FIRESTORM,
               M_SPELL_ANGEL_OF_DEATH,
               M_SPELL_VOLCANO
}
builds_list = {M_BUILDING_TEPEE,
               M_BUILDING_DRUM_TOWER,
               M_BUILDING_WARRIOR_TRAIN,
               M_BUILDING_TEMPLE,
               M_BUILDING_SUPER_TRAIN,
               M_BUILDING_SPY_TRAIN,
               M_BUILDING_BOAT_HUT_1,
               M_BUILDING_AIRSHIP_HUT_1
}

--botSpells = {M_SPELL_CONVERT_WILD,
--             M_SPELL_BLAST,
--             M_SPELL_LAND_BRIDGE,
--             M_SPELL_LIGHTNING_BOLT,
--             M_SPELL_INSECT_PLAGUE,
--             M_SPELL_GHOST_ARMY,
--             M_SPELL_SWAMP,
--             M_SPELL_HYPNOTISM,
--             M_SPELL_WHIRLWIND,
--             M_SPELL_EARTHQUAKE,
--             M_SPELL_FIRESTORM
--}
--botBldgs = {M_BUILDING_TEPEE,
--            M_BUILDING_DRUM_TOWER,
--            M_BUILDING_WARRIOR_TRAIN,
--            M_BUILDING_TEMPLE,
--            M_BUILDING_SUPER_TRAIN,
--            M_BUILDING_SPY_TRAIN
--}

--for i = 0,7 do
--  for u,v in ipairs(botSpells) do
--    PThing.SpellSet(i, v, TRUE, FALSE)
--  end

--  for y,v in ipairs(botBldgs) do
--    PThing.BldgSet(i, v, TRUE)
--  end
--end

for i = 1,7 do
  if (getPlayer(availableNums[i]).PlayerType == COMPUTER_PLAYER) then
    AITribes[i] = 1
  end
end

for i = 1,7 do
  if(AITribes[i] == 1) then
  for u,v in ipairs(spells_list) do
    if (player_has_spell_unlocked(TRIBE_BLUE, v)) then
      PThing.SpellSet(availableNums[i], v, TRUE, FALSE)
      log("Tribe " .. availableNums[i] .. " gets spell " .. v)
    end
  end
  for u,v in ipairs(builds_list) do
    if (player_has_build_unlocked(TRIBE_BLUE, v)) then
      PThing.BldgSet(availableNums[i], v, TRUE)
      log("Tribe " .. availableNums[i] .. " gets building " .. v)
    end
  end

  computer_init_player(_gsi.Players[availableNums[i]])

  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_WARRIOR_TRAINS, 1)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_WARRIOR_PEOPLE, 0)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_RELIGIOUS_TRAINS, 1)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_RELIGIOUS_PEOPLE, 0)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_SUPER_WARRIOR_TRAINS, 1)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_SUPER_WARRIOR_PEOPLE, 0)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_SPY_TRAINS, 1)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_SPY_PEOPLE, G_RANDOM(4)+2)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_BRAVE, 5)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_WARRIOR, 80)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_RELIGIOUS, 69)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_SUPER_WARRIOR, 47)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_SPY, 5)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN, 100)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_USE_PREACHER_FOR_DEFENCE, 1)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_DEFENSE_RAD_INCR, 6)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_MAX_DEFENSIVE_ACTIONS, 3)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_ATTACK_PERCENTAGE, 125)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_MAX_ATTACKS, 150)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_BASE_UNDER_ATTACK_RETREAT, 0)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_RANDOM_BUILD_SIDE, 1)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_GROUP_OPTION, 0)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PEOPLE_PER_BOAT, 7)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PEOPLE_PER_BALLOON, 8)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_ENEMY_SPY_MAX_STAND, 255)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_SPY_CHECK_FREQUENCY, 128)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_SPY_DISCOVER_CHANCE, 30)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_MAX_TRAIN_AT_ONCE, 5)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_SHAMEN_BLAST, 8)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_HOUSE_PERCENTAGE, 50+G_RANDOM(28))
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_BALLOON_HUTS, 1)
  WRITE_CP_ATTRIB(availableNums[i], ATTR_PREF_BALLOON_DRIVERS, 5)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_BUILD_VEHICLE)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_FETCH_FAR_VEHICLE)

  WRITE_CP_ATTRIB(availableNums[i], ATTR_EXPANSION, 40+G_RANDOM(30))
  WRITE_CP_ATTRIB(availableNums[i], ATTR_MAX_BUILDINGS_ON_GO, 10+G_RANDOM(6))
  WRITE_CP_ATTRIB(availableNums[i], ATTR_RETREAT_VALUE, 15+G_RANDOM(30))


  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_FETCH_WOOD)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_CONSTRUCT_BUILDING)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_BUILD_OUTER_DEFENCES)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_DEFEND)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_DEFEND_BASE)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_PREACH)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_BRING_NEW_PEOPLE_BACK)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_SUPER_DEFEND)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_TRAIN_PEOPLE)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_AUTO_ATTACK)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_HOUSE_A_PERSON)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_POPULATE_DRUM_TOWER)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_FETCH_LOST_PEOPLE)
  STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_FETCH_LOST_VEHICLE)
  if (player_has_spell_unlocked(availableNums[i], M_SPELL_CONVERT_WILD) == true) then
    STATE_SET(availableNums[i], TRUE, CP_AT_TYPE_MED_MAN_GET_WILD_PEEPS)
  else
    STATE_SET(availableNums[i], FALSE, CP_AT_TYPE_MED_MAN_GET_WILD_PEEPS)
  end

  SET_BUCKET_USAGE(availableNums[i], TRUE)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_BLAST, 8)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_CONVERT_WILD, 8)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_GHOST_ARMY, 12)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_LAND_BRIDGE, 32)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_INSECT_PLAGUE, 36)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_LIGHTNING_BOLT, 40)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_HYPNOTISM, 50)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_WHIRLWIND, 80)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_SWAMP, 100)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_EARTHQUAKE, 175)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_FIRESTORM, 275)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_SHIELD, 28)
  SET_BUCKET_COUNT_FOR_SPELL(availableNums[i], M_SPELL_INVISIBILITY, 30)

  SET_DEFENCE_RADIUS(availableNums[i], 7)
  if (player_has_spell_unlocked(availableNums[i], M_SPELL_INSECT_PLAGUE)) then
    SET_SPELL_ENTRY(availableNums[i], 0, M_SPELL_INSECT_PLAGUE, 25000, 64, 3, 0)
    SET_SPELL_ENTRY(availableNums[i], 1, M_SPELL_INSECT_PLAGUE, 25000, 64, 3, 1)
    if (player_has_spell_unlocked(availableNums[i], M_SPELL_LIGHTNING_BOLT)) then
      SET_SPELL_ENTRY(availableNums[i], 2, M_SPELL_LIGHTNING_BOLT, 40000, 64, 2, 0)
      SET_SPELL_ENTRY(availableNums[i], 3, M_SPELL_LIGHTNING_BOLT, 40000, 64, 2, 1)
      if (player_has_spell_unlocked(availableNums[i], M_SPELL_HYPNOTISM)) then
        SET_SPELL_ENTRY(availableNums[i], 4, M_SPELL_HYPNOTISM, 50000, 64, 4, 0)
        SET_SPELL_ENTRY(availableNums[i], 5, M_SPELL_HYPNOTISM, 50000, 64, 4, 1)
      end
    elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_HYPNOTISM)) then
      SET_SPELL_ENTRY(availableNums[i], 2, M_SPELL_HYPNOTISM, 50000, 64, 4, 0)
      SET_SPELL_ENTRY(availableNums[i], 3, M_SPELL_HYPNOTISM, 50000, 64, 4, 1)
    end
  elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_LIGHTNING_BOLT)) then
    SET_SPELL_ENTRY(availableNums[i], 0, M_SPELL_LIGHTNING_BOLT, 40000, 64, 2, 0)
    SET_SPELL_ENTRY(availableNums[i], 1, M_SPELL_LIGHTNING_BOLT, 40000, 64, 2, 1)
    if (player_has_spell_unlocked(availableNums[i], M_SPELL_HYPNOTISM)) then
      SET_SPELL_ENTRY(availableNums[i], 2, M_SPELL_HYPNOTISM, 50000, 64, 4, 0)
      SET_SPELL_ENTRY(availableNums[i], 3, M_SPELL_HYPNOTISM, 50000, 64, 4, 1)
    end
  elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_HYPNOTISM)) then
    SET_SPELL_ENTRY(availableNums[i], 0, M_SPELL_HYPNOTISM, 50000, 64, 4, 0)
    SET_SPELL_ENTRY(availableNums[i], 1, M_SPELL_HYPNOTISM, 50000, 64, 4, 1)
  end
  end
end

for i,k in ipairs(ReincPos) do
  local me = world_coord3d_to_map_ptr(k)
  me.MapWhoList:processList(function(t)
    if (t.Type == T_PERSON) then
      if (t.Model == M_PERSON_MEDICINE_MAN) then
        if (i == 1) then
          table.insert(goodT, t.Owner)
          if (t.Owner > 0 and AITribes[position_in_table[t.Owner]] == 1) then
            if (t.Owner == 1) then
              BaseNum = TRIBE_RED
              BaseConv = 7
              redTowers = blueBaseTowers
              redLandbridges = blueBaseLandbridges
            elseif (t.Owner == 2) then
              BaseNum = TRIBE_YELLOW
              BaseConv = 5
              yellowTowers = blueBaseTowers
              yellowLandbridges = blueBaseLandbridges
            elseif (t.Owner == 3) then
              BaseNum = TRIBE_GREEN
              BaseConv = 6
              greenTowers = blueBaseTowers
              greenLandbridges = blueBaseLandbridges
            elseif (t.Owner == 4) then
              BaseNum = TRIBE_CYAN
              BaseConv = 1
              cyanTowers = blueBaseTowers
              cyanLandbridges = blueBaseLandbridges
            elseif (t.Owner == 5) then
              BaseNum = TRIBE_PINK
              BaseConv = 2
              magentaTowers = blueBaseTowers
              magentaLandbridges = blueBaseLandbridges
            elseif (t.Owner == 6) then
              BaseNum = TRIBE_BLACK
              BaseConv = 3
              blackTowers = blueBaseTowers
              blackLandbridges = blueBaseLandbridges
            elseif (t.Owner == 7) then
              BaseNum = TRIBE_ORANGE
              BaseConv = 4
              orangeTowers = blueBaseTowers
              orangeLandbridges = blueBaseLandbridges
            end
            SHAMAN_DEFEND(BaseNum, 42, 126, TRUE)
            SET_DRUM_TOWER_POS(BaseNum, 42, 126)
            conv_center_pos[BaseConv] = MAP_XZ_2_WORLD_XYZ(42, 126)
          end
        elseif (i == 2) then
          table.insert(goodT, t.Owner)
          if (t.Owner > 0 and AITribes[position_in_table[t.Owner]] == 1) then
            if (t.Owner == 1) then
              BaseNum = TRIBE_RED
              BaseConv = 7
              redTowers = redBaseTowers
              redLandbridges = redBaseLandbridges
            elseif (t.Owner == 2) then
              BaseNum = TRIBE_YELLOW
              BaseConv = 5
              yellowTowers = redBaseTowers
              yellowLandbridges = redBaseLandbridges
            elseif (t.Owner == 3) then
              BaseNum = TRIBE_GREEN
              BaseConv = 6
              greenTowers = redBaseTowers
              greenLandbridges = redBaseLandbridges
            elseif (t.Owner == 4) then
              BaseNum = TRIBE_CYAN
              BaseConv = 1
              cyanTowers = redBaseTowers
              cyanLandbridges = redBaseLandbridges
            elseif (t.Owner == 5) then
              BaseNum = TRIBE_PINK
              BaseConv = 2
              magentaTowers = redBaseTowers
              magentaLandbridges = redBaseLandbridges
            elseif (t.Owner == 6) then
              BaseNum = TRIBE_BLACK
              BaseConv = 3
              blackTowers = redBaseTowers
              blackLandbridges = redBaseLandbridges
            elseif (t.Owner == 7) then
              BaseNum = TRIBE_ORANGE
              BaseConv = 4
              orangeTowers = redBaseTowers
              orangeLandbridges = redBaseLandbridges
            end
            SHAMAN_DEFEND(BaseNum, 42, 206, TRUE)
            SET_DRUM_TOWER_POS(BaseNum, 42, 206)
            conv_center_pos[BaseConv] = MAP_XZ_2_WORLD_XYZ(42, 206)
          end
        elseif (i == 3) then
          table.insert(evilT, t.Owner)
          if (t.Owner > 0 and AITribes[position_in_table[t.Owner]] == 1) then
            if (t.Owner == 1) then
              BaseNum = TRIBE_RED
              BaseConv = 7
              redTowers = yellowBaseTowers
              redLandbridges = yellowBaseLandbridges
            elseif (t.Owner == 2) then
              BaseNum = TRIBE_YELLOW
              BaseConv = 5
              yellowTowers = yellowBaseTowers
              yellowLandbridges = yellowBaseLandbridges
            elseif (t.Owner == 3) then
              BaseNum = TRIBE_GREEN
              BaseConv = 6
              greenTowers = yellowBaseTowers
              greenLandbridges = yellowBaseLandbridges
            elseif (t.Owner == 4) then
              BaseNum = TRIBE_CYAN
              BaseConv = 1
              cyanTowers = yellowBaseTowers
              cyanLandbridges = yellowBaseLandbridges
            elseif (t.Owner == 5) then
              BaseNum = TRIBE_PINK
              BaseConv = 2
              magentaTowers = yellowBaseTowers
              magentaLandbridges = yellowBaseLandbridges
            elseif (t.Owner == 6) then
              BaseNum = TRIBE_BLACK
              BaseConv = 3
              blackTowers = yellowBaseTowers
              blackLandbridges = yellowBaseLandbridges
            elseif (t.Owner == 7) then
              BaseNum = TRIBE_ORANGE
              BaseConv = 4
              orangeTowers = yellowBaseTowers
              orangeLandbridges = yellowBaseLandbridges
            end
            SHAMAN_DEFEND(BaseNum, 210, 126, TRUE)
            SET_DRUM_TOWER_POS(BaseNum, 210, 126)
            conv_center_pos[BaseConv] = MAP_XZ_2_WORLD_XYZ(210, 126)
          end
        elseif (i == 4) then
          table.insert(evilT, t.Owner)
          if (t.Owner > 0 and AITribes[position_in_table[t.Owner]] == 1) then
            if (t.Owner == 1) then
              BaseNum = TRIBE_RED
              BaseConv = 7
              redTowers = greenBaseTowers
              redLandbridges = greenBaseLandbridges
            elseif (t.Owner == 2) then
              BaseNum = TRIBE_YELLOW
              BaseConv = 5
              yellowTowers = greenBaseTowers
              yellowLandbridges = greenBaseLandbridges
            elseif (t.Owner == 3) then
              BaseNum = TRIBE_GREEN
              BaseConv = 6
              greenTowers = greenBaseTowers
              greenLandbridges = greenBaseLandbridges
            elseif (t.Owner == 4) then
              BaseNum = TRIBE_CYAN
              BaseConv = 1
              cyanTowers = greenBaseTowers
              cyanLandbridges = greenBaseLandbridges
            elseif (t.Owner == 5) then
              BaseNum = TRIBE_PINK
              BaseConv = 2
              magentaTowers = greenBaseTowers
              magentaLandbridges = greenBaseLandbridges
            elseif (t.Owner == 6) then
              BaseNum = TRIBE_BLACK
              BaseConv = 3
              blackTowers = greenBaseTowers
              blackLandbridges = greenBaseLandbridges
            elseif (t.Owner == 7) then
              BaseNum = TRIBE_ORANGE
              BaseConv = 4
              orangeTowers = greenBaseTowers
              orangeLandbridges = greenBaseLandbridges
            end
            SHAMAN_DEFEND(BaseNum, 210, 46, TRUE)
            SET_DRUM_TOWER_POS(BaseNum, 210, 46)
            conv_center_pos[BaseConv] = MAP_XZ_2_WORLD_XYZ(210, 46)
          end
        elseif (i == 5) then
          table.insert(lightT, t.Owner)
          if (t.Owner > 0 and AITribes[position_in_table[t.Owner]] == 1) then
            if (t.Owner == 1) then
              BaseNum = TRIBE_RED
              BaseConv = 7
              redTowers = cyanBaseTowers
              redLandbridges = cyanBaseLandbridges
            elseif (t.Owner == 2) then
              BaseNum = TRIBE_YELLOW
              BaseConv = 5
              yellowTowers = cyanBaseTowers
              yellowLandbridges = cyanBaseLandbridges
            elseif (t.Owner == 3) then
              BaseNum = TRIBE_GREEN
              BaseConv = 6
              greenTowers = cyanBaseTowers
              greenLandbridges = cyanBaseLandbridges
            elseif (t.Owner == 4) then
              BaseNum = TRIBE_CYAN
              BaseConv = 1
              cyanTowers = cyanBaseTowers
              cyanLandbridges = cyanBaseLandbridges
            elseif (t.Owner == 5) then
              BaseNum = TRIBE_PINK
              BaseConv = 2
              magentaTowers = cyanBaseTowers
              magentaLandbridges = cyanBaseLandbridges
            elseif (t.Owner == 6) then
              BaseNum = TRIBE_BLACK
              BaseConv = 3
              blackTowers = cyanBaseTowers
              blackLandbridges = cyanBaseLandbridges
            elseif (t.Owner == 7) then
              BaseNum = TRIBE_ORANGE
              BaseConv = 4
              orangeTowers = cyanBaseTowers
              orangeLandbridges = cyanBaseLandbridges
            end
            SHAMAN_DEFEND(BaseNum, 42, 46, TRUE)
            SET_DRUM_TOWER_POS(BaseNum, 42, 46)
            conv_center_pos[BaseConv] = MAP_XZ_2_WORLD_XYZ(42, 46)
          end
        elseif (i == 6) then
          table.insert(lightT, t.Owner)
          if (t.Owner > 0 and AITribes[position_in_table[t.Owner]] == 1) then
            if (t.Owner == 1) then
              BaseNum = TRIBE_RED
              BaseConv = 7
              redTowers = magentaBaseTowers
              redLandbridges = magentaBaseLandbridges
            elseif (t.Owner == 2) then
              BaseNum = TRIBE_YELLOW
              BaseConv = 5
              yellowTowers = magentaBaseTowers
              yellowLandbridges = magentaBaseLandbridges
            elseif (t.Owner == 3) then
              BaseNum = TRIBE_GREEN
              BaseConv = 6
              greenTowers = magentaBaseTowers
              greenLandbridges = magentaBaseLandbridges
            elseif (t.Owner == 4) then
              BaseNum = TRIBE_CYAN
              BaseConv = 1
              cyanTowers = magentaBaseTowers
              cyanLandbridges = magentaBaseLandbridges
            elseif (t.Owner == 5) then
              BaseNum = TRIBE_PINK
              BaseConv = 2
              magentaTowers = magentaBaseTowers
              magentaLandbridges = magentaBaseLandbridges
            elseif (t.Owner == 6) then
              BaseNum = TRIBE_BLACK
              BaseConv = 3
              blackTowers = magentaBaseTowers
              blackLandbridges = magentaBaseLandbridges
            elseif (t.Owner == 7) then
              BaseNum = TRIBE_ORANGE
              BaseConv = 4
              orangeTowers = magentaBaseTowers
              orangeLandbridges = magentaBaseLandbridges
            end
            SHAMAN_DEFEND(BaseNum, 126, 46, TRUE)
            SET_DRUM_TOWER_POS(BaseNum, 126, 46)
            conv_center_pos[BaseConv] = MAP_XZ_2_WORLD_XYZ(126, 46)
          end
        elseif (i == 7) then
          table.insert(darkT, t.Owner)
          if (t.Owner > 0 and AITribes[position_in_table[t.Owner]] == 1) then
            if (t.Owner == 1) then
              BaseNum = TRIBE_RED
              BaseConv = 7
              redTowers = blackBaseTowers
              redLandbridges = blackBaseLandbridges
            elseif (t.Owner == 2) then
              BaseNum = TRIBE_YELLOW
              BaseConv = 5
              yellowTowers = blackBaseTowers
              yellowLandbridges = blackBaseLandbridges
            elseif (t.Owner == 3) then
              BaseNum = TRIBE_GREEN
              BaseConv = 6
              greenTowers = blackBaseTowers
              greenLandbridges = blackBaseLandbridges
            elseif (t.Owner == 4) then
              BaseNum = TRIBE_CYAN
              BaseConv = 1
              cyanTowers = blackBaseTowers
              cyanLandbridges = blackBaseLandbridges
            elseif (t.Owner == 5) then
              BaseNum = TRIBE_PINK
              BaseConv = 2
              magentaTowers = blackBaseTowers
              magentaLandbridges = blackBaseLandbridges
            elseif (t.Owner == 6) then
              BaseNum = TRIBE_BLACK
              BaseConv = 3
              blackTowers = blackBaseTowers
              blackLandbridges = blackBaseLandbridges
            elseif (t.Owner == 7) then
              BaseNum = TRIBE_ORANGE
              BaseConv = 4
              orangeTowers = blackBaseTowers
              orangeLandbridges = blackBaseLandbridges
            end
            SHAMAN_DEFEND(BaseNum, 210, 206, TRUE)
            SET_DRUM_TOWER_POS(BaseNum, 210, 206)
            conv_center_pos[BaseConv] = MAP_XZ_2_WORLD_XYZ(210, 206)
          end
        elseif (i == 8) then
          table.insert(darkT, t.Owner)
          if (t.Owner > 0 and AITribes[position_in_table[t.Owner]] == 1) then
            if (t.Owner == 1) then
              BaseNum = TRIBE_RED
              BaseConv = 7
              redTowers = orangeBaseTowers
              redLandbridges = orangeBaseLandbridges
            elseif (t.Owner == 2) then
              BaseNum = TRIBE_YELLOW
              BaseConv = 5
              yellowTowers = orangeBaseTowers
              yellowLandbridges = orangeBaseLandbridges
            elseif (t.Owner == 3) then
              BaseNum = TRIBE_GREEN
              BaseConv = 6
              greenTowers = orangeBaseTowers
              greenLandbridges = orangeBaseLandbridges
            elseif (t.Owner == 4) then
              BaseNum = TRIBE_CYAN
              BaseConv = 1
              cyanTowers = orangeBaseTowers
              cyanLandbridges = orangeBaseLandbridges
            elseif (t.Owner == 5) then
              BaseNum = TRIBE_PINK
              BaseConv = 2
              magentaTowers = orangeBaseTowers
              magentaLandbridges = orangeBaseLandbridges
            elseif (t.Owner == 6) then
              BaseNum = TRIBE_BLACK
              BaseConv = 3
              blackTowers = orangeBaseTowers
              blackLandbridges = orangeBaseLandbridges
            elseif (t.Owner == 7) then
              BaseNum = TRIBE_ORANGE
              BaseConv = 4
              orangeTowers = orangeBaseTowers
              orangeLandbridges = orangeBaseLandbridges
            end
            SHAMAN_DEFEND(BaseNum, 126, 206, TRUE)
            SET_DRUM_TOWER_POS(BaseNum, 126, 206)
            conv_center_pos[BaseConv] = MAP_XZ_2_WORLD_XYZ(126, 206)
          end
        end
      end
    end
    return true
  end)
end


--DEFEND POS'S

--SHAMAN_DEFEND(TRIBE_CYAN, 204, 10, TRUE)
--SHAMAN_DEFEND(TRIBE_PINK, 158, 222, TRUE)
--SHAMAN_DEFEND(TRIBE_BLACK, 212, 116, TRUE)
--SHAMAN_DEFEND(TRIBE_ORANGE, 140, 72, TRUE)
--SHAMAN_DEFEND(TRIBE_BLUE, 30, 86, TRUE)
--SHAMAN_DEFEND(TRIBE_RED, 70, 46, TRUE)
--SHAMAN_DEFEND(TRIBE_YELLOW, 56, 178, TRUE)
--SHAMAN_DEFEND(TRIBE_GREEN, 74, 236, TRUE)
--SET_DRUM_TOWER_POS(TRIBE_CYAN, 204, 10)
--SET_DRUM_TOWER_POS(TRIBE_PINK, 158, 222)
--SET_DRUM_TOWER_POS(TRIBE_BLACK, 212, 116)
--SET_DRUM_TOWER_POS(TRIBE_ORANGE, 140, 72)
--SET_DRUM_TOWER_POS(TRIBE_BLUE, 30, 86)
--SET_DRUM_TOWER_POS(TRIBE_RED, 40, 46)
--SET_DRUM_TOWER_POS(TRIBE_YELLOW, 56, 178)
--SET_DRUM_TOWER_POS(TRIBE_GREEN, 74, 236)

function OnTurn()
  if (GetTurn() > STurn + 1 and not isPaused()) then
    for shotNum = 0,7 do
      if (cast_arma == 0 and GET_SPELLS_CAST(shotNum,M_SPELL_ARMAGEDDON) == 1) then
        cast_arma = 1
      end
    end
    if every2Pow(2) then
      for i,t in ipairs(heavy_attack) do
        if (GetTurn() > t and AITribes[i] == 1) then
          if (FREE_ENTRIES(availableNums[i]) > 2) then
            if (player_has_spell_unlocked(availableNums[i], M_SPELL_HYPNOTISM)) then
              priAttack = 7
              if (player_has_spell_unlocked(availableNums[i], M_SPELL_LIGHTNING_BOLT)) then
                secAttack = 3
              elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_INSECT_PLAGUE)) then
                secAttack = 5
              else
                secAttack = 2
              end
            elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_LIGHTNING_BOLT)) then
              priAttack = 3
              secAttack = 2
            elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_INSECT_PLAGUE)) then
              priAttack = 5
              secAttack = 2
            else
              priAttack = 2
              secAttack = 2
            end
            teleport_ready = 0
            if (player_has_spell_unlocked(availableNums[i], M_SPELL_TELEPORT) and (G_RANDOM(3) == 1) and t_charge_time[i] < 2) then    
              teleport_ready = 1
            end
            if (player_has_spell_unlocked(availableNums[i], M_SPELL_INVISIBILITY) and teleport_ready == 0) then
              supAttack = 6
            else
              if (player_has_spell_unlocked(availableNums[i], M_SPELL_HYPNOTISM)) then
                supAttack = 7
              elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_LIGHTNING_BOLT)) then
                supAttack = 3
              elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_INSECT_PLAGUE)) then
                supAttack = 5
              else
                supAttack = 2
              end
            end
            if (count_troops(availableNums[i]) > 19) then
              if (IS_SHAMAN_AVAILABLE_FOR_ATTACK(availableNums[i]) > 0 and MANA(availableNums[i]) > 200000) then
                local e = decide_an_enemy_to_attack(availableNums[i])
                if (NAV_CHECK(availableNums[i],e,ATTACK_BUILDING,1,0) > 0 and e ~= availableNums[i]) then
                  local troop_num = count_troops(availableNums[i])
                  if (player_has_spell_unlocked(availableNums[i], M_SPELL_ARMAGEDDON) and GetTurn() > armageddon_timer[i]) then
                    troop_num = (troop_num - math.ceil(troop_num / 3)) - G_RANDOM(math.ceil(troop_num / 2))
                  else
                    troop_num = (troop_num - math.ceil(troop_num / 4)) - G_RANDOM(math.ceil(troop_num / 3))
                  end
                  WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN, 1)
                  if (teleport_ready == 1) then
                    ProcessGlobalTypeList(T_BUILDING, function(t)
                      if (t.Owner == e) then
                        createThing(T_SPELL, M_SPELL_TELEPORT, availableNums[i], t.Pos.D3, false, false)
                        t_charge_time[i] = 7200
                        return false
                      end
                      return true
                    end)
                  end
                  ATTACK(availableNums[i],e,troop_num,ATTACK_BUILDING,M_BUILDING_TEPEE,999,supAttack,priAttack,secAttack,ATTACK_NORMAL,0,-1,-1,0)
                  WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN, 0)
                  if (player_has_spell_unlocked(availableNums[i], M_SPELL_ARMAGEDDON) and GetTurn() > armageddon_timer[i]) then
                    heavy_attack[i] = GetTurn() + (2048 + G_RANDOM(1536))
                    lite_attack[i] = GetTurn() + (1024 + G_RANDOM(512))
                  else
                    heavy_attack[i] = GetTurn() + (1536 + G_RANDOM(1536))
                    lite_attack[i] = GetTurn() + (512 + G_RANDOM(512))
                  end
                end
              end
            end
          end
        end
      end
    end

    if every2Pow(2) then
      for i,t in ipairs(lite_attack) do
        if (GetTurn() > t and AITribes[i] == 1) then
          if (FREE_ENTRIES(availableNums[i]) > 2) then
            if (IS_SHAMAN_AVAILABLE_FOR_ATTACK(availableNums[i]) > 0 and player_has_spell_unlocked(availableNums[i], M_SPELL_INVISIBILITY)) then
              supAttack = 6
            else
              supAttack = 0
            end
            if (count_troops(availableNums[i]) > 7) then
              local e = decide_an_enemy_to_attack(availableNums[i])
              local be = decide_an_enemy_to_attack_air(availableNums[i])
              local balloons = count_available_balloons(availableNums[i])
              if (balloons > 2 and be ~= availableNums[i]) then 
                local troop_num = count_troops(availableNums[i])
                if (troop_num > (balloons - 1) * 2) then
                  troop_num = (balloons - 1) * 2
                end
                if (troop_num > 10) then
                  troop_num = 10
                end
                WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN,0)
                ATTACK(availableNums[i],be,troop_num,ATTACK_BUILDING,4,999,supAttack,0,0,ATTACK_BY_BALLOON,0,-1,-1,0)
                WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN,1)
                lite_attack[i] = GetTurn() + (768 + G_RANDOM(512))
              elseif (NAV_CHECK(availableNums[i],e,ATTACK_BUILDING,4,0) > 0 and e ~= availableNums[i]) then
                local troop_num = count_troops(availableNums[i])
                if (troop_num > 10) then
                  troop_num = 10
                end
                WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN,0)
                ATTACK(availableNums[i],e,troop_num,ATTACK_BUILDING,4,999,supAttack,0,0,ATTACK_NORMAL,0,-1,-1,0)
                WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN,1)
                if (player_has_spell_unlocked(availableNums[i], M_SPELL_ARMAGEDDON) and GetTurn() > armageddon_timer[i]) then
                  lite_attack[i] = GetTurn() + (1024 + G_RANDOM(512))
                else
                  lite_attack[i] = GetTurn() + (768 + G_RANDOM(512))
                end
              end
            end
          end
        end
      end
    end
    
    if every2Pow(2) then
      for i,t in ipairs(shaman_attack) do
        if (GetTurn() > t and AITribes[i] == 1) then
          if (FREE_ENTRIES(availableNums[i]) > 2) then
            if (IS_SHAMAN_AVAILABLE_FOR_ATTACK(availableNums[i]) > 0 and MANA(availableNums[i]) > 200000) then
              if (player_has_spell_unlocked(availableNums[i], M_SPELL_HYPNOTISM)) then
                supAttack = 7
                if (player_has_spell_unlocked(availableNums[i], M_SPELL_LIGHTNING_BOLT)) then
                  priAttack = 3
                elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_INSECT_PLAGUE)) then
                  priAttack = 5
                else
                  priAttack = 7
                end
                if (player_has_spell_unlocked(availableNums[i], M_SPELL_INSECT_PLAGUE)) then
                  secAttack = 5
                else
                  secAttack = 2
                end
              elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_LIGHTNING_BOLT)) then
                supAttack = 3
                priAttack = 3
                secAttack = 2
              elseif (player_has_spell_unlocked(availableNums[i], M_SPELL_INSECT_PLAGUE)) then
                supAttack = 5
                priAttack = 5
                secAttack = 2
              else
                supAttack = 2
                priAttack = 2
                secAttack = 2
              end
              teleport_ready = 0
              if (player_has_spell_unlocked(availableNums[i], M_SPELL_TELEPORT) and (G_RANDOM(3) == 1) and t_charge_time[i] < 2) then    
                teleport_ready = 1
              end
              local e = decide_an_enemy_to_attack(availableNums[i])
              local be = decide_an_enemy_to_attack_air(availableNums[i])
              local balloons = count_available_balloons(availableNums[i])
              if (balloons > 1 and teleport_ready == 0 and be ~= availableNums[i]) then 
                WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN,1)
                ATTACK(availableNums[i],be,0,ATTACK_BUILDING,1+G_RANDOM(3),999,supAttack,priAttack,secAttack,ATTACK_BY_BALLOON,1,-1,-1,0)
                WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN, 0)
                shaman_attack[i] = GetTurn() + (1024 + G_RANDOM(1024))
              elseif (NAV_CHECK(availableNums[i],e,ATTACK_BUILDING,1,0) > 0 and e ~= availableNums[i]) then
                WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN, 1)
                  if (teleport_ready == 1) then
                    ProcessGlobalTypeList(T_BUILDING, function(t)
                      if (t.Owner == e) then
                        createThing(T_SPELL, M_SPELL_TELEPORT, availableNums[i], t.Pos.D3, false, false)
                        t_charge_time[i] = 7200
                        return false
                      end
                      return true
                    end)
                  end
                ATTACK(availableNums[i],e,0,ATTACK_BUILDING,1+G_RANDOM(3),999,supAttack,priAttack,secAttack,ATTACK_NORMAL,0,-1,-1,0)
                WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN, 0)
                shaman_attack[i] = GetTurn() + (1024 + G_RANDOM(1024))
              end
            end
          end
        end
      end
    end

    if every2Pow(4) then
      for i,t in ipairs(armageddon_timer) do
        if (GetTurn() > t and AITribes[i] == 1 and cast_arma == 0) then
          if (FREE_ENTRIES(availableNums[i]) > 2) then
            if (IS_SHAMAN_AVAILABLE_FOR_ATTACK(availableNums[i]) > 0) then
              local shaman = getShaman(availableNums[i])
              if (GetPopLeader() == shaman.Owner and player_has_spell_unlocked(shaman.Owner, M_SPELL_ARMAGEDDON)) then
                createThing(T_SPELL, M_SPELL_ARMAGEDDON, shaman.Owner, shaman.Pos.D3, false, false)
              end
            end
          end
        end
      end
    end

    if every2Pow(2) then
      for i,t in ipairs(bridge_attack) do
        if (GetTurn() > t and (player_has_spell_unlocked(availableNums[i], M_SPELL_LAND_BRIDGE) == true) and AITribes[i] == 1) then
          if (FREE_ENTRIES(availableNums[i]) > 2) then
            if (IS_SHAMAN_AVAILABLE_FOR_ATTACK(availableNums[i]) > 0 and MANA(availableNums[i]) > 50000 and count_troops(availableNums[i]) > 15) then
              local e = decide_an_enemy_to_attack(availableNums[i])
              WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN, 1)
              bridge_attack[i] = GetTurn() + (32 + G_RANDOM(32))
              if (availableNums[i] == 1) then
                if (tablelength(redLandbridges) > 0 and GetPlayerPeople(1) > 0) then
                  local lb_idx = tablelength(redLandbridges)
                  local lb_rnd = G_RANDOM(lb_idx)+1
                  if (redLandbridges[lb_rnd] ~= nil) then
                    lb_marker = redLandbridges[lb_rnd]
                    if (GET_HEIGHT_AT_POS(lb_marker) == 0) then
                      marker_goto = lb_marker + 1
                      marker_cast = lb_marker + 2
                      ATTACK(availableNums[i],e,0,ATTACK_BUILDING,-1,0,12,0,0,ATTACK_NORMAL,0,marker_goto,marker_cast,0)
                      bridge_attack[i] = GetTurn() + (1024 + G_RANDOM(1024))
                    else
                      table.remove(redLandbridges, lb_rnd)
                    end
                  end
                end
              end
              if (availableNums[i] == 2) then
                if (tablelength(yellowLandbridges) > 0 and GetPlayerPeople(2) > 0) then
                  local lb_idx = tablelength(yellowLandbridges)
                  local lb_rnd = G_RANDOM(lb_idx)+1
                  if (yellowLandbridges[lb_rnd] ~= nil) then
                    lb_marker = yellowLandbridges[lb_rnd]
                    if (GET_HEIGHT_AT_POS(lb_marker) == 0) then
                      marker_goto = lb_marker + 1
                      marker_cast = lb_marker + 2
                      ATTACK(availableNums[i],e,0,ATTACK_BUILDING,-1,0,12,0,0,ATTACK_NORMAL,0,marker_goto,marker_cast,0)
                      bridge_attack[i] = GetTurn() + (1024 + G_RANDOM(1024))
                    else
                      table.remove(yellowLandbridges, lb_rnd)
                    end
                  end
                end
              end
              if (availableNums[i] == 3) then
                if (tablelength(greenLandbridges) > 0 and GetPlayerPeople(3) > 0) then
                  local lb_idx = tablelength(greenLandbridges)
                  local lb_rnd = G_RANDOM(lb_idx)+1
                  if (greenLandbridges[lb_rnd] ~= nil) then
                    lb_marker = greenLandbridges[lb_rnd]
                    if (GET_HEIGHT_AT_POS(lb_marker) == 0) then
                      marker_goto = lb_marker + 1
                      marker_cast = lb_marker + 2
                      ATTACK(availableNums[i],e,0,ATTACK_BUILDING,-1,0,12,0,0,ATTACK_NORMAL,0,marker_goto,marker_cast,0)
                      bridge_attack[i] = GetTurn() + (1024 + G_RANDOM(1024))
                    else
                      table.remove(greenLandbridges, lb_rnd)
                    end
                  end
                end
              end
              if (availableNums[i] == 4) then
                if (tablelength(cyanLandbridges) > 0 and GetPlayerPeople(4) > 0) then
                  local lb_idx = tablelength(cyanLandbridges)
                  local lb_rnd = G_RANDOM(lb_idx)+1
                  if (cyanLandbridges[lb_rnd] ~= nil) then
                    lb_marker = cyanLandbridges[lb_rnd]
                    if (GET_HEIGHT_AT_POS(lb_marker) == 0) then
                      marker_goto = lb_marker + 1
                      marker_cast = lb_marker + 2
                      ATTACK(availableNums[i],e,0,ATTACK_BUILDING,-1,0,12,0,0,ATTACK_NORMAL,0,marker_goto,marker_cast,0)
                      bridge_attack[i] = GetTurn() + (1024 + G_RANDOM(1024))
                    else
                      table.remove(cyanLandbridges, lb_rnd)
                    end
                  end
                end
              end
              if (availableNums[i] == 5) then
                if (tablelength(magentaLandbridges) > 0 and GetPlayerPeople(5) > 0) then
                  local lb_idx = tablelength(magentaLandbridges)
                  local lb_rnd = G_RANDOM(lb_idx)+1
                  if (magentaLandbridges[lb_rnd] ~= nil) then
                    lb_marker = magentaLandbridges[lb_rnd]
                    if (GET_HEIGHT_AT_POS(lb_marker) == 0) then
                      marker_goto = lb_marker + 1
                      marker_cast = lb_marker + 2
                      ATTACK(availableNums[i],e,0,ATTACK_BUILDING,-1,0,12,0,0,ATTACK_NORMAL,0,marker_goto,marker_cast,0)
                      bridge_attack[i] = GetTurn() + (1024 + G_RANDOM(1024))
                    else
                      table.remove(magentaLandbridges, lb_rnd)
                    end
                  end
                end
              end
              if (availableNums[i] == 6) then
                if (tablelength(blackLandbridges) > 0 and GetPlayerPeople(6) > 0) then
                  local lb_idx = tablelength(blackLandbridges)
                  local lb_rnd = G_RANDOM(lb_idx)+1
                  if (blackLandbridges[lb_rnd] ~= nil) then
                    lb_marker = blackLandbridges[lb_rnd]
                    if (GET_HEIGHT_AT_POS(lb_marker) == 0) then
                      marker_goto = lb_marker + 1
                      marker_cast = lb_marker + 2
                      ATTACK(availableNums[i],e,0,ATTACK_BUILDING,-1,0,12,0,0,ATTACK_NORMAL,0,marker_goto,marker_cast,0)
                      bridge_attack[i] = GetTurn() + (1024 + G_RANDOM(1024))
                    else
                      table.remove(blackLandbridges, lb_rnd)
                    end
                  end
                end
              end
              if (availableNums[i] == 7) then
                if (tablelength(orangeLandbridges) > 0 and GetPlayerPeople(7) > 0) then
                  local lb_idx = tablelength(orangeLandbridges)
                  local lb_rnd = G_RANDOM(lb_idx)+1
                  if (orangeLandbridges[lb_rnd] ~= nil) then
                    lb_marker = orangeLandbridges[lb_rnd]
                    if (GET_HEIGHT_AT_POS(lb_marker) == 0) then
                      marker_goto = lb_marker + 1
                      marker_cast = lb_marker + 2
                      ATTACK(availableNums[i],e,0,ATTACK_BUILDING,-1,0,12,0,0,ATTACK_NORMAL,0,marker_goto,marker_cast,0)
                      bridge_attack[i] = GetTurn() + (1024 + G_RANDOM(1024))
                    else
                      table.remove(orangeLandbridges, lb_rnd)
                    end
                  end
                end
              end
              WRITE_CP_ATTRIB(availableNums[i], ATTR_AWAY_MEDICINE_MAN, 0)
            end
          end
        end
      end
    end

    if (every2Pow(9)) then
      for i = 1,7 do
        if (AITribes[position_in_table[i]] == 1) then
        GIVE_MANA_TO_PLAYER(i,5000+G_RANDOM(3000))
        if (PLAYERS_PEOPLE_OF_TYPE(i, M_PERSON_BRAVE) > 10 and _gsi.Players[i].NumPeople > 14) then
          WRITE_CP_ATTRIB(i, ATTR_PREF_WARRIOR_TRAINS, 1)
          WRITE_CP_ATTRIB(i, ATTR_PREF_WARRIOR_PEOPLE, 22+G_RANDOM(18))
          WRITE_CP_ATTRIB(i, ATTR_PREF_RELIGIOUS_TRAINS, 1)
          WRITE_CP_ATTRIB(i, ATTR_PREF_RELIGIOUS_PEOPLE, 18+G_RANDOM(16))
          WRITE_CP_ATTRIB(i, ATTR_PREF_SUPER_WARRIOR_TRAINS, 1)
          WRITE_CP_ATTRIB(i, ATTR_PREF_SUPER_WARRIOR_PEOPLE, 20+G_RANDOM(17))
          STATE_SET(i, FALSE, CP_AT_TYPE_MED_MAN_GET_WILD_PEEPS)
        else
          WRITE_CP_ATTRIB(i, ATTR_PREF_WARRIOR_TRAINS, 0)
          WRITE_CP_ATTRIB(i, ATTR_PREF_WARRIOR_PEOPLE, 5)
          WRITE_CP_ATTRIB(i, ATTR_PREF_RELIGIOUS_TRAINS, 0)
          WRITE_CP_ATTRIB(i, ATTR_PREF_RELIGIOUS_PEOPLE, 5)
          WRITE_CP_ATTRIB(i, ATTR_PREF_SUPER_WARRIOR_TRAINS, 0)
          WRITE_CP_ATTRIB(i, ATTR_PREF_SUPER_WARRIOR_PEOPLE, 5)
          if (player_has_spell_unlocked(i, M_SPELL_CONVERT_WILD) == true) then
            STATE_SET(i, TRUE, CP_AT_TYPE_MED_MAN_GET_WILD_PEEPS)
          else
            STATE_SET(i, FALSE, CP_AT_TYPE_MED_MAN_GET_WILD_PEEPS)
          end
        end

        if (READ_CP_ATTRIB(i,ATTR_HOUSE_PERCENTAGE) < 180 and GetPlayerPeople(i) > READ_CP_ATTRIB(i,ATTR_HOUSE_PERCENTAGE)) then
          WRITE_CP_ATTRIB(i,ATTR_HOUSE_PERCENTAGE,READ_CP_ATTRIB(i,ATTR_HOUSE_PERCENTAGE)+2)
        end
        end
      end
    end

    if (every2Pow(uprate)) then
      ProcessGlobalTypeList(T_BUILDING, function(t)
        if ((t.Model < 4 and t.Owner > 0) and AITribes[position_in_table[t.Owner]] == 1) then
          if (t.u.Bldg.SproggingCount < 2000) then
            t.u.Bldg.SproggingCount = t.u.Bldg.SproggingCount + 1500
          end
        end

        if ((t.Model < 3 and t.Owner > 0) and AITribes[position_in_table[t.Owner]] == 1) then
          if (t.u.Bldg.UpgradeCount < 1825) then
            t.u.Bldg.UpgradeCount = t.u.Bldg.UpgradeCount + 350
          end
        end

        return true
      end)
    end

    if (every2Pow(9)) then
      if (AITribes[7] == 1 and tablelength(redTowers) > 0 and GetPlayerPeople(1) > 0 and player_has_build_unlocked(TRIBE_RED, M_BUILDING_DRUM_TOWER) and (_gsi.Players[TRIBE_RED].NumPeopleOfType[M_PERSON_SUPER_WARRIOR] > 0 or _gsi.Players[TRIBE_RED].NumPeopleOfType[M_PERSON_RELIGIOUS] > 0)) then
        local t_idx = tablelength(redTowers)
        local t_rnd = G_RANDOM(t_idx)+1
        if (redTowers[t_rnd] ~= nil and FREE_ENTRIES(TRIBE_RED) > 1) then
          local map_idx = MapPosXZ.new()
          map_idx.Pos = world_coord3d_to_map_idx(redTowers[t_rnd])
          BUILD_DRUM_TOWER(TRIBE_RED, map_idx.XZ.X, map_idx.XZ.Z)
          table.remove(redTowers, t_rnd)
        end
      end

      if (AITribes[5] == 1 and tablelength(yellowTowers) > 0 and GetPlayerPeople(2) > 0 and player_has_build_unlocked(TRIBE_YELLOW, M_BUILDING_DRUM_TOWER) and (_gsi.Players[TRIBE_YELLOW].NumPeopleOfType[M_PERSON_SUPER_WARRIOR] > 0 or _gsi.Players[TRIBE_YELLOW].NumPeopleOfType[M_PERSON_RELIGIOUS] > 0)) then
        local t_idx = tablelength(yellowTowers)
        local t_rnd = G_RANDOM(t_idx)+1
        if (yellowTowers[t_rnd] ~= nil and FREE_ENTRIES(TRIBE_YELLOW) > 1) then
          local map_idx = MapPosXZ.new()
          map_idx.Pos = world_coord3d_to_map_idx(yellowTowers[t_rnd])
          BUILD_DRUM_TOWER(TRIBE_YELLOW, map_idx.XZ.X, map_idx.XZ.Z)
          table.remove(yellowTowers, t_rnd)
        end
      end

      if (AITribes[6] == 1 and tablelength(greenTowers) > 0 and GetPlayerPeople(3) > 0 and player_has_build_unlocked(TRIBE_GREEN, M_BUILDING_DRUM_TOWER) and (_gsi.Players[TRIBE_GREEN].NumPeopleOfType[M_PERSON_SUPER_WARRIOR] > 0 or _gsi.Players[TRIBE_GREEN].NumPeopleOfType[M_PERSON_RELIGIOUS] > 0)) then
        local t_idx = tablelength(greenTowers)
        local t_rnd = G_RANDOM(t_idx)+1
        if (greenTowers[t_rnd] ~= nil and FREE_ENTRIES(TRIBE_GREEN) > 1) then
          local map_idx = MapPosXZ.new()
          map_idx.Pos = world_coord3d_to_map_idx(greenTowers[t_rnd])
          BUILD_DRUM_TOWER(TRIBE_GREEN, map_idx.XZ.X, map_idx.XZ.Z)
          table.remove(greenTowers, t_rnd)
        end
      end

      if (AITribes[1] == 1 and tablelength(cyanTowers) > 0 and GetPlayerPeople(4) > 0 and player_has_build_unlocked(TRIBE_CYAN, M_BUILDING_DRUM_TOWER) and (_gsi.Players[TRIBE_CYAN].NumPeopleOfType[M_PERSON_SUPER_WARRIOR] > 0 or _gsi.Players[TRIBE_CYAN].NumPeopleOfType[M_PERSON_RELIGIOUS] > 0)) then
        local t_idx = tablelength(cyanTowers)
        local t_rnd = G_RANDOM(t_idx)+1
        if (cyanTowers[t_rnd] ~= nil and FREE_ENTRIES(TRIBE_CYAN) > 1) then
          local map_idx = MapPosXZ.new()
          map_idx.Pos = world_coord3d_to_map_idx(cyanTowers[t_rnd])
          BUILD_DRUM_TOWER(TRIBE_CYAN, map_idx.XZ.X, map_idx.XZ.Z)
          table.remove(cyanTowers, t_rnd)
        end
      end

      if (AITribes[2] == 1 and tablelength(magentaTowers) > 0 and GetPlayerPeople(5) > 0 and player_has_build_unlocked(TRIBE_PINK, M_BUILDING_DRUM_TOWER) and (_gsi.Players[TRIBE_PINK].NumPeopleOfType[M_PERSON_SUPER_WARRIOR] > 0 or _gsi.Players[TRIBE_PINK].NumPeopleOfType[M_PERSON_RELIGIOUS] > 0)) then
        local t_idx = tablelength(magentaTowers)
        local t_rnd = G_RANDOM(t_idx)+1
        if (magentaTowers[t_rnd] ~= nil and FREE_ENTRIES(TRIBE_PINK) > 1) then
          local map_idx = MapPosXZ.new()
          map_idx.Pos = world_coord3d_to_map_idx(magentaTowers[t_rnd])
          BUILD_DRUM_TOWER(TRIBE_PINK, map_idx.XZ.X, map_idx.XZ.Z)
          table.remove(magentaTowers, t_rnd)
        end
      end

      if (AITribes[3] == 1 and tablelength(blackTowers) > 0 and GetPlayerPeople(6) > 0 and player_has_build_unlocked(TRIBE_BLACK, M_BUILDING_DRUM_TOWER) and (_gsi.Players[TRIBE_BLACK].NumPeopleOfType[M_PERSON_SUPER_WARRIOR] > 0 or _gsi.Players[TRIBE_BLACK].NumPeopleOfType[M_PERSON_RELIGIOUS] > 0)) then
        local t_idx = tablelength(blackTowers)
        local t_rnd = G_RANDOM(t_idx)+1
        if (blackTowers[t_rnd] ~= nil and FREE_ENTRIES(TRIBE_BLACK) > 1) then
          local map_idx = MapPosXZ.new()
          map_idx.Pos = world_coord3d_to_map_idx(blackTowers[t_rnd])
          BUILD_DRUM_TOWER(TRIBE_BLACK, map_idx.XZ.X, map_idx.XZ.Z)
          table.remove(blackTowers, t_rnd)
        end
      end

      if (AITribes[4] == 1 and tablelength(orangeTowers) > 0 and GetPlayerPeople(7) > 0 and player_has_build_unlocked(TRIBE_ORANGE, M_BUILDING_DRUM_TOWER) and (_gsi.Players[TRIBE_ORANGE].NumPeopleOfType[M_PERSON_SUPER_WARRIOR] > 0 or _gsi.Players[TRIBE_ORANGE].NumPeopleOfType[M_PERSON_RELIGIOUS] > 0)) then
        local t_idx = tablelength(orangeTowers)
        local t_rnd = G_RANDOM(t_idx)+1
        if (orangeTowers[t_rnd] ~= nil and FREE_ENTRIES(TRIBE_ORANGE) > 1) then
          local map_idx = MapPosXZ.new()
          map_idx.Pos = world_coord3d_to_map_idx(orangeTowers[t_rnd])
          BUILD_DRUM_TOWER(TRIBE_ORANGE, map_idx.XZ.X, map_idx.XZ.Z)
          table.remove(orangeTowers, t_rnd)
        end
      end
    end

    if (every2Pow(2)) then
      for i=1,tablelength(availableNums) do
        local shaman = getShaman(availableNums[i])
        if (shaman ~= nil and cast_arma == 0) then
          if (shaman.State == S_PERSON_DROWNING and player_has_spell_unlocked(shaman.Owner, M_SPELL_LAND_BRIDGE) and AITribes[i] == 1) then
            SearchMapCells(CIRCULAR, 0, 0, 6, world_coord2d_to_map_idx(shaman.Pos.D2), function(me)
              if (is_map_elem_coast(me) > 0 and spell_delay[i] == 0) then
                local c2d = Coord2D.new()
                map_ptr_to_world_coord2d(me, c2d)
                local c3d = Coord3D.new()
                coord2D_to_coord3D(c2d, c3d)
                if (G_RANDOM(landbridge_skill_level[i]) == 0) then
                  createThing(T_SPELL, M_SPELL_LAND_BRIDGE, shaman.Owner, c3d, false, false)
                end
                spell_delay[i] = 128
                return false
              end

              return true
            end)
          end
        end
      end
    end

    if (every2Pow(3)) then
      if (_gsi.Players[TRIBE_YELLOW].NumPeople +
          _gsi.Players[TRIBE_GREEN].NumPeople +
          _gsi.Players[TRIBE_PINK].NumPeople +
          _gsi.Players[TRIBE_CYAN].NumPeople +
          _gsi.Players[TRIBE_ORANGE].NumPeople +
          _gsi.Players[TRIBE_BLACK].NumPeople < 1000 and
          GetTurn() < STurn + (12*60)*4 and
          GetTurn() > STurn + (12*10)) then
        process(numthings)
      end
    end

    if (every2Pow(srate)) then
      for i,v in ipairs(availableNums) do
        if (AITribes[i] == 1 and spell_ms_used[i] < spell_ms_max_use and cast_arma == 0) then
          local s = getShaman(v)
          if (s ~= nil and not isFlagEnabled(s.Flags2, TF2_THING_IN_AIR)) then
            local last_thing = nil
            local total_count = 0
            SearchMapCells(SQUARE,0,0,spell_ms_radius,world_coord2d_to_map_idx(s.Pos.D2),function(me)
              local count = 0
              me.MapWhoList:processList(function(t)
                if (count < 6) then
                  if (t.Type == T_PERSON) then
                    if (t.Model > 2 and t.Model < 7 and t.Owner == s.Owner and not isFlagEnabled(t.Flags3, TF3_SHIELD_ACTIVE) and not isFlagEnabled(t.Flags3, TF3_BLOODLUST_ACTIVE) and not isFlagEnabled(t.Flags2, TF2_THING_IS_A_HYPNOTISED_PERSON)) then
                      count=count+1
                      if (count > 0) then
                       last_thing = t
                       total_count=total_count+1
                      end
                    end
                  end
                  return true
                else return false
                end
              end)
              return true
            end)

            if (last_thing ~= nil and total_count > 2) then
              local thing_s = M_SPELL_SHIELD
              if (player_has_spell_unlocked(v, M_SPELL_BLOODLUST) and b_charges[i] > 0) then
                thing_s = M_SPELL_BLOODLUST
              end

              if ((thing_s == M_SPELL_SHIELD and player_has_spell_unlocked(v, M_SPELL_SHIELD) == true) or thing_s == M_SPELL_BLOODLUST) then
                local support = createThing(T_SPELL,thing_s,s.Owner,last_thing.Pos.D3,false,false)
                support.u.Spell.TargetThingIdx:set(last_thing.ThingNum)
                if (thing_s == M_SPELL_BLOODLUST) then
                  b_charges[i] = b_charges[i] - 1
                else
                  spell_ms_used[i]=spell_ms_used[i]+1
                  if (spell_ms_delay[i] == 0) then
                    spell_ms_delay[i]=calc_limit(v)
                  end
                end
              end
            end
          end
        end
      end
    end

    for i,v in ipairs(spell_ms_delay) do
      if (spell_ms_delay[i] > 0 and spell_ms_used[i] > 0) then
        spell_ms_delay[i]=v-1
      elseif(spell_ms_delay[i] == 0 and spell_ms_used[i] > 0) then
        spell_ms_delay[i]=calc_limit(i)
        spell_ms_used[i]=spell_ms_used[i]-1
      end
    end

    for i,v in ipairs(spell_delay) do
      if (v > 0) then
        spell_delay[i] = v-1
      end
    end
  end

  if (every2Pow(2)) then
    for i,v in ipairs(availableNums) do
      local cast = 0
      local shaman = getShaman(v)
      if (shaman ~= nil and v == spell_tribe_numbers[current_number] and cast_arma == 0) then
        ProcessGlobalTypeList(T_PERSON, function(t)
          if (AITribes[i] == 1 and t.Owner ~= v and are_players_allied(v,t.Owner) == 0) then
            if (get_world_dist_xyz(shaman.Pos.D3, t.Pos.D3) < (getPlayer(v).LimitsSpell.WorldCoordRange[M_SPELL_HYPNOTISM]) + shaman.Pos.D3.Ypos*3 and t.Model > 2 and t.Model < 7 and isFlagEnabled(t.Flags3, TF3_SHIELD_ACTIVE) and h_charges[i] > 0 and not isFlagEnabled(t.Flags2, TF2_THING_IS_AN_INVISIBLE_PERSON) and player_has_spell_unlocked(v, M_SPELL_HYPNOTISM) and (count_people(v,t.Pos.D3,2) > 4)) then
              if (not isFlagEnabled(shaman.Flags2, TF2_THING_IN_AIR)) then
                local hypno = createThing(T_SPELL, M_SPELL_HYPNOTISM, shaman.Owner, t.Pos.D3, false, false)
                hypno.u.Spell.TargetThingIdx:set(t.ThingNum)
                --createThing(T_SPELL, M_SPELL_HYPNOTISM, shaman.Owner, t.Pos.D3, false, false)
                h_charges[i] = h_charges[i] - 1
                cast = 1
                return false
              end
            elseif (get_world_dist_xyz(shaman.Pos.D3, t.Pos.D3) < (getPlayer(v).LimitsSpell.WorldCoordRange[M_SPELL_LIGHTNING_BOLT]) + shaman.Pos.D3.Ypos*3 and t.Model == M_PERSON_MEDICINE_MAN and l_charges[i] > 0 and player_has_spell_unlocked(v, M_SPELL_LIGHTNING_BOLT) and t.State ~= S_PERSON_ELECTROCUTED and not isFlagEnabled(t.Flags2, TF2_THING_IS_AN_INVISIBLE_PERSON) and not isFlagEnabled(t.Flags3, TF3_SHIELD_ACTIVE)) then
              if (not isFlagEnabled(shaman.Flags2, TF2_THING_IN_AIR)) then
                local aimLoc = Coord3D.new()
                local c2d = Coord2D.new()
                local xLoc = 0
                local zLoc = 0
                xLoc = t.Pos.D3.Xpos + (10 * t.Move.Velocity.X)
                zLoc = t.Pos.D3.Zpos + (10 * t.Move.Velocity.Z)
                c2d.Xpos = xLoc
                c2d.Zpos = zLoc
                coord2D_to_coord3D(c2d, aimLoc)
                if (G_RANDOM(2) == 1) then
                  createThing(T_SPELL, M_SPELL_LIGHTNING_BOLT, shaman.Owner, t.Pos.D3, false, false)
                else
                  if (G_RANDOM(2) == 1) then
                    local bolt = createThing(T_SPELL, M_SPELL_LIGHTNING_BOLT, shaman.Owner, t.Pos.D3, false, false)
                    bolt.u.Spell.TargetThingIdx:set(t.ThingNum)
                    --log_msg(shaman.Owner, "I cast at " .. t.Owner .. " With Mode: S_Click")
                  else
                    local bolt = createThing(T_SPELL, M_SPELL_LIGHTNING_BOLT, shaman.Owner, aimLoc, false, false)
                    --log_msg(shaman.Owner, "I cast at " .. t.Owner .. " With Mode: Aim")
                  end
                end
                --createThing(T_SPELL, M_SPELL_LIGHTNING_BOLT, shaman.Owner, t.Pos.D3, false, false)
                l_charges[i] = l_charges[i] - 1
                cast = 1
                return false
              end
            elseif (get_world_dist_xyz(shaman.Pos.D3, t.Pos.D3) < (getPlayer(v).LimitsSpell.WorldCoordRange[M_SPELL_INSECT_PLAGUE]) + shaman.Pos.D3.Ypos*3 and t.Model == M_PERSON_SUPER_WARRIOR and s_charges[i] > 0 and player_has_spell_unlocked(v, M_SPELL_INSECT_PLAGUE) and not isFlagEnabled(t.Flags2, TF2_THING_IS_AN_INVISIBLE_PERSON) and not isFlagEnabled(t.Flags3, TF3_SHIELD_ACTIVE) and isFlagEnabled(t.Flags2, TF2_IN_AIRSHIP)) then -- is_person_in_airship(t) > 0
              if (not isFlagEnabled(shaman.Flags2, TF2_THING_IN_AIR)) then
                local swarms = createThing(T_SPELL, M_SPELL_INSECT_PLAGUE, shaman.Owner, t.Pos.D3, false, false)
                swarms.u.Spell.TargetThingIdx:set(t.ThingNum)
                s_charges[i] = s_charges[i] - 1
                cast = 1
                return false
              end
            elseif (get_world_dist_xyz(shaman.Pos.D3, t.Pos.D3) < (getPlayer(v).LimitsSpell.WorldCoordRange[M_SPELL_BLAST]) + shaman.Pos.D3.Ypos*3 and ((t.Model == M_PERSON_RELIGIOUS and is_person_preaching(t) > 0) or (t.Model == M_PERSON_SUPER_WARRIOR and isFlagEnabled(shaman.Flags3,TF3_LIFE_CRITICAL)) or t.Model == M_PERSON_MEDICINE_MAN) and blast_charges[i] > 0 and player_has_spell_unlocked(v, M_SPELL_BLAST) and not isFlagEnabled(t.Flags2, TF2_THING_IS_AN_INVISIBLE_PERSON) and not isFlagEnabled(t.Flags3, TF3_SHIELD_ACTIVE) and is_person_on_a_building(t) < 1) then
              if (not isFlagEnabled(shaman.Flags2, TF2_THING_IN_AIR)) then
                local blasts = createThing(T_SPELL, M_SPELL_BLAST, shaman.Owner, t.Pos.D3, false, false)
                blasts.u.Spell.TargetThingIdx:set(t.ThingNum)
                blast_charges[i] = blast_charges[i] - 1
                cast = 1
                return false
              end
            elseif (get_world_dist_xyz(shaman.Pos.D3, t.Pos.D3) < (getPlayer(v).LimitsSpell.WorldCoordRange[M_SPELL_GHOST_ARMY]) + shaman.Pos.D3.Ypos*3 and t.Model == 8 and g_charges[i] > 0 and player_has_spell_unlocked(v, M_SPELL_GHOST_ARMY)) then
              local thingyPos = MapPosXZ.new()
              thingyPos.Pos = world_coord3d_to_map_idx(t.Pos.D3)
              local c3d = Coord3D.new()
              map_idx_to_world_coord3d(thingyPos.Pos, c3d)
              thingyPosY = c3d.Ypos
              if (not isFlagEnabled(shaman.Flags2, TF2_THING_IN_AIR) and thingyPosY > 0) then
                local ghosts = createThing(T_SPELL, M_SPELL_GHOST_ARMY, shaman.Owner, t.Pos.D3, false, false)
                ghosts.u.Spell.TargetThingIdx:set(t.ThingNum)
                g_charges[i] = g_charges[i] - 1
                cast = 1
                return false
              end
            end
          end

          return true
        end)

        if (cast == 0) then
          ProcessGlobalTypeList(T_BUILDING, function(t)
            if (AITribes[i] == 1 and player_has_spell_unlocked(v, M_SPELL_GHOST_ARMY) and t.Owner ~= v and are_players_allied(v,t.Owner) == 0) then
              if (get_world_dist_xyz(shaman.Pos.D3, t.Pos.D3) < (getPlayer(v).LimitsSpell.WorldCoordRange[M_SPELL_GHOST_ARMY]) + shaman.Pos.D3.Ypos*3 and t.Model == 4 and g_charges[i] > getPlayer(v).LimitsSpell.MaxCharges[M_SPELL_GHOST_ARMY] - 2) then
                if (not isFlagEnabled(shaman.Flags2, TF2_THING_IN_AIR)) then
                  createThing(T_SPELL, M_SPELL_GHOST_ARMY, shaman.Owner, t.Pos.D3, false, false)
                  g_charges[i] = g_charges[i] - 1
                  cast = 1
                  return false
                end
              end
            end

            return true
          end)
        end
      end

    end
    current_number = current_number + 1
    if (current_number > 7) then
      current_number = 1
    end
  end

  for i = 1,7 do
    if (l_charges[i] < getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_LIGHTNING_BOLT]) then
      l_charge_time[i] = l_charge_time[i] - 1
      if (l_charge_time[i] < 1) then
        l_charges[i] = l_charges[i] + 1
        l_charge_time[i] = 600
      end
    else
      l_charges[i] = getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_LIGHTNING_BOLT]
    end
    if (h_charges[i] < getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_HYPNOTISM]) then
      h_charge_time[i] = h_charge_time[i] - 1
      if (h_charge_time[i] < 1) then
        h_charges[i] = h_charges[i] + 1
        h_charge_time[i] = 720
      end
    else
      h_charges[i] = getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_HYPNOTISM]
    end
    if (g_charges[i] < getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_GHOST_ARMY]) then
      g_charge_time[i] = g_charge_time[i] - 1
      if (g_charge_time[i] < 1) then
        g_charges[i] = g_charges[i] + 1
        g_charge_time[i] = 540
      end
    else
      g_charges[i] = getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_GHOST_ARMY]
    end
    if (b_charges[i] < getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_BLOODLUST]) then
      b_charge_time[i] = b_charge_time[i] - 1
      if (b_charge_time[i] < 1) then
        b_charges[i] = b_charges[i] + 1
        b_charge_time[i] = 5760
      end
    end
    if (s_charges[i] < getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_INSECT_PLAGUE]) then
      s_charge_time[i] = s_charge_time[i] - 1
      if (s_charge_time[i] < 1) then
        s_charges[i] = s_charges[i] + 1
        s_charge_time[i] = 480
      end
    else
      s_charges[i] = getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_INSECT_PLAGUE]
    end
    if (blast_charges[i] < getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_BLAST]) then
      blast_charge_time[i] = blast_charge_time[i] - 1
      if (blast_charge_time[i] < 1) then
        blast_charges[i] = blast_charges[i] + 1
        blast_charge_time[i] = 360
      end
    else
      blast_charges[i] = getPlayer(i).LimitsSpell.MaxCharges[M_SPELL_BLAST]
    end
    if (t_charge_time[i] > 0) then
      t_charge_time[i] = t_charge_time[i] - 1
    end
  end
end

ProcessGlobalTypeList(T_PERSON, function(t)
  if (t.Model == M_PERSON_WILD) then
    table.insert(wilds, t)
  end
  return true
end)

function OnCreateThing(t)
  if (t.Type == T_PERSON and t.Model == M_PERSON_WILD) then
    table.insert(wilds, t)
  end
end

function process(n)
  for i = 0,n do
    if (index < tablelength(wilds)) then
      local t = GetThing(wilds[index].ThingNum)
      if (t ~= nil and t.Type == T_PERSON and t.Model == M_PERSON_WILD) then
        local idx = G_RANDOM(tablelength(availableNums))+1
        local s = getShaman(availableNums[idx])
        if (AITribes[idx] == 1 and s ~= nil and player_has_spell_unlocked(availableNums[idx], M_SPELL_CONVERT_WILD) == true and cast_arma == 0) then
          if(get_world_dist_xyz(t.Pos.D3, s.Pos.D3) < (8192 + s.Pos.D3.Ypos*3) and spell_delay[idx] == 0) then
            createThing(T_SPELL, M_SPELL_CONVERT_WILD, s.Owner, t.Pos.D3, false, false)
            spell_delay[idx] = 24
          elseif(get_world_dist_xyz(t.Pos.D3, conv_center_pos[idx]) < 512*16 and get_thing_curr_cmd_list_ptr(s) == nil) then
            command_person_go_to_coord2d(s, t.Pos.D2)
          end
        end
      else
        table.remove(wilds, index)
      end
      index = index+1
    else
      index = 1
    end
  end
end

function calc_limit(pn)
  if (_gsi.Players[pn].NumPeople > spell_ms_charge_time) then
    return 1 else return spell_ms_charge_time-_gsi.Players[pn].NumPeople
  end
end

function count_enemy_bldgs_around(myself,x,z,radiii)
  local count = 0
  local c3d = MAP_XZ_2_WORLD_XYZ(x,z)
  SearchMapCells(SQUARE,0,0,radiii,world_coord3d_to_map_idx(c3d),function(me)
    me.MapWhoList:processList(function(t)
      if (t.Type == T_BUILDING) then
        if (t.Owner ~= myself and are_players_allied(t.Owner,myself) == 0) then
          count=count+1
        end
      end
      return true
    end)
    return true
  end)
  return count
end

function count_people(myself,coord2,radii)
  local count = 0
  SearchMapCells(CIRCULAR, 0, 0, radii, world_coord3d_to_map_idx(coord2), function(me)
    me.MapWhoList:processList(function (t)
      if (t.Type == T_PERSON) then
        if (t.Owner ~= myself and are_players_allied(myself,t.Owner) == 0) then
          count = count+1
        end
      end
      return true
    end)
    return true
  end)

  return count
end

function decide_an_enemy_to_attack(myself)
  local target = myself
  local i = myself
  local tries = 24
  while (tries > 0) do
    tries=tries-1
    i = who_to_attack_calculation(myself)
    if (i ~= myself and are_players_allied(i,myself) == 0) then
      if (GetPlayerPeople(i) > 0) then
        if (NAV_CHECK(myself,i,ATTACK_BUILDING,0,0) == 1) then
            target = i
          break
        end
      end
    end
  end
  return target
end

function count_troops(pn)
  local count = 0
  ProcessGlobalTypeList(T_PERSON, function(t)
    if (t.Owner == pn) then
      if (t.Model == M_PERSON_WARRIOR) then
        count=count+1
      end
      if (t.Model == M_PERSON_RELIGIOUS) then
        count=count+1
      end
      if (t.Model == M_PERSON_SUPER_WARRIOR) then
        count=count+1
      end
    end
    return true
  end)

  return count
end

function count_total_friendly_people(pn)
  local count = 0
  ProcessGlobalTypeList(T_PERSON, function(t)
    if (t.Owner == pn) then
      if (t.Model > 1 and t.Model < 8) then
        count=count+1
      end
    end
    if (are_players_allied(pn,t.Owner) == 1) then
      if (t.Model > 1 and t.Model < 8) then
        count=count+1
      end
    end
    return true
  end)

  return count
end

function count_total_enemy_people(pn)
  local count = 0
  ProcessGlobalTypeList(T_PERSON, function(t)
    if (t.Owner ~= pn and not are_players_allied(pn,t.Owner)) then
      if (t.Model > 1 and t.Model < 8) then
        count=count+1
      end
    end
  end)

  return count
end

function OnCreateThing(t)
  if (t.Type == T_SPELL) then
    if (GET_NUM_ONE_OFF_SPELLS(t.Owner,t.Model) > 0 and AITribes[position_in_table[t.Owner]] == 1) then
      PThing.GiveShot(t.Owner,t.Model,-1)
    end
  end
end

function count_available_balloons(myself)
  local count = 0
  ProcessGlobalTypeList(T_BUILDING, function(t)
    if (t.Owner == myself and t.Model == M_BUILDING_AIRSHIP_HUT_1) then
      SearchMapCells(CIRCULAR,0,0,30,world_coord3d_to_map_idx(t.Pos.D3),function(me)
        me.MapWhoList:processList(function(b)
          if (b.Type == T_VEHICLE) then
            if (b.Model == 3 and b.Owner == myself) then
              count=count+1
            end
          end
          return true
        end)
        return true
      end)
    end
    return true
  end)
  return count
end

function decide_an_enemy_to_attack_air(myself)
  local target = myself
  local i = myself
  local tries = 16
  while (tries > 0) do
    tries=tries-1
    i = G_RANDOM(MAX_NUM_REAL_PLAYERS)
    if (i ~= myself and are_players_allied(i,myself) == 0) then
      if (GetPlayerPeople(i) > 0) then
        target = i
        break
      end
    end
  end
  return target
end

function OnPlayerDeath(pn)
  for i = 1,7 do
    if (landbridge_skill_level[i] > 1) then
      landbridge_skill_level[i] = landbridge_skill_level[i] - 1
    end
  end
end