Skip to main content

News

Topic: Link.js v0.4.2 (Read 95907 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.15
Reply #195

The only other issue I can think of is if the input string already contains '\0' characters to start with, but since the use case here is primarily text, that shouldn't crop up very often, if at all.  We're not parsing binary data after all. :)


In that case it was meant to prematurely end since '\0' is the standard for the end of text. So if someone does give me that as an issue I'll just say that they need to not use \0 as a delimiter or character in their text, since in C and other languages you should not do that. Still, I don't know if \0 will be a permanent fix, but I'd have to do a lot more retooling to handle the case that the query has ran the "last node". I could do it, but it'd take more code, more complexity, and this really was the best option for now.

At least I can finally do some fast text manipulations in link (which outside of Sphere I am using on various websites).
  • Last Edit: June 08, 2014, 07:01:59 pm by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.15
Reply #196
Could Link be used to tokenize a string?  For example, given the string "812>8", getting back as output [ "812", ">", "8" ].  This is something a normal split can't do, but if you're going for text manipulation as well as array manipulation, it might be a nice feature to have.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

Re: Link.js v0.2.15
Reply #197
Now you're getting C concerns in JS. Not sure if genius or insanity.

Also, I'm 100% sure there is a standard JS API already there to tokenize strings that is as old as the hills...or at least as old as the SM in Sphere 1.5.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.15
Reply #198
How is tokenization specifically a "C concern"? ???  As for there being a standard API, well, there's a standard API for string split as well...

I guess it is a bit of an edge case, though, and tokenizing a filtered string (Link's strong suit) doesn't really make sense.  Can't seem to find anything about a tokenizer API though, the only thing I can find is String:split, which doesn't tokenize.
  • Last Edit: June 08, 2014, 08:53:10 pm by Lord English
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

Re: Link.js v0.2.15
Reply #199
I was referring to worrying about NUL's in strings as a C concern. For C devs, a demon we must all face at some point.

I think it's string.substr, or something like that. It has a somewhat inobvious name.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.15
Reply #200
I was referring to the fact that Radnen is appending NULs to the end of strings internally as a sentinel.  however, that's a valid character to use in a string in JS, so if one is passed in already containing them, it could cause issues.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.15
Reply #201
I used Link to tokenize strings for a text-box module where I did some quick styling to letters (such as color, font, style etc.) And the filtering does come in handy if you want code-completion, but I doubt people will do that in Sphere or with the library, but hey, anything's possible.

Yes, it's certainly C-like to use \0 in a string. :P But giving the meaning of \0, I doubt people will casually use it.

Well, a use case for tokenizing I found recently for link under split is the ability to parse urls and file paths. So there's a good example that is often came across in real life.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Link.js v0.2.15
Reply #202
So you're saying that I shouldn't casually slip null characters into strings I'm planning on processing? Now how am I supposed to hide morse code encoded in null characters!

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.15
Reply #203

So you're saying that I shouldn't casually slip null characters into strings I'm planning on processing? Now how am I supposed to hide morse code encoded in null characters!


Haha, good one. :D  But yeah, basically this would only really prevent processing of binary data using Link's string functionality, which is an edge case at best.  Still something to keep in mind, though.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.15
Reply #204
So I was about to ask for a new feature earlier, Shuffle, that randomly re-orders the results of the query, another feature I needed for my AI-coding efforts.  But then I did some research and it turns out that not only is the basic Fisher-Yates shuffle very fast at O(n) complexity, but also drop-dead simple to implement.  So I implemented the feature myself. ;D

Radnen, check your GitHub page--there's a pull request waiting.

Usage:
Code: (javascript) [Select]
var spadeDeck = Link(cardPack)
    .filterBy('suit', 'spade')
    .shuffle();
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.15
Reply #205
Love it. Thank you dude!
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.15
Reply #206
...and in case anyone is curious what use a shuffle would be for a boss AI, well, here's my use case:

Code: (javascript) [Select]
var targets = Link(this.aic.battle.enemiesOf(this.aic.unit)).shuffle();
var combos = Link(Link(this.combos)
.where(function(combo) { return this.phase >= combo.phase; }.bind(this))
.random(targets.length))
.sort(function(a, b) { return b.rating - a.rating; });
this.tactics = [];
for (var i = 0; i < party.length; ++i) {
this.tactics.push({ moves: combos[i].moves, moveIndex: 0, unit: targets[i] });
}


The AI first asks the battle engine for the identities of all opposing party members, and shuffles that list using Link.  It then picks a number of random move combos equal to the number of opposing party members (@Radnen: This is why I lobbied for random() to be able to return duplicates!), again using a Link query, sorts them by descending rating, and finally builds a tactics table using the results of both queries.  Because the combos are sorted, this guarantees that the first entry in the tactics array is the most powerful, the "primary tactic" so to speak--a guarantee that simplifies other areas of the implementation.  However, it's important that the same unit isn't chosen to be the primary each time, which would end up being the case if not for the shuffle.
  • Last Edit: June 23, 2014, 09:00:48 pm by Lord English
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Link.js v0.2.15
Reply #207
I'm sure without Link, this would've been a bit harder for you to realize in code what you had in your head. :P

This all sounds great! This is exactly what I made the original Query library for, before it turned into this. :)
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.15
Reply #208

I'm sure without Link, this would've been a bit harder for you to realize in code what you had in your head. :P

This all sounds great! This is exactly what I made the original Query library for, before it turned into this. :)


Indeed it would have.  The two main areas where I've found Link to be invaluable are enemy AI and status effects.  Both are cases where the game needs to respond to oftentimes complex conditions while maintaining some semblance of consistent behavior, and without something like Link at my disposal, would quickly devolve into a convoluted tangle of if-elseif blocks, which would be a nightmare to maintain.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Link.js v0.2.15
Reply #209
Okay, maybe I'm just not using my imagination here, but I can't figure this one out.

Suppose I have a dataset like this:

Code: (javascript) [Select]
this.combos = [
{ phase: 1, moves: [ 'electrocute', 'heal' ], rating: 1 },
{ phase: 1, moves: [ 'hellfire', 'windchill' ], rating: 2 },
{ phase: 1, moves: [ 'windchill', 'hellfire' ], rating: 2 },
{ phase: 2, weaponID: 'powerBow', moves: [ 'flareShot', 'chillShot' ], rating: 2 },
// ...and so on
];


And I want to take a number of random items from that array such that:

  • All results have .phase <= bossPhase

  • No more than one result contains a weaponID



Is there a way I can construct a single query the meets both criteria?
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub