Log In  
Follow
Miez
[ :: Read More :: ]

OK, I know we can use ternary conditions in variable assignments. But - as far as I can tell - they can't be used in calculations. Having logical statements be used as a value operator in a calculation can be used in many circumstances, would be readable and possibly pretty token efficient.

How might this look?

(a > b) would result in a 1 if true, 0 if false.

something like "pset(100+100*(a > b), 200, 7)" would prevent the use of a bunch of ternary assignments, local variables or if statements.

So what do you think? Useful? Doable? Worth it?

P#120903 2022-11-18 14:22

[ :: Read More :: ]

Probably a silly question, but I can't seem to work it out.

I have a slow function - it takes more than 1/30 of second to execute but needs to be done in the _draw() function. This makes my code slow and unresponsive and it's clearly not the way to do it.

So what I want to do is the following:

  • run my slow function once in the _init() (drawing a bunch of stuff to the screen)
  • capture the screen to extended memory
  • in my _draw() function do repeated copies back to the screen

This works ... BUT ... when I run my slow function it shows on the screen (briefly, during _init() ). Probably because it overruns the 1/30 second _draw update time. I would rather suspend draw updates during _init().

So is there a way to suspend screen refreshing for a bit and then switch it back on?

P#106736 2022-02-11 22:39 ( Edited 2022-02-18 16:19)

[ :: Read More :: ]

The code below creates a random "word" that's 2 to 12 characters long. Some results are better than others and absolutely no guarantee it won't generate rude words (in whatever language you'd like to be offended in). This snippet might come in handy somewhere and can very easily be tweaked.
Great for all your randomly generated NPC's, Legendary Weapons, Chthonic demons and faraway planet systems.

Very loosely based on Jabbalaci's Python version (https://github.com/jabbalaci/Elite) of Ian Bell's txtelite.c script for Elite.

str_namefrags=split("AB,OU,AA,IT,ION,YL,IF,ON,ETO,AE,ERA,US,EON,ES,AR,UME,IN,AH,ER,AL,UR,AV,ET,IUS,IE,UJ,EY,YS,ON","SE,NYA,HY,SU,LO,NU,GA,LON,TH,NA,HO,LE,XA,DIA,GE,ZA,VAN,CE,HE,BI,SO,MA,DI,VE,RE,SEN,RA,TE,HI,NY,WEN,DO,QU,KI")

function random_word()
  -- creates a random word
  -- words are anything from 2 to 12 characters long

  -- _len = length of the word in fragments
  local _len,_res,_hyphen,_c,_frag = 2+flr(rnd(3)),"",0,0,""

  for z=1,_len do

    _frag=str_namefrags[1+flr(rnd(#str_namefrags))]

    -- generate a random chance to do special stuff with the chosen fragment
    _c=flr(rnd(16))

    if _c==0 then
      -- switch the first and last letter
      _res=_res..sub(_frag,-1,-1)..sub(_frag,1,1)

    elseif (_c==1 and _res!="" and z<_len and _hyphen==0) then
      -- if the string already contains some text, we're not at the
      -- last fragment and we've not done
      -- this before: add either a space, hyphen or apostrophe.
      local _mark = 1+flr(rnd(3))
      _res=_res..sub(" '-",_mark,_mark)
      _hyphen=1

    elseif _c==2 then
      -- use only the first letter of the chosen fragment
      _res=_res..sub(_frag,1,1)

    elseif _c==3 then
      -- use only the last letter of the chosen fragment
      _res=_res..sub(_frag,-1,-1)

    else 
      -- in all other cases: simply add the chosen fragment
      _res=_res.._frag

    end
  end

  if (#_res<5 and flr(rnd(70))==0) then
    -- if the result is short and once in 70 times:
    -- add the result to itself with a space in between
    _res=_res.." ".._res   

  end

  return _res

end

A sample of the "words" it will generate:
ETORAU
VEAUSSEN
NQU
IFVANERA
DIAIIUS
GESEUJAH
VANONA
TEUJAVUJ
CELOOUVE
INNUE
NYADIA
RAUSY
UJSU
REZA
SYIE
ONEY
ETUSEYR
IUSYL
ITSENHEDIA
OUQREZA
DOARAH
ELE
DI'AA
AVHISU
TIURQU
US-DIUS
LENUITWEN
AEOUTHA
UME UME
KIETNYA
EEAASU
REUMEEY
DIL
ITARNYALO
CEIFU
HE-ALSU
ABHIAA
AVALAAV
TELONHY

P#105580 2022-01-24 22:07 ( Edited 2022-01-25 09:56)

[ :: Read More :: ]

Tiny function that takes a number (float or int) and returns a string with (maximum) two decimals. For instance:

10 > "10"
20.03 > "20.03"
17.46134 > "17.46"

So not mathematically accurate, but useful under some circumstances...

function twodecimal(_n)
  -- will accept a number (float or int)
  -- turn it into a string and return the string with two decimals

  local _v=split(tostring(_n),".")
  if #_v==1 then
    return _v[1]
  else
    return (_v[1].."."..sub(_v[2],1,2))
  end

end
P#105105 2022-01-14 21:48