k_form()

A place for discussion of making game modifications.
Jem
Posts: 31
Joined: Tue Aug 15, 2017 9:21 am

k_form()

Postby Jem » Thu Jul 30, 2020 12:59 pm

TopBarContents.png
TopBarContents.png (10.8 KiB) Viewed 33110 times

As the game progresses with huge gain in resources shown above, I noticed the number formats are less than ideal, so I looked for the code that does this and later rework k_form() function to below code that formats to 3 digits with decimal and designated +/- prefixes that cleaned up the original code much.
\Lua state\SharedLua\string_functions.lua

Code: Select all

--jem e.g. k_form(234,"+","-")
function k_form(...) -- max 3digits with decimal
  local count=#{...}
  local arg={...}
  local n=arg[1]
  if count==0 return tostring(0) end -- why no arg?

  local k=1000 -- thousand
  local m=1000*k -- million
  local b=1000*m  -- billion
  local a=math.abs(n) -- absolute value of number
  local s=""

  if(a==0) return tostring(0)
  elseif(a>=b) s=format("%0.2fb",a/b)
  elseif(a>=100*m) s=format("%0.0fm",a/m)
  elseif(a>=10*m) s=format("%0.1fm",a/m)
  elseif(a>=m) s=format("%0.2fm",a/m)
  elseif(a>=100*k) s=format("%0.0fk",a/k)
  elseif(a>=10*k) s=format("%0.1fk",a/k)
  elseif(a>=k) s=format("%0.2fk",a/k)
  elseif(a>0) s=format("%d",a)
  end

  if count==3
    pos=arg[2] -- positive prefix, e.g. "+", can be blank ""
    neg=arg[3] -- negative prefix, e.g. "-", can be blank ""
    if n>0 s= pos .. s
    elseif n<0 s= neg .. s end
  end
  return s
end
Original

Code: Select all

function k_form(n)

   if(n==0) return tostring(0) end

   if(n>=100000)
      return format("%0.0fk",n*.001)
   end

   if(n>=100)

      if( math.fmod(n,1000)==0)
         return format("%0.0fk",n*.001)
      end

      return format("%.1fk",n*.001)
   end

   if(n>=10)
      return format("%.2fk",n*.001)
   end


--   if(rval:sub(1,1)=="0") return rval:sub(2,-1) end

   return format("%.3fk",n*.001)
end

Updating above function will change to this,
TopBarContents2.png
TopBarContents2.png (9.78 KiB) Viewed 33110 times


So I went to edit to
\Lua state\GUI\~GalaxyMap\@TopBarContents.lua
Original. Below are parts for Coin, Metal & Production and only Coin has k_form()

Code: Select all

local draw_coins = top_hover_fun | function ()
   local empire = gui_player()
   local credits in! empire

   local income, taxes, trade, ship_up, planet_up =  empire : income()

--  local income, taxes, trade, ship_up, planet_up in INIT.zero


   local neg_credits = credits < 0
   if neg_credits
      credits*=-1
   end

   local neg_income = income < 0

   if neg_income
      income*=-1
   end

   if(credits>20000 and income > 500)
      credits=k_form(credits)
      income=k_form(income)
   end


  local income_str = format("(%s%s)",neg_income and '-' or '+', income)

  if not empire.has_inhabited_planets
    income_str=''
  end


   local cx,cy,cw,ch = top_bar.empire_coins("%s%s " .. income_str, neg_credits and '-' or '', credits)
.
.
.
--!! metal

local draw_metal = top_hover_fun | function ()
  local empire=gui_player()
  local el = element | top_bar.empire_metal | format( '%d (%s)' , empire.metal, plus_form | empire.mem_metal_production ) 


  top_hover(top_bar.metalx,|) metal_hover_text | empire
end

-- production
local production_text = weak_memoizeNN | function(empire)
  return format('(+%d)',empire.total_raw_hammer_production)
end
So I changed them to

Code: Select all

local draw_coins = top_hover_fun | function ()
  local empire = gui_player()
  local credits in! empire
  local myincome, taxes, trade, ship_up, planet_up =  empire : income()
  local income_str = "=" .. k_form(income,"+","-") ..")"

  if not empire.has_inhabited_planets income_str='' end
  local cx,cy,cw,ch = top_bar.empire_coins("%s " .. income_str, k_form(credits,"","-"))
.
.
.
--!! metal

local draw_metal = top_hover_fun | function ()
  local empire=gui_player()
  local el = element | top_bar.empire_metal | format( "%s (%s)" , k_form(empire.metal,"","-"), k_form(empire.mem_metal_production,"+","-")) 


  top_hover(top_bar.metalx,|) metal_hover_text | empire
end

-- production
local production_text = weak_memoizeNN | function(empire)
  return format('(%s)',k_form(empire.total_raw_hammer_production,"+",""))
end
But it is the same like without editing... Metal & Production are not k_form()-ed, so I'm very confused. If anyone can help enlighten, many thanks!
Jem

Return to “Modding”

Who is online

Users browsing this forum: No registered users and 2 guests

cron