Conditional Line break ?

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Ditto
Giant
Posts: 179
Joined: Thu Jul 28, 2011 1:06 pm

Conditional Line break ?

Post by Ditto »

Hi guys, hoping to get some help again, running into some minor issues.

v1.4.0.5

I have a function that's mostly working fine. What it does is you pass it the # of dice to roll.
It rolls the dice, and displays each die rolled (pulling an image from a table).

Code: Select all

<image src='[r: tblImage("White", s, 20) ]'> </image>
This works perfectly fine.

It get's tricky, however, since I have some additional conditions that pull the image from a couple different tables (ie different colored dice to represent different types of rolls).

So my function looks like this:

Code: Select all

[h: rNum = arg(0) ]
[h: abort(length(rNum))]
[h: mod = arg(1) ]
[h: rMode = arg(2) ]
[h: rKmod = arg(3) ]

[h: ts=0]
[h: tb=0]

[h: rHalf = if ( rNum == floor(rNum), 0, 1 ) ]
[h: rNum = floor ( rNum ) ]

[r: rNum][r,if(rHalf==1),CODE:{½};{}]d6[r: strformat ( "%+d", mod ) ] <br>

[r,switch(rMode),code:
   case "knockback": {
      [r,for(i,0,rNum,1,""), CODE: {
          [h:s=1d6]
<image src='[r: tblImage("White", s, 20) ]'> </image>
         [h:ts=ts+s]
      }]
      [h:ts=ts+mod]
      [h: lBODY = rKmod]
      [h: lROLL = ts ]
      <br>
      <b>Knockback:</b> [r: lBODY - lROLL ]  
      };
   case "raw": {
      [r,for(i,0,rNum,1,""), CODE: {
          [h:s=1d6]
<image src='[r: tblImage("White", s, 20) ]'> </image>
         [h:ts=ts+s]
      }]
      [h:ts=ts+mod]
      <br>
      <b>Total:</b> [r: ts]  
      };
   case "normal": {
      [r,for(i,0,rNum,1,""), CODE: {
          [h:s=1d6]
<image src='[r: tblImage("White", s, 20) ]'> </image>
          [h:b=if(s==6,2,if(s==1,0,1))]
         [h:ts=ts+s]
         [h:tb=tb+b]
      }]
      [h:ts=ts+mod]
      <br>
      <b>Stun:</b> [r: ts]  ( <b>Body:</b> [r: tb] ) 
      };
   case "killing": {
      [r,for(i,0,rNum,1,""), CODE: {
          [h:b=1d6]
<image src='[r: tblImage("Red", b, 20) ]'> </image>
         [h:tb=tb+b]
      }]
     [r, if ( rHalf > 0 ), CODE: {
          [h:b=1d3]
<image src='[r: tblImage("Red", b, 15) ]'> </image>
         [h:tb=tb+b]
    };{}]

      [h:tb=tb+mod]
      [h:s=1d6]
 <image src='[r: tblImage("Black", s, 20) ]'> </image>
     [h:s=s-1+rKmod]
      [h:ts=s*tb]
      <br>
      <b>Body:</b> [r: tb] ( <b>Stun:</b> [r: ts] ) 
      }
]
Again - so far - this is working just fine.

With 1 small problem. If I put a large # of dice in (say 33), it dumps the 33 dice images all in 1 line, and won't wrap them. So depending on how the chat window is setup, you don't see all the rolls.

I tried playing with html tables, but couldn't get them to change the behaviour.

I tried putting a space in the loop:

Code: Select all

      [r,for(i,0,rNum,1," "), CODE: {
                         ^
                     space added here
in the hopes it would wrap a "word" at edge of display. No luck. :(

I tried adding a conditional:

Code: Select all

[h, if ( condition ), CODE: { <br> };{}]
but that broke it - I think because I have too many layers of "CODE: { }" ... (FYI: I have none prior to this function being called).

So a few questions:

1) Is there are shorter way of writing a coditional Line break (ie <br> ) that avoids using "CODE: { }" ??
2) Is there any way to use HTML tables to force it to wrap the line ?
3) Is there any other way to pull this off ?
4) Any feedback/suggestions on the code would be appreciated as well :)

Thanks!

Ditto
Giant
Posts: 179
Joined: Thu Jul 28, 2011 1:06 pm

Re: Conditional Line break ?

Post by Ditto »

