Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Guinn
Wizard


Joined: 03 Mar 2001
Posts: 1127
Location: London

PostPosted: Wed Jan 02, 2008 12:30 pm   

Generic Priority Queue
 
(NOTE: This thread is a CMUD-centric continuation of this thread. See that thread for the original code and zMUD-related discussion)

Not played much with this yet, have added local variables though to use in CMUD
Code:
#CLASS {PriorityQueue}

#VAR PQueue {} {}

#VAR PQueueFlags {} {}

#ALIAS PQueueAdd {#LOCAL $PQueueTemp1, $PQueueTemp2, $PQueueTemp3
#IF (%numparam=0) {#ECHO Parameter required for PQueueAdd} {
  $PQueueTemp3=""
  #LOOP %numparam {
    $PQueueTemp1=%db( @PQueueData, %param( %i))
    #IF ($PQueueTemp1="") {#ECHO Did not find data for queue item %param( %i)} {
      $PQueueTemp2=%word( $PQueueTemp1, 2, "R-")
      $PQueueTemp2=%word( $PQueueTemp2, 1, ",")
      $PQueueTemp1=%replace( $PQueueTemp1, %concat( "R-", $PQueueTemp2), %concat( "R-", %sort( $PQueueTemp2)))
      #ADDKEY PQueue {%param( %i)} {$PQueueTemp1}
      $PQueueTemp1=%word( $PQueueTemp1, 2, "P-")
      $PQueueTemp1=%word( $PQueueTemp1, 1, ",")
      $PQueueTemp1=%rightback( %concat( "0000", $PQueueTemp1), 5)
      $PQueueTemp3=%concat( $PQueueTemp3, "|", $PQueueTemp1, "-", %param( %i))
      }
    }
  $PQueueTemp3=%concat( $PQueueTemp3, "|", %db( @PQueue, "Priorities"))
  #DELITEM $PQueueTemp3 {}
  $PQueueTemp3=%sort( $PQueueTemp3)
  #ADDKEY PQueue {Priorities} {$PQueueTemp3}
  }
#NOOP %-1
}

#ALIAS PQueueDo {
#LOCAL $PQueueDTemp1, $PQueueDTemp2, $PQueueDTemp3
$PQueueDTemp3=%eval( %numitems( @PQueue.Priorities)>0)
#WHILE ($PQueueDTemp3) {
  $PQueueDTemp1=%word( %item( %db( @PQueue, "Priorities"), $PQueueDTemp3), 2, "-")
  $PQueueDTemp2=%word( %word( %db( @PQueue, $PQueueDTemp1), 2, "R-"), 1, ",")
  #IF (%regex( %concat( "-", %replace( $PQueueDTemp2, "|", "--"), "-"), %repeat( %concat( "(-(?:", @PQueueFlags, ")-)+"), %numitems( $PQueueDTemp2)))=0) {$PQueueDTemp2="failed"} {$PQueueDTemp2=""}
  #IF ($PQueueDTemp2="") {
    #NOOP Flags met requirements do this item
    $PQueueDTemp2=%word( %word( %db( @PQueue, $PQueueDTemp1), 2, "U-"), 1, ",")
    #WHILE ($PQueueDTemp2) {#DELITEM PQueueFlags {%pop( $PQueueDTemp2)}}
    PQueueFlags=%sort( %concat( @PQueueFlags, "|", %word( %word( %db( @PQueue, $PQueueDTemp1), 2, "G-"), 1, ",")))
    #DELITEM PQueueFlags {}
    $PQueueDTemp2=%db( @PQueueMapping, $PQueueDTemp1)
    #IF ($PQueueDTemp2) {#EXEC {$PQueueDTemp2}} {#EXEC {$PQueueDTemp1}}
    $PQueueDTemp2=%word( %word( %db( @PQueue, $PQueueDTemp1), 2, "C-"), 1)
    #DELNITEM PQueue.Priorities $PQueueDTemp3
    #IF ($PQueueDTemp3>%numitems( @PQueue.Priorities)) {$PQueueDTemp3=0}
    #IF ($PQueueDTemp2="1") {$PQueueDTemp3=0}
    } {
    #ADD $PQueueDTemp3 1
    #IF ($PQueueDTemp3>%numitems( @PQueue.Priorities)) {$PQueueDTemp3=0}
    }
  }
}

#CLASS 0

#CLASS {PriorityQueue|PQCreator}

#ALIAS PQueueCreate {
#LOCAL $PQueueCTemp1, $PQueueCTemp2, $PQueueCTemp3
#IF (%yesno( "Modify existing queue data?")) {$PQueueCTemp1=%word( %pick( "p:Please choose entry to modify", "o:1", %replace( %expanddb( %replace( @PQueueData, "|", "/"), "|", "="), ",", " ")), 1, "=")} {
  $PQueueCTemp1=%replace( %prompt( "", "Name of new command?"), %char( 34), "")
  #IF ($PQueueCTemp1!="") {
    $PQueueCTemp2=%regex( $PQueueCTemp1, "[\-\|\ =\.\?\;\[\]\,\:\(\)\'\{\}/]")
    #WHILE ($PQueueCTemp2) {
      $PQueueCTemp1=%replace( %prompt( "", %concat( "Previous entry invalid(char '", %copy( $PQueueCTemp1, $PQueueCTemp2, 1), "'). Name of new command?")), %char( 34), "")
      #IF ($PQueueCTemp1!="") {$PQueueCTemp2=%regex( $PQueueCTemp1, "[\-\|\ =\.\?\;\[\]\,\:\(\)\'\{\}/]")} {$PQueueCTemp2=0}
      }
    }
  }
#IF ($PQueueCTemp1) {
  $PQueueCTemp2=%prompt( %word( %word( %db( @PQueueData, $PQueueCTemp1), 2, "P-"), 1), %concat( "Priority for command:", $PQueueCTemp1, "?"))
  #IF ($PQueueCTemp2="") {$PQueueCTemp2="1"}
  $PQueueCTemp3=%concat( "P-", $PQueueCTemp2)
  $PQueueCTemp2=%pick( %concat( "p:Requirement flags for command:", $PQueueCTemp1), %expanddb( @PQFlagDef, "|"), "Other")
  #IF (%ismember( "Other", $PQueueCTemp2)) {
    #DELITEM $PQueueCTemp2 {Other}
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",R-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    PQueueNewFlag
    } {
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",R-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    }
  $PQueueCTemp2=%pick( %concat( "p:Uses flags for command:", $PQueueCTemp1), %expanddb( @PQFlagDef, "|"), "Other")
  #IF (%ismember( "Other", $PQueueCTemp2)) {
    #DELITEM $PQueueCTemp2 {Other}
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",U-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    PQueueNewFlag
    } {
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",U-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    }
  $PQueueCTemp2=%pick( %concat( "p:Gives flags for command:", $PQueueCTemp1), %expanddb( @PQFlagDef, "|"), "Other")
  #IF (%ismember( "Other", $PQueueCTemp2)) {
    #DELITEM $PQueueCTemp2 {Other}
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",G-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    PQueueNewFlag
    } {
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",G-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    }
  #IF (%yesno( %concat( "Await confirmation on command ", $PQueueCTemp1, "?"))) {$PQueueCTemp3=%concat( $PQueueCTemp3, ",C-1")} {$PQueueCTemp3=%concat( $PQueueCTemp3, ",C-0")}
  #IF (%yesno( %concat( "Add mapping for command ", $PQueueCTemp1, "?"))) {
    $PQueueCTemp2=%replace( %prompt( "", %concat( "Mapping value for '", $PQueueCTemp1, "'?")), %char( 34), "")
    #WHILE (%regex( $PQueueCTemp2, "[\-\|\=\.\?\;\[\]\,\:\(\)\'\{\}/]")) {$PQueueCTemp2=%replace( %prompt( "", %concat( "Previous entry invalid. Mapping value for '", $PQueueCTemp1, "'?")), %char( 34), "")}
    #IF ($PQueueCTemp2) {#ADDKEY PQueueMapping {$PQueueCTemp1} {%expand( $PQueueCTemp2, 1)}}
    }
  #ADDKEY PQueueData {$PQueueCTemp1} {$PQueueCTemp3}
  }
}

#ALIAS PQueueNewFlag {
#LOCAL $PQueueCTemp1, $PQueueCTemp2, $PQueueCTemp3
$PQueueCTemp2=%replace( %prompt( "", "New flag short value?"), %char( 34), "")
#WHILE ($PQueueCTemp2) {
  #IF (%regex( $PQueueCTemp2, "[\-\|\ =\.\?\;\[\]\,\:\(\)\'\{\}/]")) {$PQueueCTemp2=%replace( %prompt( "", "Previous entry invalid. New flag short value?"), %char( 34), "")} {#IF (%ismember( $PQueueCTemp2, %expanddb( @PQFlagDef, "|", "|"))) {$PQueueCTemp2=%replace( %prompt( "", %concat( "'", $PQueueCTemp2, "' already used for '", %item( %expanddb( @PQFlagDef, "|", "|"), %eval( %ismember( $PQueueCTemp2, %expanddb( @PQFlagDef, "|", "|"))-1)), "'. New flag short value?")), %char( 34), "")} {#IF ($PQueueCTemp2!="") {
        $PQueueCTemp3=%concat( $PQueueCTemp3, "|", $PQueueCTemp2)
        $PQueueCTemp2=""
        #WHILE ($PQueueCTemp2="") {
          $PQueueCTemp2=%replace( %prompt( "", %concat( "Description of flag '", %item( $PQueueCTemp3, %numitems( $PQueueCTemp3)), "'?")), %char( 34), "")
          #WHILE (%regex( $PQueueCTemp2, "[\-\|\=\.\?\;\[\]\,\:\(\)\'\{\}/]")) {$PQueueCTemp2=%replace( %prompt( "", %concat( "Description of flag '", %item( $PQueueCTemp3, %numitems( $PQueueCTemp3)), "'?")), %char( 34), "")}
          }
        #ADDKEY PQFlagDef {$PQueueCTemp2} {%item( $PQueueCTemp3, %numitems( $PQueueCTemp3))}
        }}}
  $PQueueCTemp2=%replace( %prompt( "", "New flag short value?"), %char( 34), "")
  }
}

#CLASS 0

#CLASS {PriorityQueue|PQPersistentVars}

#VAR PQueueData {}

#VAR PQueueMapping {}

#VAR PQFlagDef {}

#CLASS 0


If it seems longer it's because I've let CMUD reformat the script so there are far more newlines. You can still paste into the command line though
_________________
CMUD Pro, Windows Vista x64
Core2 Q6600, 4GB RAM, GeForce 8800GT
Because you need it for text... ;)
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Thu Jan 03, 2008 5:03 am   
 
As long as we are changing this over to Cmud here is an XML version of it.
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <class name="PriorityQueue">
    <alias name="PQueueAdd">
      <value>#local $Priorities $Requirements $Data $PNum
#IF (%numparam=0) {#ECHO Parameter required for PQueueAdd} {
  #LOOP %numparam
    {
     $Data=%db( @PQueueData, %param( %i))
     #IF ($Data="") {#ECHO Did not find data for queue item %param( %i)} {
       $Requirements=%word( %word( $Data, 2, "R-"), 1, ",")
       $Data=%replace( $Data, %concat( "R-", $Requirements), %concat( "R-", %sort( $Requirements)))
       #ADDKEY PQueue {%param( %i)} {$Data}
       $PNum=%word(%word( $Data, 2, "P-") , 1, ",")
       $PNum=%rightback( %concat( "0000", $PNum), 5)
       $Priorities=%concat( $Priorities, "|", $PNum, "-", %param( %i))
      }
    }
  $Priorities=%concat( $Priorities, "|", %db( @PQueue, "Priorities"))
  #DELITEM $Priorities {}
  $Priorities=%sort( $Priorities)
  #ADDKEY PQueue {Priorities} {$Priorities}
  }
#NOOP %-1</value>
    </alias>
    <alias name="PQueueDo">
      <value>#Local $ItemN $PItem $Requirements $Uses $Skill $RConfirm
$ItemN=%eval( %numitems( @PQueue.Priorities)>0)
#WHILE ($ItemN) {
  $PItem=%word( %item( %db( @PQueue, "Priorities"), $ItemN), 2, "-")
  $Requirements=%word( %word( %db( @PQueue, $PItem), 2, "R-"), 1, ",")
  #IF (%regex( %concat( "-", %replace( $Requirements, "|", "--"), "-"), %repeat( %concat( "(-(?:", @PQueueFlags, ")-)+"), %numitems( $Requirements)))=0) {$Requirements="failed"} {$Requirements="passed"}
  #IF ($Requirements="passed") {
    #NOOP Flags met requirements do this item
    $Uses=%word( %word( %db( @PQueue, $PItem), 2, "U-"), 1, ",")
    #WHILE ($Uses) {#DELITEM PQueueFlags {%pop( $Uses)}}
    PQueueFlags=%sort( %concat( @PQueueFlags, "|", %word( %word( %db( @PQueue, $PItem), 2, "G-"), 1, ",")))
    #DELITEM PQueueFlags {}
    $Skill=%db( @PQueueMapping, $PItem)
    #IF ($Skill) {#EXEC {$Skill}} {#EXEC {$PItem}}
    $RConfirm=%word( %word( %db( @PQueue, $PItem), 2, "C-"), 1)
    #DELNITEM PQueue.Priorities $ItemN
    #IF ($ItemN>%numitems( @PQueue.Priorities)) {$ItemN=0}
    #IF ($RConfirm="1") {$ItemN=0}
    } {
    #ADD $ItemN 1
    #IF ($ItemN>%numitems( @PQueue.Priorities)) {$ItemN=0}
    }
  }</value>
      <notes>Skill=PriorityN,Requirements,Uses,Gives,Confirm
Vitality=P-90,R-A|e|G|N|S|V|x,U-e|V,G-,C-0

Priorities=PriorityN-Skill
Priorities=00090-Vitality</notes>
    </alias>
    <var name="PQueue" type="Record" usedef="true">
      <notes>Priority,Requirements,Uses,Gives,Confirm
P-90,R-A|e|G|N|S|V|x,U-e|V,G-,C-0</notes>
    </var>
    <var name="PQueueFlags" type="StringList" usedef="true"/>
    <class name="PQPersistentVars">
      <var name="PQueueData" type="Record">
        <value>vitality="P-90,R-e|x|G|N|V|A|S,U-e|V,G-,C-0"|upc2="P-122,R-e|A|S|r,U-r,G-,C-0"|scs="P-110,R-e|x|A|S,U-x,G-l|r,C-0"|sdk="P-120,R-e|x|A|S,U-x,G-l|r,C-0"|upc="P-121,R-e|A|S|l,U-l,G-,C-0"|weathering="P-100,R-x|A|S,U-,G-,C-0"</value>
        <notes>e|x|G|N|V|A|S</notes>
      </var>
      <var name="PQueueMapping" type="Record">upc2=upc @target|sdk=sdk @target|upc=upc @target</var>
      <var name="PQFlagDef" type="Record">vitality balance=V|right arm balance=r|equilibrium=e|standing=S|full health=G|full mana=N|left arm balance=l|awake=A|full balance=x</var>
    </class>
    <class name="PQCreator">
      <alias name="PQueueCreate">
        <value>#LOCAL $PQueueCTemp1, $PQueueCTemp2, $PQueueCTemp3
#IF (%yesno( "Modify existing queue data?")) {$PQueueCTemp1=%word( %pick( "p:Please choose entry to modify", "o:1", %replace( %expanddb( %replace( @PQueueData, "|", "/"), "|", "="), ",", " ")), 1, "=")} {
  $PQueueCTemp1=%replace( %prompt( "", "Name of new command?"), %char( 34), "")
  #IF ($PQueueCTemp1!="") {
    $PQueueCTemp2=%regex( $PQueueCTemp1, "[\-\|\ =\.\?\;\[\]\,\:\(\)\'\{\}/]")
    #WHILE ($PQueueCTemp2) {
      $PQueueCTemp1=%replace( %prompt( "", %concat( "Previous entry invalid(char '", %copy( $PQueueCTemp1, $PQueueCTemp2, 1), "'). Name of new command?")), %char( 34), "")
      #IF ($PQueueCTemp1!="") {$PQueueCTemp2=%regex( $PQueueCTemp1, "[\-\|\ =\.\?\;\[\]\,\:\(\)\'\{\}/]")} {$PQueueCTemp2=0}
      }
    }
  }
#IF ($PQueueCTemp1) {
  $PQueueCTemp2=%prompt( %word( %word( %db( @PQueueData, $PQueueCTemp1), 2, "P-"), 1), %concat( "Priority for command:", $PQueueCTemp1, "?"))
  #IF ($PQueueCTemp2="") {$PQueueCTemp2="1"}
  $PQueueCTemp3=%concat( "P-", $PQueueCTemp2)
  $PQueueCTemp2=%pick( %concat( "p:Requirement flags for command:", $PQueueCTemp1), %expanddb( @PQFlagDef, "|"), "Other")
  #IF (%ismember( "Other", $PQueueCTemp2)) {
    #DELITEM $PQueueCTemp2 {Other}
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",R-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    PQueueNewFlag
    } {
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",R-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    }
  $PQueueCTemp2=%pick( %concat( "p:Uses flags for command:", $PQueueCTemp1), %expanddb( @PQFlagDef, "|"), "Other")
  #IF (%ismember( "Other", $PQueueCTemp2)) {
    #DELITEM $PQueueCTemp2 {Other}
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",U-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    PQueueNewFlag
    } {
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",U-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    }
  $PQueueCTemp2=%pick( %concat( "p:Gives flags for command:", $PQueueCTemp1), %expanddb( @PQFlagDef, "|"), "Other")
  #IF (%ismember( "Other", $PQueueCTemp2)) {
    #DELITEM $PQueueCTemp2 {Other}
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",G-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    PQueueNewFlag
    } {
    $PQueueCTemp3=%concat( $PQueueCTemp3, ",G-")
    #FORALL $PQueueCTemp2 {$PQueueCTemp3=%concat( $PQueueCTemp3, %word( %i, 2, "="), "|")}
    #DELITEM $PQueueCTemp3 {}
    }
  #IF (%yesno( %concat( "Await confirmation on command ", $PQueueCTemp1, "?"))) {$PQueueCTemp3=%concat( $PQueueCTemp3, ",C-1")} {$PQueueCTemp3=%concat( $PQueueCTemp3, ",C-0")}
  #IF (%yesno( %concat( "Add mapping for command ", $PQueueCTemp1, "?"))) {
    $PQueueCTemp2=%replace( %prompt( "", %concat( "Mapping value for '", $PQueueCTemp1, "'?")), %char( 34), "")
    #WHILE (%regex( $PQueueCTemp2, "[\-\|\=\.\?\;\[\]\,\:\(\)\'\{\}/]")) {$PQueueCTemp2=%replace( %prompt( "", %concat( "Previous entry invalid. Mapping value for '", $PQueueCTemp1, "'?")), %char( 34), "")}
    #IF ($PQueueCTemp2) {#ADDKEY PQueueMapping {$PQueueCTemp1} {%expand( $PQueueCTemp2, 1)}}
    }
  #ADDKEY PQueueData {$PQueueCTemp1} {$PQueueCTemp3}
  }</value>
      </alias>
      <alias name="PQueueNewFlag">
        <value>#LOCAL $PQueueCTemp1, $PQueueCTemp2, $PQueueCTemp3
$PQueueCTemp2=%replace( %prompt( "", "New flag short value?"), %char( 34), "")
#WHILE ($PQueueCTemp2) {
  #IF (%regex( $PQueueCTemp2, "[\-\|\ =\.\?\;\[\]\,\:\(\)\'\{\}/]")) {$PQueueCTemp2=%replace( %prompt( "", "Previous entry invalid. New flag short value?"), %char( 34), "")} {#IF (%ismember( $PQueueCTemp2, %expanddb( @PQFlagDef, "|", "|"))) {$PQueueCTemp2=%replace( %prompt( "", %concat( "'", $PQueueCTemp2, "' already used for '", %item( %expanddb( @PQFlagDef, "|", "|"), %eval( %ismember( $PQueueCTemp2, %expanddb( @PQFlagDef, "|", "|"))-1)), "'. New flag short value?")), %char( 34), "")} {#IF ($PQueueCTemp2!="") {
        $PQueueCTemp3=%concat( $PQueueCTemp3, "|", $PQueueCTemp2)
        $PQueueCTemp2=""
        #WHILE ($PQueueCTemp2="") {
          $PQueueCTemp2=%replace( %prompt( "", %concat( "Description of flag '", %item( $PQueueCTemp3, %numitems( $PQueueCTemp3)), "'?")), %char( 34), "")
          #WHILE (%regex( $PQueueCTemp2, "[\-\|\=\.\?\;\[\]\,\:\(\)\'\{\}/]")) {$PQueueCTemp2=%replace( %prompt( "", %concat( "Description of flag '", %item( $PQueueCTemp3, %numitems( $PQueueCTemp3)), "'?")), %char( 34), "")}
          }
        #ADDKEY PQFlagDef {$PQueueCTemp2} {%item( $PQueueCTemp3, %numitems( $PQueueCTemp3))}
        }}}
  $PQueueCTemp2=%replace( %prompt( "", "New flag short value?"), %char( 34), "")
  }</value>
      </alias>
    </class>
  </class>
  <alias name="b">
    <value>PQueueAdd upc2 sdk upc
PQueueFlags="e|x|A|S"
Target="test"
PQueueDo</value>
  </alias>
  <var name="Target" type="Literal">test</var>
</cmud>


Vijilante's example is already in there too. It is the alias b.

Note that I made some alterations to the PQueueAdd and PQueueDo aliases.
These changes may or may not slow down the script slightly, I haven't checked, but they do not alter the design or functionality at all.
I simply changed/added a few local variable to make the script easier to follow.

For those of you that are intimidated by this script here is a bit more explanation.

The PQueue variable is in the following format.

Skill Name=Priority Number, Balance Requirements [string List], Uses these balances [and thus removes them] [string list], Gives these balances [string list], Keep this item as a priority until it is confirmed [0 or 1] <-<< This last one is not fully implemented and I suggest that you leave it as 0

Example
Vitality=P-90,R-A|e|G|N|S|V|x,U-e|V,G-,C-0

Skill Name: Vitality
Priority Number: 90
Balance Requirements: A|e|G|N|S|V|x
Uses these balances: e|V
Gives these Balances: None
Keep on list:0

Priorities=PriorityN-Skill
Priorities=00090-Vitality

The variable PQueueFlags is a string list that the PQueueDo alias checks for these requirements if a
requirement is not found in this list then the item is not performed and remains in the queue.

The variable PQFlagDef has a list of what each letter represents.
Key=vitality balance
Value=V
Key=awake
Value=A
etc.

The variable PQueueData holds the list of recognized skills and their info.
Key=vitality
Value=P-90,R-e|x|G|N|V|A|S,U-e|V,G-,C-0
key=sdk
Value=P-120,R-e|x|A|S,U-x,G-l|r,C-0

This should look familiar as it is almost the same format as the PQueue variable.
The only difference is that there is no Priorities key.

Finally the PQueueMapping variable.
Key=Skill Name
Value=Skill Long hand command

Example.
Key=sdk
Value=sdk @target
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Jan 03, 2008 9:10 am   
 
I love you, Arminas. Not just for that format and usage information, which is great, but for changing the names of the variables. $Data is so much more useful than @PQueueTemp3 - I understand that the original intention was probably to avoid naming conflicts, but regardless, it makes the script much easier to follow.

I reckoned that this script was highly suited to a Lua implementation, especially given Lua's very flexible sorting option. Here's an idea I had:

Code:
-- Attack data variable. The flags aren't hardcoded - check out PQueueDo() which will flexibly look for any flag you specify. Ommitted flags are assumed false.
-- If you wanted, you could change this to use long flag definitions and short flag names, like the original script, but I prefer clarity.
PQueueData = {}
-- example :
PQueueData.vitality = { priority    = 122 ,
                        requires    =   {   equilibrium     = true,
                                            balance         = true,
                                            health          = true,
                                            mana            = true,
                                            awake           = true,
                                            standing        = true,
                                            vita_balance    = true,
                                        },
                        takes       =   {   equilibrium     = true,
                                            vita_balance    = true
                                        },
                        gives       =   {},
                        command = "vitality"
                      }

-- Integer table, values are just names of attacks
PQueue = {}


function PQueueAdd ( attack )
    -- attack (string) - name of the ability to add to the list
   
    -- error if invalid name - assert if you like
    if not PQueueData[attack] then
        return nil, "Bad Argument to PQueueAdd - unknown ability " .. attack
    end
   
    table.insert(PQueue, attack)
    -- no need to handle all the extra information now, do it later when we want a value
    -- could add a sort here, but I prefer to do it when pulling a value
    return attack
end



-- rather than use a static flag variable, I chose to have the flags table passed as a parameter to the function. You can keep the flag list in a variable if you want, but I won't make you.
-- the flexible sorting lets you do some really interesting stuff, like prioritise items that don't take any balances and the like. Go nuts.

function PQueueDo (flags, sort)
    -- flags (string table - keys flag names, values true or false) -  will be compared to the flag list for an ability
    -- sort (function) - function for use with table.sort. Defaults to sorting by priority value
   
    -- because this table's going to be called a lot
    local PQueueData = PQueueData
   
    -- sort our queue - use sort if it's given
    table.sort(PQueue, sort or function (a,b) return PQueueData[a].priority < PQueueData[b].priority end)
   
    for n,item in ipairs(PQueue) do
        local currentitem = PQueueData[item]
        local failed = false

        -- check current (highest-priority) attack's flags - I'm sure there's a better way to do this but I'm buggered if I can think of it now
        for _,flag in pairs(currentitem.requires) do
            if ( not flags[flag] ) then
                failed = true
            end
        end
        if not failed then
            for _,flag in pairs(currentitem.takes) do
                flags[flag] = false
            end
            for _,flag in pairs(currentitem.gives) do
                flags[flag] = true
            end
            return(table.remove(PQueue,n))
        end
    end
    return false
end

-- since adding items is fairly trivial, I decided to leave out PQueueCreate() - you could write your own if you wanted.
-- I like not needing PQueueNewFlag() because it adds more flexibility - you could use a wrapper for it that checks for valid flags if you want.


I have no idea if this code actually works or not (though I'd hope so) - I post it more as a proof of concept ;)

It's too early in the morning, I can't work out if lines 58 and 59 should read

Code:
for _,flag in pairs(currentitem.requires) do
    if ( not flags[flag] ) then


or if it even makes a difference to the logic. It might do.

EDIT: Coming back to this in the afternoon, it's obvious that it should check the ones required against the current flags and not the other way around (because the current way would pass if the current flags were a subset of the required ones, even if all the required ones weren't present). I've changed it above.

I'm going to split the 2008 posts in this thread into the CMUD Gen Desc forum, since they're getting deeper and deeper into CMUD implementations.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Guinn
Wizard


Joined: 03 Mar 2001
Posts: 1127
Location: London

PostPosted: Thu Jan 03, 2008 10:32 pm   
 
(Wonder if we need the 'finished scripts' forum to become 'Finished zMUD Scripts' and have Zugg introduce a 'Finished CMUD Scripts' as well?)
_________________
CMUD Pro, Windows Vista x64
Core2 Q6600, 4GB RAM, GeForce 8800GT
Because you need it for text... ;)
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Thu Jan 03, 2008 11:14 pm   
 
Well, Zugg was talking like he wanted to use the package library for that.

But what about a compromise that we don't post the actual scripts in the Cmud scripts forum but we use it for discussing scripts published in the library? *head scratch*
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Jan 03, 2008 11:17 pm   
 
There was talk of having cmud support some kind cmud:// link that'll open a package in the package library. That'd make that kind of thing even easier.

Might as well use this forum for that for now, though.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Guinn
Wizard


Joined: 03 Mar 2001
Posts: 1127
Location: London

PostPosted: Fri Jan 04, 2008 12:06 am   
 
Hmm, I far prefer the forums - makes it easier to collaborate on a script or offer any help.
I don't imagine I'd browse the library for new scripts regularly like I do the forums and if making any suggestions you'd only have to paste them here to comment on anyway Confused
_________________
CMUD Pro, Windows Vista x64
Core2 Q6600, 4GB RAM, GeForce 8800GT
Because you need it for text... ;)
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Mon Jan 14, 2008 12:24 am   
 
It just struck me that line 71 of the script above should probably be return table.remove(PQueue,n), flags so that you can update your global flags table (if you have one) with the new flag data.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net