-- 4k0-soft / Harvest More / 09.12.2016 -- 
hm = {}
hm.multi = 3.0;

function hm:debug(message)
  print(('### Harvest More: %s'):format(message));
end;

function hm:rpad(str, len, char)
    if len == nil then len = 15 end
    if char == nil then char = ' ' end
    return hm:ternary(str ~= nil, string.rep(char, len - #tostring(str)) .. tostring(str), string.rep(char, len));
end
function hm:lpad(str, len, char)
    if len == nil then len = 15 end
    if char == nil then char = ' ' end
    return hm:ternary(str ~= nil, tostring(str) .. string.rep(char, len - #tostring(str)), string.rep(char, len));
end
function hm:ternary(cond, T, F)
    if cond then return T else return F end
end

function hm:printFruiteTypes()
  hm:debug('printing fruiteTypes');
  --DebugUtil.printTableRecursively(g_currentMission.fruitTypeManager.fruitTypes, ".", 0, 10);

  hm:debug(('%s | %s | %s | %s | %s | %s | %s | %s | %s | %s')
    :format(hm:rpad('name'), hm:rpad('useForFieldJob'), hm:rpad('needsSeeding'), hm:rpad('allowsSeeding'),  hm:rpad('seedUsagePerSqm', 20), 
            hm:rpad('literPerSqm', 20), hm:rpad('hasWindrow'), hm:rpad('windrowName'), hm:rpad('windrowLiterPerSqm', 20), hm:rpad('preparingOutputName', 20)));

  for k,v in pairs(g_currentMission.fruitTypeManager.fruitTypes) do
      --DebugUtil.printTableRecursively(v, ".", 0, 10);
      hm:debug(('%s | %s | %s | %s | %s | %s | %s | %s | %s | %s')
        :format(hm:rpad(v.name), hm:rpad(v.useForFieldJob), hm:rpad(v.needsSeeding), hm:rpad(v.allowsSeeding),  hm:rpad(v.seedUsagePerSqm, 20), 
                hm:rpad(v.literPerSqm, 20), hm:rpad(v.hasWindrow), hm:rpad(v.windrowName), hm:rpad(v.windrowLiterPerSqm, 20), hm:rpad(v.preparingOutputName, 20)));
      --end;
  end;
end;

function hm:updateFruiteTypes()
  for k,v in pairs(g_currentMission.fruitTypeManager.fruitTypes) do
    if v.allowsSeeding ~= nil and v.allowsSeeding then
      hm:debug(('fruit %s allows seeding!'):format(v.name));
      hm:debug(('changing seedUsagePerSqm from %f to %f'):format(v.seedUsagePerSqm, v.seedUsagePerSqm * hm.multi));
      v.seedUsagePerSqm = v.seedUsagePerSqm * hm.multi;
      
      hm:debug(('changing literPerSqm from %f to %f'):format(v.literPerSqm, v.literPerSqm * hm.multi));
      v.literPerSqm = v.literPerSqm * hm.multi;

      if v.hasWindrow ~= nil and v.hasWindrow and v.windrowName ~= 'STRAW' then
        hm:debug(('changing windrowLiterPerSqm from %f to %f'):format(v.windrowLiterPerSqm, v.windrowLiterPerSqm * hm.multi));
        v.windrowLiterPerSqm = v.windrowLiterPerSqm * hm.multi;
      end;
    else
      hm:debug(('fruit %s doesn\'t allow seeding!'):format(v.name));
    end;
  end;
end;

function hm:printAnimalTypes()
  --hm:debug('printing AnimalManager');
  --DebugUtil.printTableRecursively(g_currentMission.inGameMenu.pageAnimals.animalManager, ".", 0, 5);
  --hm:debug('printing animalTypes');
  --DebugUtil.printTableRecursively(g_currentMission.inGameMenu.pageAnimals.animalManager.animals, ".", 0, 10);

  hm:debug(('%s | %s | %s | %s | %s | %s | %s | %s | %s | %s')
    :format(hm:rpad('type'), hm:rpad('subTypeName', 25), hm:rpad('strawPerDay'), hm:rpad('waterPerDay'),  hm:rpad('foodPerDay'), 
            hm:rpad('manurePerDay'), hm:rpad('liquidManurePerDay'), hm:rpad('milkPerDay'), hm:rpad('palletsPerDay'), hm:rpad('birthRatePerDay', 20)));

  for k,v in pairs(g_currentMission.inGameMenu.pageAnimals.animalManager.animals) do
      --DebugUtil.printTableRecursively(v, ".", 0, 10);
	  for sk,sv in pairs(v.subTypes) do
      hm:debug(('%s | %s | %s | %s | %s | %s | %s | %s | %s | %s')
        :format(hm:rpad(v.class), hm:rpad(sv.subTypeName, 25), hm:rpad(sv.input.strawPerDay), hm:rpad(sv.input.waterPerDay),  hm:rpad(sv.input.foodPerDay), 
                hm:rpad(sv.output.manurePerDay), hm:rpad(sv.output.liquidManurePerDay), hm:rpad(sv.output.milkPerDay), hm:rpad(sv.output.palletsPerDay), hm:rpad(sv.breeding.birthRatePerDay, 20)));
	end;
  end;
end;

function hm:updateAnimalTypes()

  for k,v in pairs(g_currentMission.inGameMenu.pageAnimals.animalManager.animals) do
      --DebugUtil.printTableRecursively(v, ".", 0, 10);
	  for sk,sv in pairs(v.subTypes) do

      if v.class == 'Animal' and sv.output.milkPerDay > 0.0 then
        hm:debug(('animal %s gives milk!  Changing milkPerDay      from %f to %f'):format(sv.subTypeName, sv.output.milkPerDay, sv.output.milkPerDay * hm.multi));
        sv.output.milkPerDay = sv.output.milkPerDay * hm.multi;
      end;

      if v.class == 'Animal' and sv.output.palletsPerDay > 0.0 then
        hm:debug(('animal %s drops smthg! Changing palletsPerDay   from %f to %f'):format(sv.subTypeName, sv.output.palletsPerDay, sv.output.palletsPerDay * hm.multi));
        sv.output.palletsPerDay = sv.output.palletsPerDay * hm.multi;
      end;

      if v.class == 'Animal' and sv.breeding.birthRatePerDay > 0.0 then
        hm:debug(('animal %s reproduces!  Changing birthRatePerDay from %f to %f'):format(sv.subTypeName, sv.breeding.birthRatePerDay, sv.breeding.birthRatePerDay * hm.multi));
        sv.breeding.birthRatePerDay = sv.breeding.birthRatePerDay * hm.multi;
      end;
    end;
  end;
end;

function hm:loadMap(name)
  hm:printFruiteTypes();
  hm:updateFruiteTypes();
  
  hm:printAnimalTypes();
  hm:updateAnimalTypes();
end;

function hm:keyEvent(unicode, sym, modifier, isDown)
end;

function hm:update(dt)
end;

function hm:draw()
end;

function hm:deleteMap()
end;

function hm:mouseEvent(posX, posY, isDown, isUp, button)
end;

addModEventListener(hm);





























