Author Topic: Numstrat - Stumbling into Lua  (Read 25949 times)

0 Members and 1 Guest are viewing this topic.

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Numstrat - Stumbling into Lua
« Reply #15 on: April 11, 2012, 07:34:12 am »
I think he's talking about your own work. I know you've been doing some edits, optimizations, and remakes, but you could try making your own project.  ;)

As a suggestion, you could try making a Lua sudoku. It'd be easy enough and it'd be pretty nice.  :D

Well kind of cyano. I was talking about any project anywhere that someone started and even though it is from someone else's project I'd still count it. Very nice..

And good idea for the Lua Sudoku.
I'm possibly leaning towards making some card games in Lua including Texas Hold 'em Poker (porting my Basic version), Hearts Solitare etc.
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Numstrat - Stumbling into Lua
« Reply #16 on: April 11, 2012, 04:08:36 pm »
For card games you might learn a lot from chockosta. He made the freecell clone, which is really nice and runs perfect. dragable cards, checking for values and freee places etc.

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Numstrat - Stumbling into Lua
« Reply #17 on: April 11, 2012, 05:19:32 pm »
Levak made an excellent Lua sudoku btw (quite advanced/complex Nspire Lua though) available here :
http://levak.free.fr/ftp/nspire/Sudoku/
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Numstrat - Stumbling into Lua
« Reply #18 on: April 12, 2012, 04:04:24 am »
Where do all these Lua programs pop out of?  ??? There always seems to be unreleased programs hiding everywhere...

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Numstrat - Stumbling into Lua
« Reply #19 on: April 14, 2012, 09:47:05 pm »
For card games you might learn a lot from chockosta. He made the freecell clone, which is really nice and runs perfect. dragable cards, checking for values and freee places etc.

Yeah I've learnt a bit from him already. He has lots of nice Lua programs
Cyano too :D
And it does take an effort to release programs, so there would be many development/unreleased programs lying around.

Anyway the code by someone:
Spoiler For Code by someone:
I redefined some variables, I think is more legible this way:

