Merge lp:~armagetronad-ct/armagetronad/0.4-delaycmd into lp:~armagetronad-dev/armagetronad/0.4-armagetronad-work

Proposed by Voodoo
Status: Needs review
Proposed branch: lp:~armagetronad-ct/armagetronad/0.4-delaycmd
Merge into: lp:~armagetronad-dev/armagetronad/0.4-armagetronad-work
Diff against target: 115 lines (+91/-0)
1 file modified
src/tron/gGame.cpp (+91/-0)
To merge this branch: bzr merge lp:~armagetronad-ct/armagetronad/0.4-delaycmd
Reviewer Review Type Date Requested Status
Armagetron Advanced Developers Pending
Review via email: mp+130693@code.launchpad.net

Description of the change

Hey,

I don't know if it worth including it in mainstream but it happens to be a useful feature for servers like ed's onslaught fortress, mayhem or flower power, and it was just a matter of a few minutes to extract it.
This is the delay_command from sty+ct rewritten a while ago for my experiments on scripting (armagetronad-ct branch). It works the exact same way as in sty+ct:
delay_command [r<repeat_delay>] [+-]<start_delay> <command>

The optional + indicates whether start_delay is relative instead of absolute.
The optional - indicates it should be send before game start (absolute time during countdown)
The repeat option was added by kyle later (may be for some wild fortress maps) and is rather self explanatory.

I allows to avoid external scripting in simple cases like timing messages and scoring (onslaught) or sending stages settings (mayhem).

Some typical example:
delay_command 60 center_message 2 minutes left!

delay_command 37 center_message - 3 -
delay_command 38 center_message - 2 -
delay_command 39 center_message - 1 -
delay_command 40 center_message Stage 2 is starting now!
delay_command 40 cycle_speed 50
...

To post a comment you must log in.

Unmerged revisions

1454. By Voodoo

add delay_command to 0.4

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/tron/gGame.cpp'
--- src/tron/gGame.cpp 2012-08-26 15:53:43 +0000
+++ src/tron/gGame.cpp 2012-10-21 07:50:24 +0000
@@ -759,6 +759,93 @@
759static tSettingItem<bool> sg_ttm("TALK_TO_MASTER",759static tSettingItem<bool> sg_ttm("TALK_TO_MASTER",
760 sg_TalkToMaster);760 sg_TalkToMaster);
761761
762// *************************
763// *** delayed commands
764// *************************
765
766class delayedCommands {
767private:
768 static std::map<int, std::set<std::string> > cmd_map;
769 // compute key for std::multimap
770 static int Key(REAL time) {return int(ceil(time*10));};
771public:
772 // clear all delayed commands
773 static void Clear() {
774 cmd_map.clear();
775 con << "Clearing delayed commands ...\n";
776 };
777 // add new delayed commands
778 static void Add(REAL time, std::string cmd, int interval) {
779 std::stringstream store;
780 store << interval << " " << cmd;
781 cmd_map[Key(time)].insert(store.str());
782 };
783 // check, run and remove delayed commands
784 static void Run(REAL currentTime);
785};
786
787std::map<int, std::set<std::string> > delayedCommands::cmd_map;
788
789static void sg_AddDelayedCmd(std::istream &s)
790{
791 // first parse the line to get the param : delay or interval
792 // if the param start by an r then it means we have the interval
793 // if the param start by a +, assume that it's a delay relative to current game time ...
794 int interval=0;
795 tString delay_str;
796 s >> delay_str;
797 if (delay_str.SubStr(0,1)=="r") {
798 interval = atoi(delay_str.SubStr(1));
799 s >> delay_str;
800 }
801 REAL delay = atof(delay_str);
802 if (delay_str.SubStr(0,1)=="+") {
803 REAL gt = se_GameTime();
804 delay += gt;
805 }
806 // this will make sure it using the command if start time has passed
807 if ((interval > 0) && (se_GameTime() > delay)){
808 REAL ogt = se_GameTime() - delay;
809 int disposition = (ogt/interval)+1;
810 delay = disposition*interval+delay;
811 }
812 tString cmd_str;
813 cmd_str.ReadLine( s, true );
814 if (cmd_str.length()==0) return;
815
816 // add extracted command
817 delayedCommands::Add(delay,cmd_str,interval);
818 //con << "DELAY_COMMAND " << delay << " "<< interval<<" &" << cmd_str.str() << "&\n";
819}
820
821static tConfItemFunc sg_AddDelayedCmd_conf("DELAY_COMMAND",&sg_AddDelayedCmd);
822static tAccessLevelSetter sg_AddDelayedCmdConfLevel( sg_AddDelayedCmd_conf, tAccessLevel_Owner );
823
824void delayedCommands::Run(REAL currentTime) {
825 if (cmd_map.empty()) return;
826 std::map<int, std::set<std::string> >::iterator it = cmd_map.begin();
827 while ((it != cmd_map.end())&&(it->first<=Key(currentTime))) {
828 if (it->first>Key(currentTime-1.0)) {
829 std::set<std::string>::iterator sit (it->second.begin()), send(it->second.end());
830 for(;sit!=send;++sit) {
831 std::istringstream stream(*sit);
832 int interval;
833 stream >> interval;
834 tCurrentAccessLevel elevator( sg_AddDelayedCmd_conf.GetRequiredLevel(), true );
835 tConfItemBase::LoadAll(stream); // run command if it's not too old, otherwise, just skip it ...
836 if (interval>0) {
837 cmd_map[Key(currentTime+interval)].insert(stream.str());
838 }
839 }
840 }
841 cmd_map.erase(it++); // erase current and get next iterator
842 }
843}
844
845// *****************************
846// *** end delayed commands
847// *****************************
848
762#define PREPARE_TIME 4849#define PREPARE_TIME 4
763850
764static bool just_connected=true;851static bool just_connected=true;
@@ -3300,6 +3387,8 @@
33003387
3301 Analysis(0);3388 Analysis(0);
33023389
3390 delayedCommands::Clear();
3391
3303 // wait for external script to end its work if needed3392 // wait for external script to end its work if needed
3304 REAL timeout = tSysTimeFloat() + sg_waitForExternalScriptTimeout;3393 REAL timeout = tSysTimeFloat() + sg_waitForExternalScriptTimeout;
3305 if ( sg_waitForExternalScript )3394 if ( sg_waitForExternalScript )
@@ -4604,6 +4693,8 @@
4604 }4693 }
4605 }4694 }
46064695
4696 delayedCommands::Run(gtime);
4697
4607 static float lastTime = 1e42;4698 static float lastTime = 1e42;
46084699
4609 if(sg_gameTimeInterval >= 0 && (gtime >= lastTime + sg_gameTimeInterval || gtime < lastTime)) {4700 if(sg_gameTimeInterval >= 0 && (gtime >= lastTime + sg_gameTimeInterval || gtime < lastTime)) {

Subscribers

People subscribed via source and target branches