Author Topic: More Lua...  (Read 1198 times)

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
More Lua...
« on: 29 January 2011, 23:21:59 »
We can't let MG beat us in Lua, can we?

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>
I do think we should be able to perform any command with Lua, so these would be very handy to support, and I do particularly like the ability to change the background music and play sound effects (but please make sure you can use relative paths. Softcoder's example is using an absolute path, which is naughty naughty naughty. And while on that topic, I'd like to request the ability to display a still image in the same manner as how the background images for the loading screens are displayed. In addition, let us set the length of time to display that image, and toggle whether it can be skipped by pressing any button.
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

silnarm

  • Local Moderator
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: More Lua...
« Reply #1 on: 31 January 2011, 06:13:28 »
destroyUnit(unitId)
We already have a command for this, and it's more generic than this one, allowing you to 'kill' a unit, or just plain 'remove' it.

Quote
morphToUnit(unitId,morphName, ignoreRequirements)
This is a production-command, and is already possible.

Quote
moveToUnit(unitId,destUnitId)
This is possible with the original Glest Lua interface, why softcoder felt the need to make it its own command I don't know, but it is completely unnecessary, and will not feature in GAE.

Quote
giveAttackStoppedCommand(unitId, valueName,ignoreRequirements)
Already possible.

Quote
playStaticSound(playSound)
playStreamingSound(playSound)
stopStreamingSound(playSound)
stopAllSound()

These might be nice, but unless we require you to 'register' all sounds at the start of the script, this would need to be done in a separate thread, so it probably wont happen any time soon.

I do think we should be able to perform any command with Lua...

http://sourceforge.net/apps/trac/glestae/wiki/LuaReference
You can, some of the newer commands you can't yet, but everything else has been possible for some time now.

Glest Advanced Engine - Code Monkey

Timeline | Downloads

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: More Lua...
« Reply #2 on: 4 March 2011, 13:20:01 »
We can't let MG beat us in Lua, can we?

You can  :P
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.

ultifd

  • Airship
  • ********
  • Posts: 4,443
  • The Glest Video Guy :) The one and only. :P
    • View Profile
    • My Youtube Channel
Re: More Lua...
« Reply #3 on: 4 March 2011, 22:53:57 »
Well it doesn't really matter anyways now... Outcome: Lua FTW!

ChupaReaper

  • Guest
Re: More Lua...
« Reply #4 on: 11 March 2011, 00:11:58 »
Though I wont be making scenarios much any time soon, to ability to play sounds and change background music (and playlists if they're supported at the time) would be very useful especially sounds for voice acting which I intend to have when I get around to making campaigns.