Code: [Select]
--[[Things to do:
Declare variables first within a function
Make things more fun and complex
Points System:
OLD: Atk -{1,2,3} +{4,5} Def +{1,2,3} -{4,5}
NEW: Atk -{4,5} +{4,5} Def +{1,2,3} -{1,2,3}
Any better ways? Maybe make Atk more risky, so it's less about luck]]--

chance = { [-1] = "",
            [0] = "unsuccessful . -",
            [1] = "successful . +" }

action = { nothing="", attack="Attack", defense="Defense"}

function initialize_variables()
    v = 0
    t = 0
    r1 = 0
    r2 = -1
    state = action.nothing
end

--function on.create()
initialize_variables()
--end

function on.paint(gc)
    gc:setFont("sansserif","r",11)
    gc:setColorRGB(0,0,0)
   
    gc:drawString("[A]tk or [D]ef?    [R]eset",10,10,"top")
    gc:drawString(v,10,30,"top")

    if state == action.attack then
        gc:drawString("Attack was "  .. chance[r2] .. r1, 10, 50, "top")
    elseif state == action.defense then
        gc:drawString("Defense was " .. chance[r2] .. r1, 10, 50, "top")
    end

    gc:drawString("Turn " .. t,200,10,"top")
    if v~=0 or t~=0 then
        gc:drawString("Average " .. round(v/t,2), 200, 30, "top")
    end
    gc:setFont("sansserif","r",8)
    gc:drawString("Numstrat - Jason Ho",10,200,"top")
end

function on.charIn(ch)
    if ch=="a" then
        state = action.attack
        r1=math.random(4,5)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        t=t+1
    end

    elseif ch=="d" then
        state = action.defense
        r1=math.random(1,3)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        state=action.defense
        t=t+1
    end

    elseif ch=="r" then
        initialize_variables()
    end

    platform.window:invalidate()
end

function round(value, digits)
    return string.format("%." .. digits .. "f", value)
end


Is a bit advanced for me. So I'll create things my own way and see if I can implement those things in as I go.
« Last Edit: April 14, 2012, 09:48:24 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline someone

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: Numstrat - Stumbling into Lua
« Reply #20 on: April 16, 2012, 11:16:18 am »
Well, I agree that the first part was a little abstract, so I removed it.

If you have any question on this code, be free to ask.

Code: [Select]
--[[Things to do:
Declare variables first within a function
Make things more fun and complex
Points System:
OLD: Atk -{1,2,3} +{4,5} Def +{1,2,3} -{4,5}
NEW: Atk -{4,5} +{4,5} Def +{1,2,3} -{1,2,3}
Any better ways? Maybe make Atk more risky, so it's less about luck]]--

-- Call this function whenever you want to set the variables to their original values
function initialize_variables()
    v = 0
    t = 0
    r1 = 0
    r2 = -1
    state = ""
end

--function on.create()
initialize_variables()
--end

function on.paint(gc)
    gc:setFont("sansserif","r",11)
    gc:setColorRGB(0,0,0)
   
    gc:drawString("[A]tk or [D]ef?    [R]eset",10,10,"top")
    gc:drawString(v,10,30,"top")

    if state == "attack" then
        if r2 == 1 then
            gc:drawString("Attack was successful . + "  .. r1, 10, 50, "top")
        else    --if r2 == 0 then
            gc:drawString("Attack was unsuccessful . -" .. r1, 10, 50, "top")
        end
    elseif state == "defense" then
        if r2 == 1 then
            gc:drawString("Defense was successful . + "  .. r1, 10, 50, "top")
        else    --if r2 == 0 then
            gc:drawString("Defense was unsuccessful . -" .. r1, 10, 50, "top")
        end
    end

    gc:drawString("Turn " .. t,200,10,"top")
    if v~=0 or t~=0 then
        gc:drawString("Average " .. round(v/t,2), 200, 30, "top")
    end
    gc:setFont("sansserif","r",8)
    gc:drawString("Numstrat - Jason Ho",10,200,"top")
end

function on.charIn(ch)
    if ch=="a" then
        state = "attack"
        r1=math.random(4,5)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        t=t+1

    elseif ch=="d" then
        state = "defense"
        r1=math.random(1,3)
        r2=math.random(0,1)
        if r2==1 then
            v=v+r1
        else    --if r2==0 then
            v=v-r1
        end
        t=t+1

    elseif ch=="r" then
        initialize_variables()
    end

    platform.window:invalidate()
end

--This function rounds the value passed depending on the number of digits needed
function round(value, digits)
    return string.format("%." .. digits .. "f", value)
end

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Numstrat - Stumbling into Lua
« Reply #21 on: April 19, 2012, 01:45:27 am »
I noticed that the number of lines has increased a bit from 67 to 84. But I guess your modification does allow for expansion if I wish.
Thanks for all your help someone.
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Numstrat - Stumbling into Lua
« Reply #22 on: September 10, 2012, 01:49:56 am »
I'm back. I looked back at a copy I had a few days ago of the code that someone put in above and I get what it's saying.
I mostly get the first version here with the use of tables (which I'll have to familiarise myself with)
http://ourl.ca/15758/295401
« Last Edit: September 10, 2012, 01:50:01 am by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Twilight Sparkle

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 5
  • Rating: +2/-0
  • Programming is Magic
    • View Profile
Re: Numstrat - Stumbling into Lua
« Reply #23 on: September 18, 2012, 11:34:33 am »
Hi, just wanted to note that you can change
Code: [Select]
    if state == "attack" then
        if r2 == 1 then
            gc:drawString("Attack was successful . + "  .. r1, 10, 50, "top")
        else    --if r2 == 0 then
            gc:drawString("Attack was unsuccessful . -" .. r1, 10, 50, "top")
        end
    elseif state == "defense" then
        if r2 == 1 then
            gc:drawString("Defense was successful . + "  .. r1, 10, 50, "top")
        else    --if r2 == 0 then
            gc:drawString("Defense was unsuccessful . -" .. r1, 10, 50, "top")
        end
    end

to

Code: [Select]
if state ~= "" then
gc:drawString((state=="attack" and "Attack" or "Defense") .. " was " .. (r2==0 and "unsuccessful. -" or "successful. +") .. r1, 10, 50, "top")
end

Makes it a bit smaler :D
« Last Edit: September 18, 2012, 11:35:36 am by Twilight Sparkle »

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Numstrat - Stumbling into Lua
« Reply #24 on: September 18, 2012, 11:42:54 am »
Hmm, optimization's like that are good indeed.
I would also recommend you to *always* use at least a simple screen manager, it makes life easier for the developer, and it's easier for him to make the game look better too :)
« Last Edit: September 18, 2012, 11:43:17 am by Jim Bauwens »

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Numstrat - Stumbling into Lua
« Reply #25 on: September 22, 2012, 09:29:34 pm »
Hmm I like that optimisation. I am still working on Lua programming, just a bit slowly.
And Jim Bauwens, what do you mean by a screen manager?
« Last Edit: September 22, 2012, 09:29:55 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Twilight Sparkle

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 5
  • Rating: +2/-0
  • Programming is Magic
    • View Profile
Re: Numstrat - Stumbling into Lua
« Reply #26 on: September 23, 2012, 07:08:17 am »
A screen manager is something that allows you to devide your Lua applications in several pieces.
Basically, you will have a paint handler for every scene in your program, without needing to put all your stuff in on.paint.

Here is a simple screen manager, so you can understand how it works a bit:
Code: [Select]

------ Screen Manager -------

-- In this table we will contain all 'screen' objects.
Screens = {}

-- This function is used to push a screen into the above table (Screens)
function pushScreen(scrn)
    table.insert(Screens, scrn)
    platform.window:invalidate()
end

-- This function is used to remove the last item in the 'Screens' table
function pullScreen()
    table.remove(Screens)
    platform.window:invalidate()
end

-- Return the last item of 'Screens', the current screen
function currentScreen()
    return Screens[#Screens]
end

-- This function will loop through all screens in the 'Screens' table and draw them
function paintScreens(gc)
    for _, screen in ipairs(Screens) do
        screen:paint(gc)
    end
end

-- Our screen object class
Screen = class()

function Screen:arrowKey() end
function Screen:paint()        end
function Screen:charIn()      end
function Screen:enterKey()  end

-- Link the Screen manager to 'on'

function on.paint(gc)              paintScreens(gc)                           end
function on.arrowKey(arrow) currentScreen():arrowKey(arrow) end
function on.charIn(ch)            currentScreen():charIn(ch)           end
function on.enterKey()            currentScreen():enterKey()          end

--- User code ----
popup = Screen()

function popup:paint(gc)
    gc:setColorRGB(255,255,255)
    gc:fillRect(50,50,200,100)
    gc:setColorRGB(0,0,0)
    gc:drawRect(50,50,200,100)
    gc:drawString("Hey!", 52, 50, "top")
    gc:drawString("Press enter to hide this popup", 52, 80, "top")
end

function popup:enterKey()
    pullScreen()
end


main = Screen()

function main:paint(gc)
    gc:drawString("Hello World! Press enter :D", 10, 10, "top")
end

function main:enterKey()
    pushScreen(popup)
end

pushScreen(main)

Adriweb and Jim Bauwens created a more advanced screen manager together with a nice widget API, you can find it on the github page for EEPro I think.
« Last Edit: September 23, 2012, 02:42:09 pm by Twilight Sparkle »

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Numstrat - Stumbling into Lua
« Reply #27 on: September 23, 2012, 08:04:03 am »
Indeed, that's ETK. Jim created the core of it.
You can find the screen + widgets etc. here 
https://github.com/adriweb/EEPro-for-Nspire/blob/master/Global%20Libraries

But that's far more advanced and probably too complex for simpler usecases than FormulaPro (eepro).
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Numstrat - Stumbling into Lua
« Reply #28 on: September 23, 2012, 08:53:54 am »
Jonius, that code that Twilight gave you should point you in the right direction :) (Thanks Twilight!)
You can also find some more info here: http://www.inspired-lua.org/2012/02/how-to-create-a-screen-manager/ .

Adriweb: isn't Tower defence more simple ? I think most apps will be more simple than FormulaPro :P

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Numstrat - Stumbling into Lua
« Reply #29 on: September 23, 2012, 09:18:03 am »
Well, you can "simply" take this code until line 1440 : https://github.com/adriweb/LuaTowerDefense/blob/master/LuaTowerDefense.lua
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation