Author Topic: Some new LUA functions  (Read 1761 times)

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
Some new LUA functions
« on: 29 January 2011, 18:28:25 »
I added the following LUA functions to the svn version which will be released with 3.4:

destroyUnit(unitId)
morphToUnit(unitId,morphName, ignoreRequirements)
moveToUnit(unitId,destUnitId)
giveAttackStoppedCommand(unitId, valueName,ignoreRequirements)
playStaticSound(playSound)
playStreamingSound(playSound)
stopStreamingSound(playSound)
stopAllSound()

The commands taking ignoreRequirements expect 0 for try to execute the command with requirements and 1 to skip requirements checking.

Below is a sample scenario using some of these methods:

Code: [Select]
<?xml version="1.0" standalone="yes" ?>
<scenario>
<difficulty value="3"/>
<players>
<player control="human" faction="tech" team="1"/>
<player control="cpu" faction="tech" team="2"/>
<player control="cpu" faction="tech" team="2"/>
<player control="cpu" faction="tech" team="2"/>
</players>
<map value="in_the_forest"/>
<tileset value="forest"/>
<tech-tree value="megapack"/>
<default-resources value="false"/>
<default-units value="false"/>
<default-victory-conditions value="false"/>
<scripts>
<startup>
print( 'Test, START of Capture the flag!' )
--disable AI
disableAi(1)
disableAi(2)
disableAi(3)
disableConsume(0)

unitA = 'guard'
unitB = 'castle'
captureTheFlagSeconds = 90

DisplayFormattedText('You have %s seconds to move the %s into the %s!', captureTheFlagSeconds,unitA, unitB)

for i=0, 3 do
print( 'Is AI enabled for faction index: ' .. i .. ', ' .. tostring( getAiEnabled(i)) )
print( 'Is consume enabled for faction index: ' .. i .. ', ' .. tostring( getConsumeEnabled(i)) )
end

--allied units
--createUnit('farm', 0, startLocation(2))
createUnit(unitB, 1, startLocation(2))
enemyCastle= lastCreatedUnit()

castleUnit= lastCreatedUnit()
createUnit('worker', 1, startLocation(2))
enemyWorker= lastCreatedUnit()

for i=1, 5 do
createUnit('swordman', 1, startLocation(2))
end

giveResource('gold', 0, 1000);
giveResource('wood', 0, 1000);
giveResource('food', 0, 1000); 

giveResource('food', 1, 5); 

--player units
createUnit('battle_machine', 0, startLocation(1))
battle_machine= lastCreatedUnit()
createUnit(unitA, 0, startLocation(2))
guard= lastCreatedUnit()


moveToUnit(guard,enemyWorker)

createUnit('technician', 0, startLocation(1))
technician= lastCreatedUnit()

moveToUnit(technician,enemyCastle)

createUnit('battle_machine', 0, startLocation(1))
bm2= lastCreatedUnit()

for i=1, 3 do
createUnit('archer', 0, startLocation(2))
test = lastCreatedUnit()
giveAttackCommand(test, castleUnit)
end
for i=1, 10 do
createUnit('swordman', 0, startLocation(1))
givePositionCommand(lastCreatedUnit(), 'attack', startLocation(1))
end

cell_event1 = registerCellTriggerEventForUnitToUnit(guard,castleUnit)
print( 'cell_event1 = ' .. cell_event1 )
cell_event2 = registerCellTriggerEventForUnitToUnit(battle_machine,castleUnit)
print( 'cell_event2 = ' .. cell_event2 )

timer_event1 = startTimerEvent()
print( 'timer_event1 = ' .. timer_event1 )

setCameraPosition(unitPosition(guard))

</startup>

<unitCreated>
</unitCreated>

<unitDied>
enableConsume(0)
enableAi(1)
enableAi(2)
enableAi(3)

for i=0, 3 do
print( 'Is AI enabled for faction index: ' .. i .. ', ' .. tostring( getAiEnabled(i)) )
print( 'Is consume enabled for faction index: ' .. i .. ', ' .. tostring( getConsumeEnabled(i)) )
end

clearDisplayText()

print( 'About to morph unit: ' .. technician )

morphToUnit(technician,'build_air_ballista',1)
destroyUnit(battle_machine);

giveAttackStoppedCommand(bm2,'hold_position',1)
playStreamingSound('/home/softcoder/Code/megaglest/trunk/data/glest_game/techs/megapack/factions/romans_beta/music/romans.ogg')

print( 'Done morphing...' )
</unitDied>

<cellTriggerEvent>
--print('Cell Event ' .. triggeredCellEventId() )
--print( 'In cell event triggeredCellEventId = ' .. triggeredCellEventId() .. ' cell_event1 = ' .. cell_event1 .. ' cell_event2 = ' .. cell_event2 )
if triggeredCellEventId() == cell_event1 or triggeredCellEventId() == cell_event2 then
clearDisplayText()
DisplayFormattedText('You captured the FLAG!')
stopTimerEvent(timer_event1)
unregisterCellTriggerEvent(cell_event1)
unregisterCellTriggerEvent(cell_event2)
end
</cellTriggerEvent>

