Author Topic: [READY TO TEST in 3.3.7-dev] A few new LUA commands  (Read 2988 times)

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
[READY TO TEST in 3.3.7-dev] A few new LUA commands
« on: 28 August 2010, 08:09:53 »
While fixing a bug related to AI players not losing their HP when running out of food, Tiger asked for some new commands so the latest version in SVN has the following LUA support:

void giveAttackCommand(attackerunit, unittoattack)
void disableAi(factionindex)
void enableAi(factionindex)
void disableHunger(factionindex)
void enableHunger(factionindex)
bool getAiEnabled(factionindex)
bool getHungerEnabled(factionindex)
void startPerformanceTimer()
void endPerformanceTimer()
Vec2i getPerformanceTimerResults()   (returns a 2D int array, item #1 is avg updateFps, #2 is avg RenderFps)
void DisplayFormattedText(format, ...)   (prints a message using printf style formatting and NOT using language files)
int registerCellTriggerEventForUnitToUnit(int sourceUnitId, int destUnitId)  
int registerCellTriggerEventForUnitToLocation(int sourceUnitId, Vec2i pos)
int registerCellTriggerEventForFactionToUnit(int sourceFactionId, int destUnitId)
int registerCellTriggerEventForFactionToLocation(int sourceFactionId, Vec2i pos)
int getCellTriggerEventCount(int eventId)
void unregisterCellTriggerEvent(int eventId)
int startTimerEvent()
int stopTimerEvent(int eventId)
int getTimerEventSecondsElapsed(int eventId)
int getCellTriggeredEventId()
int getTimerTriggeredEventId()

events:
gameOver
timerTriggerEvent
cellTriggerEvent

This should make for some very interesting scenarios :)

EDIT (here's an example):

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="1"/>
<player control="cpu" faction="tech" team="1"/>
<player control="cpu" faction="tech" team="1"/>
</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="true"/>
<scripts>
<startup>
--disable AI
disableAi(1)
disableAi(2)
disableAi(3)
disableHunger(0)

age = 37
you = 17
DisplayFormattedText('I am %s years old. You are %s', age, you)

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

startPerformanceTimer()

--allied units
--createUnit('farm', 0, startLocation(2))
createUnit('castle', 1, startLocation(2))
castleUnit= lastCreatedUnit()
createUnit('worker', 1, startLocation(2))

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(2))
battle_machine= lastCreatedUnit()
createUnit('guard', 0, startLocation(2))
guard= lastCreatedUnit()

for i=1, 3 do
createUnit('archer', 0, startLocation(2))

giveAttackCommand(lastCreatedUnit(), 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 )
timer_event1 = startTimerEvent()
print( 'timer_event1 = ' .. timer_event1 )

</startup>

<unitCreated>
</unitCreated>

<unitDied>
enableHunger(0)
enableAi(1)

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

endPerformanceTimer()
clearDisplayText()
setDisplayText(string.format('Performance, updateFPS: %d, RenderFPS: %d', getPerformanceTimerResults()[1],getPerformanceTimerResults()[2]))

</unitDied>

<cellTriggerEvent>
--print('Cell Event ' .. triggeredCellEventId() )
print( 'In cell event triggeredCellEventId = ' .. triggeredCellEventId() .. ' cell_event1 = ' .. cell_event1 )
if triggeredCellEventId() == cell_event1 then
clearDisplayText()
setDisplayText('You captured the FLAG!')
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()) >= 7 then
print( 'In timer event triggeredTimerEventId = ' .. triggeredTimerEventId() .. ' timer_event1 = ' .. timer_event1 .. ' seconds elapsed = ' .. timerEventSecondsElapsed(triggeredTimerEventId()) )
clearDisplayText()
setDisplayText('Timer 1 triggered!')
stopTimerEvent(timer_event1)
end
end
</timerTriggerEvent>
</scripts>
</scenario>
« Last Edit: 30 August 2010, 14:20:04 by titi »

titi

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 4,240
    • View Profile
    • http://www.titusgames.de
Re: [DONE in 3.3.7-dev} A few new LUA commands
« Reply #1 on: 29 August 2010, 21:52:06 »
Just for info, if you add new lua commands try to look here, so we stay compatible if possible:
http://glestguide.co.cc/lau.php

or newer  :P:
http://glestguide.co.cc/lua.php

Edit by Omega: Updated links
« Last Edit: 2 December 2010, 23:28:44 by Omega »
Try Megaglest! Improved Engine / New factions / New tilesets / New maps / New scenarios

silnarm

  • GAE Team
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #2 on: 1 September 2010, 02:13:00 »
Yeah, virtually all of this is possible in GAE, using completely different commands of course...

Maybe we should just add an engineVersion() function to both, that way modders can at least check what engine the user is running, and give them a message if its wrong for their scenario... then we can just continue on our merry way, providing two different and completely incompatible Lua interfaces, thats no doubt what the modders want us to do...  :look:
Glest Advanced Engine - Code Monkey

Timeline | Downloads

titi

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 4,240
    • View Profile
    • http://www.titusgames.de
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #3 on: 1 September 2010, 14:06:09 »
enableHunger .... maybe enableConsume is more general?
Try Megaglest! Improved Engine / New factions / New tilesets / New maps / New scenarios

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #4 on: 3 October 2010, 13:21:16 »
Hm...

How to use the registerCellTriggerEventForUnit/Location/FactionToUnit/Location/Faction commands?  :look:

And your example do not work in 3.3.7 beta 3.
if i start,i have no units.
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: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #5 on: 4 October 2010, 05:34:00 »
Sorry that I did not get back to you sooner, i was busy. I'll try to take a look at these lua functions using my tests and see what i find and report back.

UPDATE:

Ok, Titi had changed the method for toggling hunger to use a different name. It seems like using invalid (unknown) functions in the LUA script causes the script to STOP processing once it hits a function that is unknown. Anyways the function word 'hunger' was changed to consume. Here is the updated scenario:

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="1"/>
<player control="cpu" faction="tech" team="1"/>
<player control="cpu" faction="tech" team="1"/>
</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="true"/>
<scripts>
<startup>
print( 'Test, START of Storming Test' )
--disable AI
disableAi(1)
disableAi(2)
disableAi(3)
disableConsume(0)

print( 'Test - A1' )

unitA = 'guard'
unitB = 'castle'
captureTheFlagSeconds = 15

print( 'Test - A2' )

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

print( 'Test - B' )

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

print( 'Test - C' )

startPerformanceTimer()

print( 'Test - D' )

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

print( 'Test - E' )

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

print( 'Test - F' )

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

giveResource('food', 1, 5); 

--player units
createUnit('battle_machine', 0, startLocation(2))
battle_machine= lastCreatedUnit()
createUnit(unitA, 0, startLocation(2))
guard= 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 )

</startup>

<unitCreated>
</unitCreated>

<unitDied>
enableHunger(0)
enableAi(1)

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

endPerformanceTimer()
clearDisplayText()
setDisplayText(string.format('Performance, updateFPS: %d, RenderFPS: %d', getPerformanceTimerResults()[1],getPerformanceTimerResults()[2]))

</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)
end
end
</timerTriggerEvent>
</scripts>
</scenario>

« Last Edit: 4 October 2010, 16:02:10 by softcoder »

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #6 on: 9 October 2010, 18:12:57 »
Thanks

( :O I start and lose... :O )
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: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #7 on: 9 October 2010, 18:28:57 »
FYI, starting in the next version (after 3.3.7) we found out how to capture invalid LUA function names now! This will make it MUCH easier to write lua scripts.

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #8 on: 17 October 2010, 16:37:23 »
why is there no registerCellTriggerEventForFactionToFaction command  :o
If you find the enemy and i want to show a Message but then i see that there is no registerCellTriggerEventForFactionToFaction command
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: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #9 on: 17 October 2010, 17:05:12 »
Yes not sure exactly what you want with such an event (please explain in more details). I tried t oadd the new events and methods on our wiki (with examples) at:

https://docs.megaglest.org/MG/Scripted_Scenarios
« Last Edit: 18 June 2016, 16:43:41 by filux »

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #10 on: 14 November 2010, 07:52:13 »
i mean that:

if you "touch" an enemy i want to show a message.
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.

titi_son

  • Draco Rider
  • *****
  • Posts: 283
  • titi_son
    • View Profile
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #11 on: 14 November 2010, 10:48:39 »
What is wrong with this scenario?
If my man touch the sphinx,timer_event1 and timer_event2 count up.And they only count up if i move my man.
What is wrong?  :look:
Code: [Select]
<?xml version="1.0" standalone="yes" ?>
<scenario>
<difficulty value="3"/>
<players>
<player control="human" faction="egypt" team="1"/>
<player control="cpu" faction="egypt" team="1"/>
<player control="cpu" faction="flag" team="1"/>
</players>
<map value="../scenarios/flag/flag"/>
<tileset value="forest"/>
<tech-tree value="../scenarios/flag/scenariosmod"/>
<default-resources value="true"/>
<default-units value="true"/>
<default-victory-conditions value="false"/>
<scripts>
<startup>

--objectives
objective='no_one'
showMessage('Brief1', 'Storming')
DisplayFormattedText('Enemy: %s sec You: %s sec.If a Player stand 1200sec near the flag,he win', timer_event1, timer_event2)
timer_event1 = 0
timer_event2 = 0

