Skip to content

Enhanced extension: Complexify!.js #2113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

Kenay555
Copy link

@Kenay555 Kenay555 commented May 9, 2025

A better extension for complex numbers, fixing all the issues with the original Complexity! See the first one at #2091
More motion, vectors, decimals and factorials**********!

*[Somehow not working]

A better extension for complex numbers, fixing all the issues with the original Complexity!
@github-actions github-actions bot added the pr: new extension Pull requests that add a new extension label May 9, 2025
@Kenay555
Copy link
Author

Kenay555 commented May 9, 2025

What do you think, @Brackets-Coder and @yuri-kiss?

Copy link
Contributor

@Brackets-Coder Brackets-Coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking you should try to follow formatting best practices so your code doesn't look cluttered. Additionally, try to avoid opening new pull requests when it isn't necessary and you could just update the original :)

@@ -0,0 +1,1012 @@
// Name: Complexify!
// ID: kenayComplexify
// Description: Complex Numbres in TurboWarp! Can you believe it?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description has typos and should be more professional, targeted towards users. try something like "Complex Numerical Operations in TurboWarp." or something idk

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

'use strict';
//Just in case:
if (!Scratch.extensions.unsandboxed) {
alert("We don't like sand");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice allusion to Anakin Skywalker's famous opinion, but generally you should avoid alerts and just throw the error

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i'll avoid alerts. also who's Anakin Skywalker?

Copy link
Contributor

@Brackets-Coder Brackets-Coder May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i'll avoid alerts. also who's Anakin Skywalker?

I'm just going to assume you've never watched the masterpiece of Star Wars

}
}

function jsCode() { /**Again, thanks Rawify*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, you're putting the library (which is already inside an Immediately Invoked Function Expression) inside a function that is just executed elsewhere, and you're also re-declaring "use-strict" again which is totally redundant. Why can't you just minify the library and put it inside the class constructor?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're saying i can just insert the complex code in the constructor?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're saying i can just insert the complex code in the constructor?

That's how JS works, if you're just executing the function once inside the try catch block than might as well not have the function and reduce line count

Comment on lines 371 to 375
{
filter: [Scratch.TargetType.SPRITE], //Just in case
blockType: Scratch.BlockType.LABEL,
text: Scratch.translate("Motion"),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this section seems to be restricted to sprites, but let's check to make sure you're also filtering the block code so it doesn't return an error if these blocks are dragged from a sprite into the backdrop

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to do that, but i'll try

Copy link
Contributor

@Brackets-Coder Brackets-Coder May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to do that, but i'll try

You're correctly filtering the block pallet in the stage, but the sprite-exclusive blocks can be dragged into the stage and may cause errors. You should check to see if the block's target is the stage, it works like this:

blockOpcode({ Arg1, Arg2 }, { target }) {
   console.log(target);
}

just console log target and you'll see how to detect the stage

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i'll if (util.target.isStage) return; to each movement block

Comment on lines 411 to 429
filter: [Scratch.TargetType.SPRITE],
opcode: 'goToPolar', //A block no one asked for, and I fear no one needs
blockType: Scratch.BlockType.COMMAND,
text: 'Go polar [RADII] ∠ [ANGLY]',
arguments: {
RADII: { type: Scratch.ArgumentType.STRING, defaultValue: 50 },
ANGLY: { type: Scratch.ArgumentType.STRING, defaultValue: '0.9272952180016123' }
},
},
{
filter: [Scratch.TargetType.SPRITE],
opcode: 'glideComplex', //My favourite block [I love it]
blockType: Scratch.BlockType.COMMAND,
text: 'Glide [SECS] secs to [COMPLEX]',
arguments: {
COMPLEX: { type: Scratch.ArgumentType.STRING, defaultValue: '30+40i' },
SECS: { type: Scratch.ArgumentType.NUMBER, defaultValue: 1 }
},
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the offhand comments here really necessary? I think they just distract from the code

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, just some nice details. i'll delete them

Comment on lines 588 to 593
/**
We'll use toString() to return the Complex number with math notation.
If you wonder why, remember Complex is a class, and hence, returns objects.
So, we don't want "{re: -5, im: 1}", "[-5,1]" or "[object Object]". We want "-5+i" as is
Thus, no Scratch.Cast.toString() or anything like that, because toString() will always do.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask why this is the case? If everything is returned as objects then shouldn't you just parse them and return their properties instead of the whole object? The reason Scratch.Cast.toString() exists is because scratch has weird quirks that it has to account for which the normal javascript toString doesn't

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.toString can be used here, we know the value it returns is going to be a string cause it's toString, and since it's not a user inputted object (it's created from user input but that doesn't matter in this case) we don't have to care about using Cast.toString, and in fact Cast.toString would just make this code slower than it already is when returning due to the slight overhead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

casting the inputs on the other hand is a different thing entirely and should be done whenever the input accepts dynamic or possible differing of type user inputs, in this case Cast.toString SHOULD be used before passing the value to Complex, even if the try catch exists it is good practice.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the tip, @yuri-kiss! also @Brackets-Coder, the true reason we used .toString() is because no other function will output the strings we want

Comment on lines 829 to 859
glideComplex (args, util) { //Do you recognize this? Answer at the end!
if (util.stackFrame.timer) {
const timeElapsed = util.stackFrame.timer.timeElapsed();
if (timeElapsed < util.stackFrame.duration * 1000) {
// We've moving! And we'll move again.
const frac = timeElapsed / (util.stackFrame.duration * 1000);
const dx = frac * (util.stackFrame.endX - util.stackFrame.startX);
const dy = frac * (util.stackFrame.endY - util.stackFrame.startY);
util.target.setXY(util.stackFrame.startX + dx, util.stackFrame.startY + dy);
util.yield();
} else {
// We're done! Now, lets end this
util.target.setXY(util.stackFrame.endX, util.stackFrame.endY);
}
} else {
// We're starting! So, new Timer!
util.stackFrame.timer = new Timer();
util.stackFrame.timer.start();
util.stackFrame.duration = args.SECS;
util.stackFrame.startX = util.target.x;
util.stackFrame.startY = util.target.y;
util.stackFrame.endX = Complex(args.COMPLEX).re; //A little edit
util.stackFrame.endY = Complex(args.COMPLEX).im;
if (util.stackFrame.duration <= 0) {
// We can't glide -1 seconds, can we?
util.target.setXY(util.stackFrame.endX, util.stackFrame.endY);
return;
}
util.yield();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems really unoptimized

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea i just copied the code from Scratch-vm because we didn't know how to glide it ourselves

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even asked ChatGPT, we couldn't find any optimization

Comment on lines 878 to 896
convertComplex({ ANGLE, TOSMTH }) {
try {
if (ANGLE == "") {
return 0;
}
const cInstance = Complex(ANGLE);
switch (TOSMTH) {
case 'degs to rads': if (cInstance.im == 0) return (cInstance.re * 0.017453292519943295) % twoPi;
return cInstance.mul(0.017453292519943295).toString(); break;
case '𝜋': return cInstance.mul(3.141592653589793).toString(); break;
case 'rads to degs': if (cInstance.im == 0) return (cInstance.re * 57.29577951308232) % 360;
return cInstance.mul(57.29577951308232).toString(); break;
default: return NaN
}
} catch (e) {
console.log(e);
return 0;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my gosh the formatting is crazy I'll see if I can fix it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not suppoded to happen
image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just learn I can !format

@Brackets-Coder
Copy link
Contributor

!format

@yuri-kiss yuri-kiss marked this pull request as draft May 9, 2025 03:21
@yuri-kiss
Copy link
Member

This is riddled with licensing issues, poor code formatting, horrible practices and lots of spelling mistakes, I am making this a draft until further notice...

Kenay555 added 2 commits May 9, 2025 13:31
I'll add a better glideComplex, more Scratch.Cast and the minified code later. For now, small changes first
Still working on some thing though. I'll tell you when it's ready.
@Kenay555
Copy link
Author

Kenay555 commented May 10, 2025

Generally speaking you should try to follow formatting best practices so your code doesn't look cluttered. Additionally, try to avoid opening new pull requests when it isn't necessary and you could just update the original :)

@Brackets-Coder, I'm new to JS and GitHub and that sort of stuff. I don't know a lot of things. Some things, but not all of them. I didn't knew that you could edit pull-requesting files until recently, after I created this new pull request.
But don't take me like an ignorant either. I may be learning English and JS, but I learned enough to understand what you're saying. I'll always try my best to reach my goal, even if it takes me a new set of rules to learn.
So don't expect me to be a perfect coder, nor be a random guy that has a laptop and asks ChatGPT and other coders to do all work for him. Just wanting to point that out.
Also, @yuri-kiss, what's wrong about the licence? ClickerTale_2 used MPL-2.0, as reccomended by the CONTRIBUTING.md,

This is riddled with licensing issues, poor code formatting, horrible practices and lots of spelling mistakes, I am making this a draft until further notice...

And which are the horrible practices? Now that I know how to edit files, maybe I can correct Complexify!.js

@CubesterYT
Copy link
Member

!format

@Brackets-Coder
Copy link
Contributor

Generally speaking you should try to follow formatting best practices so your code doesn't look cluttered. Additionally, try to avoid opening new pull requests when it isn't necessary and you could just update the original :)

@Brackets-Coder, I'm new to JS and GitHub and that sort of stuff. I don't know a lot of things. Some things, but not all of them. I didn't knew that you could edit pull-requesting files until recently, after I created this new pull request. But don't take me like an ignorant either. I may be learning English and JS, but I learned enough to understand what you're saying. I'll always try my best to reach my goal, even if it takes me a new set of rules to learn. So don't expect me to be a perfect coder, nor be a random guy that has a laptop and asks ChatGPT and other coders to do all work for him. Just wanting to point that out. Also, @yuri-kiss, what's wrong about the licence? ClickerTale_2 used MPL-2.0, as reccomended by the CONTRIBUTING.md,

This is riddled with licensing issues, poor code formatting, horrible practices and lots of spelling mistakes, I am making this a draft until further notice...

And which are the horrible practices? Now that I know how to edit files, maybe I can correct Complexify!.js

Absolutely not trying to be critical, we all were there once and it was only recently (in the past few months) that I really started with Github. It's an understandable situation, I'm just here to try to help you through it.

@Kenay555 Kenay555 marked this pull request as ready for review May 10, 2025 17:54
@Kenay555
Copy link
Author

Kenay555 commented May 21, 2025

!format (heard these fix smth idk)

Copy link

The formatting bot didn't find any formatting issues. It currently only checks the extensions folder. The author or a maintainer can run terminal command 'npm run format' manually to format all files.

Yep. MORE THINGIES. Thanks to every error you've found, it's better than ever. Can't wait to see it at the gallery!
@Kenay555
Copy link
Author

Kenay555 commented May 21, 2025

!format (for the update. two done, one to go)

Copy link

The formatting bot didn't find any formatting issues. It currently only checks the extensions folder. The author or a maintainer can run terminal command 'npm run format' manually to format all files.

@Kenay555
Copy link
Author

!format

Copy link

The formatting bot didn't find any formatting issues. It currently only checks the extensions folder. The author or a maintainer can run terminal command 'npm run format' manually to format all files.

@Kenay555
Copy link
Author

Kenay555 commented May 22, 2025

After half an hour hours of coding, I got it. No more untranslated strings, no more wierd formatting, no more distracting comments, and no more horrible practices. Only Complexify!, no more.

Thanks @Brackets-Coder for telling me where I could be better.
Thanks @yuri-kiss for telling me some tips and in what I was wrong.
Thanks @CubesterYT and @CubesterYT for just passing by.
And thanks @Clickertale2 for being always there, always helping.

Fixing such a wierd extension is truly hard, but enhancing #2091 was really worth it.
All three checks are done, all three green successful checks.
I'm not sure what's next, do I just keep waiting? Do I start making the image? Or did it ended before its start?

What now?

(also i thought !format just meant my formatting wasn't good, my bad)

Copy link

The formatting bot didn't find any formatting issues. It currently only checks the extensions folder. The author or a maintainer can run terminal command 'npm run format' manually to format all files.

@Brackets-Coder
Copy link
Contributor

For now, we'll go through the next round of reviews before merging just to make sure everything is perfect and add some policing details before it gets merged. It's going to be really nitpicky and specific and detailed so just warning you ahead of time if some requests seem odd.

Additionally, we strongly recommend a gallery thumbnail, so if you haven't started making that you should do that now or generate one with a tool of your choice (assuming proper attribution and credit of sources). If you like, you can also write documentation and add sample projects, but these aren't required.

@CubesterYT
Copy link
Member

Also make sure the extension still works the same after all those changes

@Kenay555
Copy link
Author

@CubesterYT: Also make sure the extension still works the same after all those changes

It does! Maybe I'll edit it in the future due to silly factorial issues (of course, I set hideFromPalette: true to every factorial-related), but outside of that it's safe to use. I tested it, and if it doesn't- please tell me!

[I FORGOT THE LICENCE THINGIE]
@Kenay555
Copy link
Author

A teeny-tiny change just for it to be better [bet you won't notice] but it's not like the !format is wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: new extension Pull requests that add a new extension
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants