FANDOM


local p = {}
function p.creaturemap( f )
  local regions = f:getParent().args
  local size, opacity, color, link, missingRegions, linktitle, map, mapfile, isCave = '400px', 0.5, '', '', false, '', 'The Island', 'Map_The_Island.jpg', false
  local regionDivs, symbolDivs = {}, {}
  local rarityLegend = {'Very Common', 'Common', 'Uncommon', 'Very Uncommon', 'Rare', 'Very Rare'}
  local usedRarities = {
    ['very common'] = false,
    ['common'] = false,
    ['uncommon'] = false,
    ['very uncommon'] = false,
    ['rare'] = false,
    ['very rare'] = false,
    ['untameable'] = false
  }
  if regions.size ~= nil then
    size = regions.size
    regions.size = nil
  end
  if regions.opacity ~= nil then
    opacity = regions.opacity
    regions.opacity = nil
  end
  if regions.map ~= nil then
    map = regions.map
    regions.map = nil
  end
 
  local regionColorClasses = {
    ['very common'] = 'cr-region-map-very-common',
    ['common'] = 'cr-region-map-common',
    ['uncommon'] = 'cr-region-map-uncommon',
    ['very uncommon'] = 'cr-region-map-very-uncommon',
    ['rare'] = 'cr-region-map-rare',
    ['very rare'] = 'cr-region-map-very-rare',
  }
 
  local regionData
  if map == 'The Island' then
    regionData = mw.loadData( 'Module:CreatureMap/regions' )
  elseif map == 'The Center' then
    regionData = mw.loadData( 'Module:CreatureMap/centerregions' )
    mapfile = 'The_Center_Topographic.jpg'
  elseif map == 'Scorched Earth' then
    regionData = mw.loadData( 'Module:CreatureMap/scorchedregions' )
    mapfile = 'Scorched_Earth_Map.jpg'
  end
  local colors = {
    ['very common'] = 'cr-region-map-cl-very-common',
    ['common'] = 'cr-region-map-cl-common',
    ['uncommon'] = 'cr-region-map-cl-uncommon',
    ['very uncommon'] = 'cr-region-map-cl-very-uncommon',
    ['rare'] = 'cr-region-map-cl-rare',
    ['very rare'] = 'cr-region-map-cl-very-rare',
    ['unknown'] = 'cr-region-map-cl-unknown'
  }
 
  for region,rarity in pairs(regions) do
    rarity = string.lower(rarity)
    if rarity ~= 'no' and regionData[region] ~= nil then
 
      isCave = string.sub(region,1,4) == 'cave'
      local isUntameable
      rarity, isUntameable = string.gsub(rarity,'%s*untameable%s*','')
 
      if colors[rarity] ~= nil then
        color = colors[rarity]
        usedRarities[rarity] = true
      else
        color = colors.unknown
        missingRegions = true
      end
      -- show regionshape only if creature surely occurs
      if color ~= colors.unknown and isCave ~= true then
        if regionDivs[rarity] == nil then
          regionDivs[rarity] = {}
        end
        table.insert(regionDivs[rarity],'<div style="position:absolute">[[File:Region '..regionData[region].name..'.png|'..size..']]</div>')
      end
      linktitle = ''
      if string.lower(string.sub(regionData[region].name,1,3)) == 'sea' then
        link = 'Sea'
        linktitle = ' title="'..regionData[region].name..'"'
      else
        if regionData[region].linktitle ~= nil then
          linktitle = ' title="'..regionData[region].linktitle..'"'
        end
        link = regionData[region].name
      end
      local cssClass, cssClassStripes = ' cr-region-map-marker', ''
      if isCave then
        cssClass = cssClass .. ' cr-region-map-markercave'
      end
      if isUntameable > 0 then
        cssClassStripes = ' class="stripes"'
        usedRarities['untameable'] = true
      end
      table.insert(symbolDivs,'<div class="'..color..cssClass..'" style="left:'..regionData[region].left..'%;top:'..regionData[region].top..'%;"><div'..cssClassStripes..'>[['..link..'|<span style="color:black;padding:.1em .2em"'..linktitle..'>'..regionData[region].abbr..'</span>]]</div></div>')
    end
  end
 
  local regionDivsContainer = ''
  for cRarity,cRegions in pairs(regionDivs) do
    regionDivsContainer = regionDivsContainer..'<div style="position:absolute" class="'..regionColorClasses[cRarity]..'">'..table.concat(cRegions)..'</div>'
  end
 
  local missingRegionsText, legendUnknown = '[[Category:Completed Creaturemap]]', ''
  if missingRegions then
    missingRegionsText = '[[Category:Incomplete Creaturemap]]<div style="color:darkred;text-align:middle;padding:3px 1px">This creature is missing habitat information.<br/>You can help by finding its map locations in-game and updating them</div>'
    legendUnknown = '<div class="'..colors.unknown..'" style="display:inline-block;border-radius:2px;width:1.5em;">&nbsp;</div>&nbsp;Unknown'
  end
 
  local legend, rarityKeys = {}, {'very common','common','uncommon','very uncommon','rare','very rare'}
  for l=1,6 do
    if usedRarities[rarityKeys[l]] then
      table.insert(legend,'<div class="'..colors[rarityKeys[l]]..'" style="display:inline-block;border-radius:2px;width:1.5em;border:1px solid lightgray;">&nbsp;</div>&nbsp;'..rarityLegend[l]..' ')
    end
  end
 
  if usedRarities['untameable'] then
    table.insert(legend,'<div class="'..colors['unknown']..' stripes" style="display:inline-block;border-radius:2px;width:1.5em;position:relative;">&nbsp;</div>&nbsp;Untameable ')
  end
 
  return missingRegionsText..'<div class="noviewer" style="position: relative;width:'..size..';height:'..size..'"><div style="position:absolute" class="cr-region-map-map">[[File:'..mapfile..'|'..size..']]</div><div style="position:absolute;opacity:'..opacity..'">'..regionDivsContainer..'</div><div style="position:absolute;width:100%;height:100%">'..table.concat(symbolDivs)..'</div></div>'
  ..'<div style="float:right;font-size:.7em;margin-bottom: -0.6em;">[[colorblind]]?</div>'..
-- legend
'<div style="padding:.7em 0;"><small>'..table.concat(legend)..legendUnknown..'</small></div>'
end
return p