createUnit('flag', 2, startLocation(2))
flagUnit= lastCreatedUnit()

cell_event1 = registerCellTriggerEventForFactionToUnit(0,flagUnit)
cell_event2 = registerCellTriggerEventForFactionToUnit(1,flagUnit)

</startup>
<cellTriggerEvent>
if triggeredCellEventId() == cell_event1 and objective=='no_one' or objective=='playerII' then
timer_event1 = startTimerEvent()
stopTimerEvent(timer_event2)
objective='playerI'
end
if triggeredCellEventId() == cell_event2 and objective=='no_one' or objective=='playerI' then
timer_event2 = startTimerEvent()
stopTimerEvent(timer_event1)
objective='playerII'
end
DisplayFormattedText('Enemy: %s sec You: %s sec.If a Player stand 1200sec near the flag,he win', timer_event1, timer_event2)
</cellTriggerEvent>
</scripts>
</scenario>

PS: You can win yet. And you can't test it because i have made a extra faction and a extra map.
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.

Coldfusionstorm

  • Golem
  • ******
  • Posts: 868
    • View Profile
Re: [DONE in 3.3.7-dev} A few new LUA commands
« Reply #12 on: 14 November 2010, 14:39:21 »
Just for info, if you add new lua commands try to look here, so we stay compatible if possible:
http://glest.110mb.com/lau.php

You know what i abolutely HATE about this adress:http://glest.110mb.com/lau.php

LAU.php

it is not LAU it have never BEEN LAU and its never going to BE LAU.

it is LUA.

Now, lets call a shovel for a shovel and a hammer for a hammer, Mkay?.
WiP Game developer.
I do danish translations.
"i break stuff"

titi

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 4,240
    • View Profile
    • http://www.titusgames.de
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #13 on: 14 November 2010, 23:11:00 »
?? it was called like this! I just putted the link here
Try Megaglest! Improved Engine / New factions / New tilesets / New maps / New scenarios

John.d.h

  • Moderator
  • Airship
  • ********
  • Posts: 3,757
  • I have to go now. My planet needs me.
    • View Profile
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #14 on: 14 November 2010, 23:26:54 »
That link is dead anyway.

http://glestguide.co.cc/lau.php

(Blame Omega for not being able to spell. :P)

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #15 on: 30 November 2010, 18:48:32 »
[LATE REPLY]

Actually, that's the old link. See,
http://glestguide.co.cc/lua.php
works too...

I keep the old link up because (1) I forgot to update the main page's link and (2) for compatibility. It's still the same page mirrored between the two... Oddly enough, that's just the filename, and all records of the misspelling in the actual page were fixed shortly after release. But since filename has the problems of that if I rename the page, all links to it are broken, and I may not be able to change some of those links (ie: external sites). Recently, I mirrored the page on the new page, as linked above.
Edit the MegaGlest wiki: http://docs.megaglest.org/

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

tomreyn

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 2,764
    • View Profile
    • MegaGlest - the free and open source cross platform 3D real-time strategy game
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #16 on: 1 December 2010, 07:58:38 »
Would it be possible (speaking of both licensing terms and of where web links are pointed to) to move this article to the glest wiki on wikia.com? Information on anything *Glest* is already much too shattered now, it would be good to keep it somewhat centralized where that's possible...
« Last Edit: 2 December 2010, 16:56:41 by tomreyn »
atibox: Ryzen 1800X (8 cores @3.6GHz), 32 GB RAM, MSI Radeon RX 580 Gaming X 8G, PCI subsystem ID [1462:3417], (Radeon RX 580 chipset, POLARIS10) @3440x1440; latest stable Ubuntu release, (open source) radeon (amdgpu) / mesa video driver
atibox (old): Core2Quad Q9400 (4 cores @2.66GHz), 8 GB RAM, XFX HD-467X-DDF2, PCI subsystem ID [1682:2931], (Radeon HD 4670, RV730 XT) @1680x1050; latest stable Ubuntu release, (open source) radeon / mesa video driver
notebook: HP envy13d020ng
internet access: VDSL2+

· · · How YOU can contribute to MG · Latest development snapshot · How to build yourself · Megapack techtree · Currently hosted MG games · · ·

Coldfusionstorm

  • Golem
  • ******
  • Posts: 868
    • View Profile
Re: [READY TO TEST in 3.3.7-dev] A few new LUA commands
« Reply #17 on: 2 December 2010, 10:01:48 »
?? it was called like this! I just putted the link here


i wanst blaming you, and good to know the reason for it omega :).
WiP Game developer.
I do danish translations.
"i break stuff"