Author Topic: More lua functions  (Read 941 times)

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
More lua functions
« on: 30 March 2012, 07:19:23 »
The follow are some new lua functions we commited in svn:

Neutral units can be created by setting their team # to 9 (makes the system think they are an observer)

-- this function can be check to see if the game is over
bool isGameOver()
-- this function creates a timer that will only trigger the timer event after triggerSecondsElapsed seconds then remove the timer
int startEfficientTimerEvent(int triggerSecondsElapsed)
-- this function can be safely used for predictable random number seeding
setRandomGenInit(int seed);
-- this function generates a predictable random number
int getRandomGen(int minVal, int maxVal);
-- this function returns the world frame count (maybe good for seeding random number generation)
int getWorldFrameCount();

-- this function returns a list of units for a faction that match one or both commandtype and field criteria
-- if commandtype and field are empty it gets all units in a lua array for the faction
-- if commandtype if empty and field is set (0 = land 1 = air) then it finds units currently in that field
-- if commandtype is set but field is empty it gets all units that have that command type
-- if commandtype is set and field is set it gets all units that have that command type in the given field (Attack air for example)
int [] getUnitsForFaction(int factionIndex,const string& commandTypeName, int field);
-- this function returns the field the specified unit is in
int getUnitCurrentField(int unitId)

-- this function is helpful for network scenarios and shows the message for players in the specified faction
void networkShowMessageForFaction(const string &text, const string &header,int factionIndex);
-- this function is helpful for network scenarios and shows the message for players on the specified team
void networkShowMessageForTeam(const string &text, const string &header,int teamIndex);

Heres an example scenario using some of these features:

Code: [Select]
<?xml version="1.0" standalone="yes" ?>
<?xml version="1.0" standalone="yes" ?>
<scenario>
<difficulty value="3"/>
<players>
<player control="human" faction="tech" team="1"/>
<player control="cpu-ultra" faction="norsemen" team="2"/>
<player control="cpu-ultra" faction="tech" team="9"/>
<player control="cpu-ultra" faction="persian" team="9"/>
</players>
        <fog-of-war value="false"/>
<map value="8vs8islands"/>
<tileset value="desert2"/>
<tech-tree value="megapack"/>
<default-resources value="false"/>
<default-units value="false"/>
<default-victory-conditions value="false"/>
<scripts>
                <global>
        -- global vars
                        timer_event_crazy_interval = 15
                        timer_event_enemy_getflag_interval = 15
                </global>
<startup>

--disable AI for neutral factions
disableAi(3)
                        disableAi(4)

--my units
                        myStart=getRandomGen(0,3)
createUnit('castle', 0, startLocation(myStart))
myCastle=lastCreatedUnit()
createUnit('cow', 0, startLocation(myStart))
createUnit('worker', 0, startLocation(myStart))
createUnit('swordman', 0, startLocation(myStart))
createUnit('battle_machine', 0, startLocation(myStart))
                        myBonsol=lastCreatedUnit()
createUnit('archer', 0, startLocation(myStart))

                        giveResource('gold', 0, 1000);
giveResource('wood', 0, 1300);
giveResource('stone', 0, 350);
giveResource('food', 0, 1000);

-- enemy units
                        enemyStart=getRandomGen(0,3)
createUnit('castle', 1, startLocation(enemyStart))
                        enemyCastle=lastCreatedUnit()
                        createUnit('cow', 1, startLocation(enemyStart))
                        for i=1, 5 do
                                createUnit('thrull', 1, startLocation(enemyStart))
                        end
                        for i=1, 5 do
                                createUnit('battleaxe', 1, startLocation(enemyStart))
                        end
                        createUnit('valkyrie', 1, startLocation(enemyStart))
                        myValkyrie=lastCreatedUnit()
                        createUnit('archer', 1, startLocation(enemyStart))
                       
giveResource('gold', 1, 1000);
giveResource('wood', 1, 1000);
                        giveResource('stone', 1, 1000); 
giveResource('food', 1, 1000);

                        -- neutral units
                        neutralStart=getRandomGen(0,3)
                        while neutralStart == myStart or neutralStart == enemyStart do
                                neutralStart=getRandomGen(0,3)
                        end

createUnit('castle', 2, startLocation(neutralStart))
neutralCastle1=lastCreatedUnit()
                        createUnit('palace', 3, startLocation(neutralStart))
                        neutralCastle2=lastCreatedUnit()

--objectives
objective='firstwave'
showMessage('Brief1', 'CaptureFlag')
setDisplayText('firstwave')

                        --print 'TEST A'
                        cell_event1 = registerCellTriggerEventForUnitToUnit(myBonsol,neutralCastle1)
                        cell_event2 = registerCellTriggerEventForUnitToUnit(myValkyrie,neutralCastle2)
                        timer_event2 = startEfficientTimerEvent(timer_event_enemy_getflag_interval)
                        --print 'TEST B'

                        --networkShowMessageForFaction('BriefNetworkFaction', 'CaptureFlag', 0)
                        --networkShowMessageForTeam('BriefNetworkTeam', 'CaptureFlag', 2)
</startup>

<unitCreated>
</unitCreated>

<unitDied>
                        --print 'TEST C'
                        if myValkyrie == lastDeadUnit() then
        showMessage('WinEnemyDead', 'Won')
                                setPlayerAsWinner(0);
                                endGame()
                        elseif myBonsol == lastDeadUnit() then
        showMessage('WinEnemy', 'Lose')
                                setPlayerAsWinner(1);
                                endGame()
                        end
</unitDied>

                <timerTriggerEvent>
                        if triggeredTimerEventId() == timer_event2 then
                                timer_event2 = nil
                                showMessage('Brief4', 'CaptureFlag')
                                moveToUnit(myValkyrie,neutralCastle2)
                        elseif triggeredTimerEventId() == timer_event_crazy then
                                if isGameOver() == true then
                                        timer_event_crazy = nil
                                else
                                        --givePositionCommand(myValkyrie,'attack',startLocation(myStart))
                                        attackers=getUnitsForFaction(1,'attack',getUnitCurrentField(myBonsol))
                                        for i=1, #attackers do
                                                giveAttackCommand(attackers[i],myBonsol)
                                                --print('Unit id: ' .. attackers[i] .. ' is attacking unit id: ' .. myBonsol)
                                        end
                                        timer_event_crazy = startEfficientTimerEvent(timer_event_crazy_interval)
                                end
                        end
                </timerTriggerEvent>

                <cellTriggerEvent>
                        --print 'TEST CellEvent'
                        if triggeredCellEventId() == cell_event1 then
                                --print 'TEST CellEvent1'

        showMessage('Brief2', 'CaptureFlag')
        setDisplayText('secondwave')
                                objective='secondwave'
                                unregisterCellTriggerEvent(cell_event1)
                                unregisterCellTriggerEvent(cell_event2)
                                cell_event1 = nil
                                cell_event2 = nil

                                cell_event3 = registerCellTriggerEventForUnitToUnit(myBonsol,myCastle)

                                for i=1, 5 do
                                        createUnit('flyingvalkyrie', 1, startLocation(enemyStart))
                                end

                                timer_event_crazy = startEfficientTimerEvent(timer_event_crazy_interval)
                        elseif triggeredCellEventId() == cell_event2 then
                                --print 'TEST CellEvent2'

        showMessage('Brief3', 'CaptureFlag')
        setDisplayText('secondwave2')
                                objective='secondwave2'
                                unregisterCellTriggerEvent(cell_event1)
                                unregisterCellTriggerEvent(cell_event2)
                                cell_event1 = nil
                                cell_event2 = nil

                                cell_event3 = registerCellTriggerEventForUnitToUnit(myValkyrie,enemyCastle)
                                moveToUnit(myValkyrie,enemyCastle)
                        elseif triggeredCellEventId() == cell_event3 then
                                --print 'TEST CellEvent3'

                                unregisterCellTriggerEvent(cell_event3)
                                cell_event3 = nil

                                if objective == 'secondwave' then
                showMessage('WinEnemyDead', 'Won')
                                        setPlayerAsWinner(0);
                                        endGame()
                                elseif objective == 'secondwave2' then
                showMessage('WinEnemy', 'Lose')
                                        setPlayerAsWinner(1);
                                        endGame()
                                end
                        end
                </cellTriggerEvent>
                <GameOver>
                        if cell_event1 ~= nil then
                                unregisterCellTriggerEvent(cell_event1)
                                cell_event1 = nil
                        end
                        if cell_event2 ~= nil then
                                unregisterCellTriggerEvent(cell_event2)
                                cell_event2 = nil
                        end
                        if cell_event3 ~= nil then
                                unregisterCellTriggerEvent(cell_event3)
                                cell_event3 = nil
                        end

                        if timer_event2 ~= nil then
                                stopTimerEvent(timer_event2)
                                timer_event2 = nil
                        end
                        if timer_event_crazy ~= nil then
                                stopTimerEvent(timer_event_crazy)
                                timer_event_crazy = nil
                        end
                </GameOver>
</scripts>
</scenario>
« Last Edit: 30 March 2012, 14:47:14 by softcoder »

MuwuM

  • Ornithopter
  • *****
  • Posts: 426
  • No Game without Move(ment)
    • View Profile
    • MuwuM - Lexicons
Re: More lua functions
« Reply #1 on: 1 April 2012, 23:35:41 »
Neutral units can be created by setting their team # to 9 (makes the system think they are an observer)

Tested it today. I love the neutral units  :thumbup:. Will test the other stuff later.

MuwuM

  • Ornithopter
  • *****
  • Posts: 426
  • No Game without Move(ment)
    • View Profile
    • MuwuM - Lexicons
Re: More lua functions
« Reply #2 on: 13 April 2012, 14:34:58 »
I'm using getUnitsForFaction(int factionIndex,const string& commandTypeName, int field), too. Maybe you should ask whether commandTypeName is NIL/NULL in addition to '', because 'empty' sounds more like NIL than ''. I needed some tries to notice, that I have to use ''.

 

anything