<timerTriggerEvent>
--print('Timer Event ' .. triggeredTimerEventId() )
--print( 'In timer event triggeredTimerEventId = ' .. triggeredTimerEventId() .. ' timer_event1 = ' .. timer_event1 )
if triggeredTimerEventId() == timer_event1 then
if timerEventSecondsElapsed(triggeredTimerEventId()) >= captureTheFlagSeconds then
print( 'In timer event triggeredTimerEventId = ' .. triggeredTimerEventId() .. ' timer_event1 = ' .. timer_event1 .. ' seconds elapsed = ' .. timerEventSecondsElapsed(triggeredTimerEventId()) )
clearDisplayText()
DisplayFormattedText('Your time ran out of time to capture the FLAG!')
stopTimerEvent(timer_event1)
unregisterCellTriggerEvent(cell_event1)
unregisterCellTriggerEvent(cell_event2)

                    endGame()
end
end
</timerTriggerEvent>
</scripts>
</scenario>
« Last Edit: 29 January 2011, 19:02:29 by softcoder »

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: Some new LUA functions
« Reply #1 on: 17 February 2011, 16:18:27 »
very good   ;D
now wee only need a command to add commands to queue  :|

by the way how to use the sound command  :scared:
« Last Edit: 17 February 2011, 16:30:19 by PT »
My first Tilseset: SPRING :) (included in Megaglest )

Secret Hint: To play online join the IRC #megaglest-lobby on freenode which is the lobby chat ingame. So you can chat with or wait for people in the lobby without running megaglest all the time.

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
Re: Some new LUA functions
« Reply #2 on: 18 February 2011, 17:47:42 »
The sound command is sown in the sample script:

playStreamingSound('/home/softcoder/Code/megaglest/trunk/data/glest_game/techs/megapack/factions/romans_beta/music/romans.ogg')

Thanks

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: Some new LUA functions
« Reply #3 on: 18 February 2011, 21:19:51 »
and why no .wav files?  ::)
My first Tilseset: SPRING :) (included in Megaglest )

Secret Hint: To play online join the IRC #megaglest-lobby on freenode which is the lobby chat ingame. So you can chat with or wait for people in the lobby without running megaglest all the time.

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
Re: Some new LUA functions
« Reply #4 on: 19 February 2011, 00:54:09 »
both wav and ogg should work. Ogg files would be better since they most often are smaller in size.

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: Some new LUA functions
« Reply #5 on: 3 March 2011, 12:54:12 »
is there a way to write the playStreamingSound('/home/softcoder/Code/megaglest/trunk/data/glest_game/techs/megapack/factions/romans_beta/music/romans.ogg')
without /home/softcoder/Code/megaglest/trunk/data/glest_game/ (shorter and  for all) ?  :|
My first Tilseset: SPRING :) (included in Megaglest )

Secret Hint: To play online join the IRC #megaglest-lobby on freenode which is the lobby chat ingame. So you can chat with or wait for people in the lobby without running megaglest all the time.

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
Re: Some new LUA functions
« Reply #6 on: 4 March 2011, 00:51:40 »
you could write (using relative paths):

Code: [Select]
playStreamingSound('techs/megapack/factions/romans_beta/music/romans.ogg')
For developers running from the build folder:

Code: [Select]
playStreamingSound('../data/glest_game/techs/megapack/factions/romans_beta/music/romans.ogg')

will

  • Golem
  • ******
  • Posts: 783
    • View Profile
Re: Some new LUA functions
« Reply #7 on: 4 March 2011, 06:11:10 »
surely it should always be relative to the data directory, and should be jailed there?

although perhaps that is something easiest with physfs...

Otherwise there are security implications, and whilst nobody else might be worried about running Eliminator's scenarios, surely generally its a bad thing to slip in?

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: Some new LUA functions
« Reply #8 on: 10 March 2011, 11:05:10 »
i found a bug in timer_event:
if you write FastSpeedLoops=20 in the glest.ini and set game speed to fast the counter count like FastSpeedLoops=2  :|
My first Tilseset: SPRING :) (included in Megaglest )

Secret Hint: To play online join the IRC #megaglest-lobby on freenode which is the lobby chat ingame. So you can chat with or wait for people in the lobby without running megaglest all the time.

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
Re: Some new LUA functions
« Reply #9 on: 11 March 2011, 07:08:32 »
Not sure what your mean? Please explain the bug more?

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
Re: Some new LUA functions
« Reply #10 on: 28 May 2013, 02:55:19 »
Updated sound and video functions so they can use some new variables:

added ability to reference sound and videos in lua using variables: {TECHTREEPATH}, {SCENARIOPATH}, {TUTORIALPATH} example:
playStreamingSound('{SCENARIOPATH}/music/myambient.ogg')

 

anything