mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Feb 2023
Posts: 2
H
Bowl of petunias
OP Offline
Bowl of petunias
H
Joined: Feb 2023
Posts: 2
Hey everyone smile

Can someone please help me with a script which I just tried to make? Im really new to scripting and can't find a solution


What it should do:

- Give all letters and numbers a random color on input
- The colors 0, 1, 2, 3, 5, 14 and 15 should not be chosen
- The same color should not be chosen in the next 3 letters/numbers

My code so far:

Code
alias rand {
  return $rand(0,15)
}

on *:input:#: {
  var %i = 1, %str, %color, %lastColor1 = $null, %lastColor2 = $null, %lastColor3 = $null
  while (%i <= $len($1-)) {
    if ($mid($1-,%i,1) == " ") {
      %str = %str " "
      inc %i
      continue
    }

    %color = $rand(0,15)
    while (%color == 0 || %color == 1 || %color == 2 || %color == 3 || %color == 5 || %color == 14 || %color == 15 || %color == %lastColor1 || %color == %lastColor2 || %color == %lastColor3) {
      %color = $rand(0,15)
    }
    %str = %str $+ $chr(3) $+ %color $+ $mid($1-,%i,1)
    set %lastColor3 %lastColor2
    set %lastColor2 %lastColor1
    set %lastColor1 %color
    inc %i
  }
  msg $chan %str
  halt
}

It works partly. It gives every letter a random color. But it does not display spaces, and when I use numbers in my input, it misses some in output.

Thank you in advance smile

Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
Colors can be either 1 or 2 integers, so if you intend ^4123 to show the number 123 in red, it needs to be zero-padded to 2 digits.

The only time doublequotes are not included in the string the way you're doing it is that var %var = "" sets the var to the empty string. A variable cannot end with a single space unless using the -p switch.

Instead of checking if it's one of the colors not allowed, I just have a list of zero-padded numbers that are allowed, and I hardcoded my $rand call to use the number of items in the list. (Your 'rand' alias is never seen because a built-in identifier is always seen before a scripted alias)

Instead of making 3 checks against the 3 previous colors, I just toss back the random color if it's in the list. And I can't have a false match because I'm comparing 2 digits against 2 digits that are separated by spaces.

Note that if you comment out the line having the special handling for spaces, this will show your message as if it has consecutive spaces because they all have colors between them.

I added a check to see if your input began with a '/' because otherwise this would be showing your editbox /command to channel. The $ctrlenter is if you want to be able to selectively exclude messages from being colorized by pressing Ctrl+Enter together. The $inpaste doesn't trigger if you have editbox lines set to auto.

Things to consider if you use this for sending actual messages to actual channels:

* This acts in all channels, and some of them hate colors and will ban you for it. Some channels have a setting that strips colors from messages before displaying them, while other channels have a different flag that completely blocks a message containing any colors. You can restrict it by changing from :#: to :#chan1,#chan2: without spaces.

* By now, the 99 colors are supported almost universally except for those still using old versions. Other users may not see colors 00-15 the same way because they can define them to any 24-bit RGB they wish. But on the other hand, colors 16-98 are standardized as specific color shades that cannot be changed, so you can use color 52 and be guaranteed that they see red except those using versions older than v7.52 from 2018.

* You're assuming that everyone has the same background color as you do, or that they have modified the RGB for colors 0-15 so they work against whatever is their chosen background color. For example, the default color 12 contrasts well against white, but not against blackground. So the only way to get it to contrast against black is to change the RGB into a light-blue shade that contrasts against black, but doesn't look good against white. Plus, there's the problem of wanting to see a 'good blue' for windows where I set the background color, and the problem trying to find blue-ish shades for 02 12 10 11 that all contrast against black while still still allowing you to tell the 4 apart.

One way to solve the contrast problem is to begin your message by beginning your message with the color code that sets it to a specific background color. But then, people can be annoyed to see your messages showing up as a different background stripe than all the other.

* If people find this annoying, you might want to instead have the behavior be normal most of the time, but only changes to be colorized if you DO press ctrl+enter, such as changing the 1st line to be:

if (!$ctrlenter) return

Or, you can have an alias to only do this when you have an editbox command, where you'd put this code into the '/colormytext' alias instead of intercepting ON INPUT