Ok, I kept playing with it ... and I did find a work around .. however, I'm still open to better ideas/suggestions if anyone has any :)

Here's the code I got working:
(basically, I just split up the switch, by trying to do more common stuff together ... )

Code: Select all

[h: rNum = arg(0) ]
[h: abort(length(rNum))]
[h: mod = arg(1) ]
[h: rMode = arg(2) ]
[h: rKmod = arg(3) ]
[h: cWrap = 20]

[h: ts=0]
[h: tb=0]

[h: rHalf = if ( rNum == floor(rNum), 0, 1 ) ]
[h: rNum = floor ( rNum ) ]

[r: rNum][r,if(rHalf==1),CODE:{½};{}]d6[r: strformat ( "%+d", mod ) ] <br>

[r,switch(rMode),code:
   case "knockback": {
      [h: dieTbl = "White" ]
      };
   case "raw": {
      [h: dieTbl = "White" ]
      };
   case "normal": {
      [h: dieTbl = "White" ]
      };
   case "killing": {
      [h: dieTbl = "Red" ]
      }
]

[h: numLoops = ceiling(rNum/cWrap) ]

[r,for(j,0,numLoops,1,"<br>"), CODE: {
   [h: NumItems = min(cWrap, rNum - (j*cWrap) ) ]
   [r,for(i,0,NumItems,1,""), CODE: {
      [h:s=1d6]
      <image src='[r: tblImage(dieTbl, s, 20) ]'></image>
      [h:b=if(s==6,2,if(s==1,0,1))]
      [h:ts=ts+s]
      [h:tb=tb+b]
   }]
}]

[r,switch(rMode),code:
   case "knockback": {
      [h:ts=ts+mod]
      [h: lBODY = rKmod]
      [h: lROLL = ts ]
      <br>
      <b>Knockback:</b> [r: lBODY - lROLL ]  
      };
   case "raw": {
      [h:ts=ts+mod]
      <br>
      <b>Total:</b> [r: ts]  
      };
   case "normal": {
      [h:ts=ts+mod]
      <br>
      <b>Stun:</b> [r: ts]  ( <b>Body:</b> [r: tb] ) 
      };
   case "killing": {
      [h: tb=ts]
      [h: ts=0]
      
      [r, if ( rHalf > 0 ), CODE: {
         [h:b=1d3]
         .<image src='[r: tblImage("Red", b, 15) ]'></image>
         [h:tb=tb+b]
      };{}]

      [h:tb=tb+mod]
      [h:s=1d6]
      X<image src='[r: tblImage("Black", s, 20) ]'> </image>
      [h:s=s-1+rKmod]
      [h:ts=s*tb]
      <br>
      <b>Body:</b> [r: tb] ( <b>Stun:</b> [r: ts] ) 
      }
]

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Conditional Line break ?

Post by Irrlicht »

I'm in a bit of a hurry, so I may have misunderstood what you need, but maybe you just need this:
[r, if(condition): "<br>"]
Or, as part of the body:
[r: Stuff + " other stuff" + if(condition, "<br>", "")]
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Conditional Line break ?

Post by aliasmask »

Here's how I would do it. First, put all your output in to a variable. Also, don't use the MT shortcut for size.

Code: Select all

[H: output = ""]
[H: numDice = 5d6]
[H, for(i,0,numDice), code: {
   [H: rollImage = tableImage("D100",1d10)]
   [H: output = output + strformat('<img src="%{rollImage}" width=40 height=40 />')]
   [H, if(! math.mod(i+1,5) && i != numDice -1): output = output + "<br />"]
}]
[dialog("D"):{[r: output]}]

Ditto
Giant
Posts: 179
Joined: Thu Jul 28, 2011 1:06 pm

Re: Conditional Line break ?

Post by Ditto »

aliasmask wrote:Here's how I would do it. First, put all your output in to a variable. Also, don't use the MT shortcut for size.

Code: Select all

[H: output = ""]
[H: numDice = 5d6]
[H, for(i,0,numDice), code: {
   [H: rollImage = tableImage("D100",1d10)]
   [H: output = output + strformat('<img src="%{rollImage}" width=40 height=40 />')]
   [H, if(! math.mod(i+1,5) && i != numDice -1): output = output + "<br />"]
}]
[dialog("D"):{[r: output]}]
Thanks .. I'm going to give this a try ..
I wasn't sure how to deal with the image in this case, so I just went very "brute-force-basic" :)

Post Reply

Return to “Macros”