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:   FillChar(UnitReq,SizeOf(UnitReq),#0); //Clear up
  
    //Citizens
    for i:=1 to HOUSE_COUNT do begin //Count overall unit requirement
      if THouseType(i)<>ht_Barracks then //Exclude Barracks
      if HouseDAT[i].OwnerType<>-1 then //Exclude houses without owners
      inc(UnitReq[HouseDAT[i].OwnerType+1], Assets.GetHouseQty(THouseType(i)));
    end;
  
    SetLength(Schools,Assets.GetHouseQty(ht_School));
    k := 1;
    HS := TKMHouseSchool(Assets.FindHouse(ht_School,k));
    while HS <> nil do
    begin
      Schools[k-1] := HS;
      if HS.UnitQueue[1]<>ut_None then
        dec(UnitReq[integer(HS.UnitQueue[1])]); //Decrease requirement for each unit in training
      inc(k);
      HS := TKMHouseSchool(Assets.FindHouse(ht_School,k));
    end;
  
    for k:=1 to Length(Schools) do
    begin
      HS := Schools[k-1];
      if (HS<>nil)and(HS.UnitQueue[1]=ut_None) then
      begin
        for i:=1 to length(UnitReq) do
          if UnitReq[i] > Assets.GetUnitQty(TUnitType(i)) then
          begin
  
negative.code: 
    function CheckUnitRequirements(Req:integer; aUnitType:TUnitType):boolean;
    begin
      //We summ up requirements for e.g. Recruits required at Towers and Barracks
      if Assets.GetUnitQty(aUnitType) < (Req+UnitReq[byte(aUnitType)]) then
      begin
        dec(UnitReq[byte(aUnitType)]); //So other schools don't order same unit
        HS.AddUnitToQueue(aUnitType);
        Result := true;
      end
  