JASS can't do that -or- The quine challenge

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
The title is the result of some (nearly) random MSN "talk".

The idea was to write a JASS function that, when called, would display its own source code.

Obviously, this can not be done.
Hence the following function that does it anyway:
JASS:
function F takes nothing returns nothing
local string s1="function F takes nothing returns nothing
local string s1="
local string s2="local string s2=
call DisplayTextToPlayer(GetLocalPlayer(),0,0,s1 + s + s1 + s + SubString(s2,16,17) + SubString(s2,0,16) + s + SubString(s2,0,1000) + s + SubString(s2,16,17) + SubString(s2,0,14) + SubString(s2,15,16) + s + e + s + s + SubString(s2,16,17) + SubString(s2,0,13) + SubString(s1,14,15) + SubString(s2,15,16) + s + e + e + s + SubString(s2,16,17) + SubString(s2,17,1000))
endfunction"
local string s="\""
local string e="\\"
call DisplayTextToPlayer(GetLocalPlayer(),0,0,s1 + s + s1 + s + SubString(s2,16,17) + SubString(s2,0,16) + s + SubString(s2,0,1000) + s + SubString(s2,16,17) + SubString(s2,0,14) + SubString(s2,15,16) + s + e + s + s + SubString(s2,16,17) + SubString(s2,0,13) + SubString(s1,14,15) + SubString(s2,15,16) + s + e + e + s + SubString(s2,16,17) + SubString(s2,17,1000))
endfunction


While the output on screen is somewhat prohibitive, "Message log" and a screenshot or two do, indeed, allow to verify that the function does display itself (including "s, \s and newlines).


In need of a challenge?
+20 rep. for a function that does the same, preferably shorter.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Well this doesn't really count, but "call BJDebugMsg(" is shorter than "call DisplayTextToPlayer(GetLocalPlayer(),0,0," is shorter (and less flexible :rolleyes:).

But here it is anyway :p

JASS:
function F takes nothing returns nothing
local string s1="function F takes nothing returns nothing
local string s1="
local string s2="local string s2=
call BJDebugMsg(s1 + s + s1 + s + SubString(s2,16,17) + SubString(s2,0,16) + s + SubString(s2,0,999) + s + SubString(s2,16,17) + SubString(s2,0,14) + SubString(s2,15,16) + s + e + s + s + SubString(s2,16,17) + SubString(s2,0,13) + SubString(s1,14,15) + SubString(s2,15,16) + s + e + e + s + SubString(s2,16,17) + SubString(s2,17,999))
endfunction"
local string s="\""
local string e="\\"
call BJDebugMsg(s1 + s + s1 + s + SubString(s2,16,17) + SubString(s2,0,16) + s + SubString(s2,0,999) + s + SubString(s2,16,17) + SubString(s2,0,14) + SubString(s2,15,16) + s + e + s + s + SubString(s2,16,17) + SubString(s2,0,13) + SubString(s1,14,15) + SubString(s2,15,16) + s + e + e + s + SubString(s2,16,17) + SubString(s2,17,999))
endfunction


I also used 999 instead of 1000 to make it a few characters shorter.

Cool idea. :p
 

Vestras

Retired
Reaction score
248
JASS:
function F takes nothing returns nothing
local string s1="function F takes nothing returns nothing
local string s1="
local string s2="local string s2=
call DisplayTextToPlayer(GetLocalPlayer(),0,0,s1 + s + s1 + s + SubString(s2,16,17) + SubString(s2,0,16) + s + SubString(s2,0,1000) + s + SubString(s2,16,17) + SubString(s2,0,14) + SubString(s2,15,16) + s + s + s + s + s + SubString(s2,16,17) + SubString(s2,0,13) + SubString(s1,14,15) + SubString(s2,15,16) + s + s + s + s + s + s + SubString(s2,16,17) + SubString(s2,17,1000))
endfunction"
local string s="\""
call DisplayTextToPlayer(GetLocalPlayer(),0,0,s1 + s + s1 + s + SubString(s2,16,17) + SubString(s2,0,16) + s + SubString(s2,0,1000) + s + SubString(s2,16,17) + SubString(s2,0,14) + SubString(s2,15,16) + s + s + s + s + s + SubString(s2,16,17) + SubString(s2,0,13) + SubString(s1,14,15) + SubString(s2,15,16) + s + s + s + s + s + s + SubString(s2,16,17) + SubString(s2,17,1000))
endfunction


One line shorter.
 

Sooda

Diversity enchants
Reaction score
318
Might be cheesy, but AceHart made his first string like that, displays source code of function:
JASS:
function F takes nothing returns nothing
    call DisplayTextToPlayer(GetLocalPlayer(),0,0, "function F takes nothing returns nothing
    call DisplayTextToPlayer(GetLocalPlayer(),0,0, \"\")
endfunction")
endfunction

EDIT:
It just seems wrong, I fail to see how Ace code works. My function source code is:
JASS:
function F takes nothing returns nothing
    call DisplayTextToPlayer(GetLocalPlayer(),0,0, "")
endfunction

I'm confused.
 

Builder Bob

Live free or don't
Reaction score
249
Slightly different, but much the same

JASS:
function F takes nothing returns nothing
local string s="function F takes nothing returns nothing
local string s=\"\\
call DisplayTextToPlayer(GetLocalPlayer(),0,0,SubString(s,0,56)+SubString(s,56,57)+SubString(s,0,56)+SubString(s,57,58)+SubString(s,56,57)+SubString(s,57,58)+SubString(s,57,58)+SubString(s,58,59)+SubString(s,59,347)+SubString(s,56,57)+SubString(s,58,59)+SubString(s,59,347))
endfunction"
call DisplayTextToPlayer(GetLocalPlayer(),0,0,SubString(s,0,56)+SubString(s,56,57)+SubString(s,0,56)+SubString(s,57,58)+SubString(s,56,57)+SubString(s,57,58)+SubString(s,57,58)+SubString(s,58,59)+SubString(s,59,347)+SubString(s,56,57)+SubString(s,58,59)+SubString(s,59,347))
endfunction
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
> One line shorter.

In case you missed it, the idea was for that "better" version to still work correctly.


> Might be cheesy

As long as it works...
Yours doesn't.


> Slightly different, but much the same

Clearly. But it's good nonetheless.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
> No idea if it lets to declare the global string like that...

That's not strictly a function anymore...
And, either way, if it needs a global block, that block is part of the code and must be displayed as well. Currently, it doesn't.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,694
Question:

Why can't we just use 1 function, and type the whole code in a string variable?

EDIT: I copy-pasted and tested the JASS code Acehart posted, and it failed to trigger in WC3.

EDIT2: Waaait a minute...
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
> it failed to trigger

It's not a trigger, just a function.

I used this to call it, automatically and on command if needed:
Trigger:
  • Call
    • Events
      • Time - Elapsed game time is 1.00 seconds
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Custom script: call F()


With the function in the header.


> ... Bob's

You can cut down on the SubString calls:
JASS:
function F takes nothing returns nothing
local string s="function F takes nothing returns nothing
local string s=\"\\
call DisplayTextToPlayer(GetLocalPlayer(),0,0,SubString(s,0,56) + SubString(s,56,57) + SubString(s,0,56) + SubString(s,57,58) + SubString(s,56,57) + SubString(s,57,58) + SubString(s,57,58) + SubString(s,58,999) + SubString(s,56,57) + SubString(s,58,999))
endfunction"
call DisplayTextToPlayer(GetLocalPlayer(),0,0,SubString(s,0,56) + SubString(s,56,57) + SubString(s,0,56) + SubString(s,57,58) + SubString(s,56,57) + SubString(s,57,58) + SubString(s,57,58) + SubString(s,58,999) + SubString(s,56,57) + SubString(s,58,999))
endfunction


Still the best so far... :p
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
Reason? Good? What?

- Fun?
- Because you can?
- Keep people busy?
- Learn something new today?
- Because there's always a better way to do it?
- Because!
 

Builder Bob

Live free or don't
Reaction score
249
Doing things purely with the reason that we can, is always fun.

I would like to ask a little question. When you say "preferably shorter", do you mean shorter in lines of code, characters used, processing power required/function calls used, amount of text printed in-game, something different, or a combination of these?
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
Well, I don't really see replacing DisplayTextToPlayer with BJDebugMsg as an improvement.
It's shorter by characters, but computationally more intensive as it calls DisplayTextToPlayer anyway (in a loop to all players)...

Though, less strings, less function calls and less string concatenations... that's an actual improvement.
And helps readability, both the code and in game.

Even better would be a solution that uses a somewhat different method.
 

Builder Bob

Live free or don't
Reaction score
249
Though, less strings, less function calls and less string concatenations... that's an actual improvement.
And helps readability, both the code and in game.

I guess everyone can judge for themselves then. Less strings can lead to more functions calls, and visa versa.

Here's a more modular approach which doesn't depend on substring positions. It doesn't display well in-game without using the log, and it's longer in some ways, but it was fun making it.
JASS:
function F takes nothing returns nothing
local string s="
local string s"
local string s1="function F takes nothing returns nothing"
local string s2="
call DisplayTextToPlayer(GetLocalPlayer(),0,0,s1+s+s3+s4+s+s4+s+I2S(1)+s3+s4+s1+s4+s+I2S(2)+s3+s4+s2+s4+s+I2S(3)+s3+s4+s3+s4+s+I2S(4)+s3+s4+s5+s4+s4+s+I2S(5)+s3+s4+s5+s5+s4+s2)
endfunction"
local string s3="="
local string s4="\""
local string s5="\\"
call DisplayTextToPlayer(GetLocalPlayer(),0,0,s1+s+s3+s4+s+s4+s+I2S(1)+s3+s4+s1+s4+s+I2S(2)+s3+s4+s2+s4+s+I2S(3)+s3+s4+s3+s4+s+I2S(4)+s3+s4+s5+s4+s4+s+I2S(5)+s3+s4+s5+s5+s4+s2)
endfunction
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,694
So that's how you call for the function in a trigger like that...

Reason? Good? What?

- Fun?
- Because you can?
- Keep people busy?
- Learn something new today?
- Because there's always a better way to do it?
- Because!

You're getting hyperactive here.
 

Builder Bob

Live free or don't
Reaction score
249
> ... Bob's

You can cut down on the SubString calls:
JASS:
function F takes nothing returns nothing
local string s="function F takes nothing returns nothing
local string s=\"\\
call DisplayTextToPlayer(GetLocalPlayer(),0,0,SubString(s,0,56) + SubString(s,56,57) + SubString(s,0,56) + SubString(s,57,58) + SubString(s,56,57) + SubString(s,57,58) + SubString(s,57,58) + SubString(s,58,999) + SubString(s,56,57) + SubString(s,58,999))
endfunction"
call DisplayTextToPlayer(GetLocalPlayer(),0,0,SubString(s,0,56) + SubString(s,56,57) + SubString(s,0,56) + SubString(s,57,58) + SubString(s,56,57) + SubString(s,57,58) + SubString(s,57,58) + SubString(s,58,999) + SubString(s,56,57) + SubString(s,58,999))
endfunction


Still the best so far... :p

Cut down the number of SubString calls even further.
JASS:
function F takes nothing returns nothing
local string s="function F takes nothing returns nothing
local string s=\"\\
call DisplayTextToPlayer(GetLocalPlayer(),0,0,SubString(s,0,57) + SubString(s,0,56) + SubString(s,57,58) + SubString(s,56,58) + SubString(s,57,347) + SubString(s,56,57) + SubString(s,58,347))
endfunction"
call DisplayTextToPlayer(GetLocalPlayer(),0,0,SubString(s,0,57) + SubString(s,0,56) + SubString(s,57,58) + SubString(s,56,58) + SubString(s,57,347) + SubString(s,56,57) + SubString(s,58,347))
endfunction
 

quraji

zap
Reaction score
144
AceHart..tsk. Trying to be sneaky with that edit?

At least you can delete that part of my post so I don't look like a fool :p
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top