anchor.diff:  unit KM_PlayerAI;
   {$I KaM_Remake.inc}
   interface
  -uses Classes, KM_CommonTypes, KM_Defaults, KromUtils, KM_Player, KM_Utils, KM_Houses, KM_Units, KM_Units_Warrior;
  +uses Classes, Math, KromUtils,
  +    KM_CommonTypes, KM_Defaults, KM_Houses, KM_Player, KM_Units, KM_Units_Warrior, KM_Utils;
   
   type
     TAIDefencePosType = (adt_FrontLine, //Front line troops may not go on attacks, they are for defence
  
positive.code: 
    function CheckUnitRequirements(Req:integer; aUnitType:TUnitType):boolean;
    begin
      if Assets.GetUnitQty(aUnitType) < (Req+UnitReq[integer(aUnitType)]) then
      begin
        dec(UnitReq[integer(aUnitType)]); //So other schools don't order same unit
        HS.AddUnitToQueue(aUnitType);
        Result := true;
      end
  
negative.code:   FillChar(UnitReq,SizeOf(UnitReq),#0); //Clear up
  
    //Citizens
    //Count overall unit requirement (excluding Barracks and ownerless houses)
    for i:=1 to HOUSE_COUNT do
      if (HouseDAT[i].OwnerType<>-1) and (THouseType(i)<>ht_Barracks) then
        inc(UnitReq[HouseDAT[i].OwnerType+1], Assets.GetHouseQty(THouseType(i)));
  
    //Schools
    //Count overall schools count and exclude already training units from UnitReq
    SetLength(Schools,Assets.GetHouseQty(ht_School));
    k := 1;
    HS := TKMHouseSchool(Assets.FindHouse(ht_School,k));
    while HS <> nil do
    begin
      Schools[k-1] := HS;
      for i:=1 to 6 do //Decrease requirement for each unit in training
        if HS.UnitQueue[i]<>ut_None then
          UnitReq[byte(HS.UnitQueue[i])] := max(UnitReq[byte(HS.UnitQueue[i])] - 1, 0);
      inc(k);
      HS := TKMHouseSchool(Assets.FindHouse(ht_School,k));
    end;
  
    //Order the training
    for k:=1 to Length(Schools) do
    begin
      HS := Schools[k-1];
      if (HS<>nil)and(HS.UnitQueue[1]=ut_None) then
      begin
        //Order citizen training
        for i:=1 to length(UnitReq) do
          if UnitReq[i] > Assets.GetUnitQty(TUnitType(i)) then
          begin
  