Code
on *:INPUT:#:{
  if ((/* iswm $1) || ($ctrlenter) || ($inpaste) ) return
  var %i = 0, %str, %colors 04 06 07 08 09 10 11 12 13 , %recentcolors
  while (%i < $len($parms)) {
    inc %i
    var -p %char $mid($parms,%i,1)
    if (%char == $chr(32)) { var -p %str $+(%str,$v2) | continue }
    while ($gettok(%colors,$rand(1,9),32) isin %recentcolors) noop
    var %recentcolors $gettok($v1 %recentcolors,1-3,32)
    var -p %str $+(%str,$chr(3),$v1,%char)
  }
  !msg $chan %str
  halt
}

Joined: Feb 2023
Posts: 2
H
Bowl of petunias
OP Offline
Bowl of petunias
H
Joined: Feb 2023
Posts: 2
Hello @maroon and thank you very much for your extensive explaination of your code.

As I see you are a real mIRC veteran grin Can you tell me what you are doing for a living and what was your greatest project on mIRC scripting?


To understand your code, I have a few questions I hope I may ask:

1. Why do you use
Code
$parms
instead of
Code
$1-
? In mIRC help it says, $parms is an untokenized $1-. What would change that in output?


2. What exaxtly does this do?
Code
var -p %str $+(%str,$v2)
For me it sets the variable %str to "%str_" (_=space) when
Code
%char == $chr(32)
Why do we need to specify the variable %str already on this line? Because later in
Code
var -p %str $+(%str,$chr(3),$v1,%char)
the variable %str gets changed anyways. Am I assuming right, that
Code
$v2
refers to the "$chr(32)" in
Code
if (%char == $chr(32))
?


3. What is the "32" in
Code
$gettok(%colors,$rand(1,9),32
for? I assume ascii code for "space", but I don't fully understand $gettok. In mIRC help it says "Returns the Nth token in text". I don't understand "Nth". You also used it here:
Code
$gettok($v1 %recentcolors,1-3,32)


4. Why do you first set the while loop for checking about recent used colors in
Code
while ($gettok(%colors,$rand(1,9),32) isin %recentcolors) noop
and after that, you set the variable %recentcolors to the values which were used last using
Code
var %recentcolors $gettok($v1 %recentcolors,1-3,32)
? Until here %recentcolos is empty or $null anyways, or not?


5. What is the "$v1" in
Code
var %recentcolors $gettok($v1 %recentcolors,1-3,32)
and
Code
var -p %str $+(%str,$chr(3),$v1,%char)
refering to?


6. Whats the advantage of using an "!" on
Code
!msg $chan %str
?



Thank you in advance. With much respect,

Housecat

Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
A1. Tokenizing into $1- removes duplicated/leading/trailing spaces, so if you want to preserve those, $parms is your guy. For the most part, a /command tokenizes the input while an $identifier() does not. There's a few exceptions like /returnex

A2. The -p switch is a combination of the -n switch that does math against the $1-, and preserving spaces. Without -n or -p, it's impossible to set a %var to 2 + 2, and without -p it's impossible to have the %var have exactly 1 trailing space, which means "var -p %str $+(%str,$v2)" would do nothing if $v2 is the space, unless %str already had at least 1 trailing space.

I defined the %char because there there's 2 different things to do with it, depending if it's a space or not. If a space, we append it to the string build, but otherwise we append it along with the color. The alternative would be to make a 2nd call to $mid() each time we append a non-space

A3. The F1 help has syntax for all the commands and identifiers. Well, most of them. You can also find help at

https://en.wikichip.org/wiki/mirc

The wikichip has often been given more detailed descriptions, and each identifier/command has its own page, which can sometimes help in understanding things. For example, all the *tok identifiers are inside F1 on a single page, and when you go to $gettok where it shows $gettok(text,N,C) you're supposed to go up to $addtok to find out what the C means. The C just means the codepoint used to delimit a string into tokens, and N is the number of the token in the list you want, so this shows the top level folder of your mirc's path next to the root

//echo -a $gettok($mircdir,2,$asc(\))

A4 A5 These are the same question. If you have an if() or elseif() or while() branch, $v1 is the term1 and $v2 is the term2, so using the while() this way was a quick way to avoid entering the loop if $1- is blank, and exits when finished handling it. And, the $v1 and $v2 are reset each time the loop completes, and /continue is basically the same as 'goto the while() command'

//if ($me != $network) echo -a term1 is $v1 and term2 is $v2

A lot of inefficient scripts will make redundant slow-calls instead of just using $v1 to get the same result

reads the disk twice:
//if ($read(file,nt,%i) != $null) echo -a $read(file,nt,%i)
reads the disk once:
//if ($read(file,nt,%i) != $null) echo -a $v1

Note that in this example you can have an echo error if the line is entirely spaces, because they'd get tokenized down as if you didn't feed any string to echo. So that can be avoided like:

//if ($gettok($read(file,nt,%i),1-,32) != $null) echo -a $v1

Wikichip has more details on the duration of $v1 and $v2

A6 Putting a ! in front of a /command makes sure that the internal command is the one that gets executed. If there's some other script that has intercepted /echo /msg /sound etc, and you don't want to risk having some kind of theme script messing with the string, the ! prefix makes it ignore an alias of the same name as a built-in command. And it will be an error if you use !fupdate in an older version where fupdate did not exist as a built-in command.

Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
Additional info on this topic.

You may find this script not working as intended, due to a default setting that many other users may have. Someone came into the help channel with a problem, where he had a channel that he didn't see any color in it, but someone else sent a screenshot showing that the topic did have color.

It turns out that the default setting for mIRC is to hide colors from people if there's too many of them. In options/irc/messages "strip codes from incoming messages", the default settings for that row is to strip color and the other control codes if there's more than 50 of them in the message. This next command sends 50 characters, each in a different color, and under the default setting, other people will see all the colors.

//say $regsubex($str(x,50),/x/g,$chr(3) $+ $base(\n,10,10,2) $+ $chr($calc(64+ \n)))

But if you change the 50 to 51, that causes there to be 'more than 50 control codes', and they'll be stripped from the incoming message, and they'll see this as if no colors were used.

So that means your script works for short messages, but if the string is long enough that it needs 50 colors, sorry charlie frown

You won't see anything unusual in channel messages, but if you

//query $me

you'll see the colors stripped out of the message when the 51 is used instead of 50

--

I'm not so sure there's a need for this default setting anymore, because modern networks now give channels the tools to deal with colors if they consider them a problem. There's now channelmode +c that blocks the message if it contains any colors, and +S that allows the message after stripping the colors. So if the channel has either of these 2 messages, your script also fails.


Link Copied to Clipboard