Skip to main content

News

Topic: Personlib 1.0 (Read 350 times) previous topic - next topic

Personlib 1.0
I finally got around to completing my Personlib library that I mentioned a while ago. The syntax is almost identical to the Sphere 1.x, with some changes to account for miniSphere's differences. It also makes good use of miniSphere's threading capabilities, and adds some convenience functions of its own. For example:

Code: (js) [Select]
// Cellscript.mjs 
transpile('@/lib', files('lib/personlib.mjs'));

// main.mjs
import { Person } from "personlib";

var player = new Person("player","player.rss",false); // same arguments as CreatePerson
player.customUpdate = function() {
    this.attachInput();
    this.attachCamera();
    var adjacent = this.getAdjacentObstructions(5); // where 5 is the offset in pixels
    var rand_r = Math.floor((Math.random() * 255));
    var rand_g = Math.floor((Math.random() * 255));
    var rand_b = Math.floor((Math.random() * 255));
    var rand_a = Math.floor((Math.random() * 255));
    this.setMask(new Color(rand_r, rand_g, rand_b, rand_a)); // setMask detects if the parameter is a miniSphere color object or a 1.x color object, and getMask allows you specify which one you want
    if(adjacent.north.tile > -1 || adjacent.north.person != "") Sphere.abort(" You are ded. RIP in pieces.");
    else this.queueCommand(COMMAND_MOVE_NORTH, true);
}
return true;
Since thread.create requires an update function and a render function, the Person object's update and render functions just call customUpdate and customRender respectively and return whatever customUpdate/customRender returns.
Most of the object methods are just call the legacy function given the appropriate arguments, so if you run into issues, there's a decent chance that it's miniSphere's fault. If you suspect that isn't the case or you have some suggestions, let me know.
  • Last Edit: August 08, 2017, 10:02:53 pm by Eggbert

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
Re: Personlib 0.5
Reply #1
Tip: If using ES6 with Cell:

Code: (javascript) [Select]

const { Person } = require('personlib');
var someone = new Person("some guy", 'guy.rss', false);
// etc...


Or .mjs module syntax:
Code: (javascript) [Select]

// at top of file
import { Person } from 'personlib';


edit: Wow, REALLY?  iOS 11 uses smart quotes?  I used to like iOS because it was much easier to type code with compared to Android, but the replacement with smart quotes kind of ruins it...
  • Last Edit: July 19, 2017, 01:05:19 pm by Fat Cerberus
miniSphere 5.0b2 (stable: 4.8.8) - Cell compiler - SSj debugger - thread | on GitHub
For the sake of our continued health I very much hope that Fat Cerberus does not become skilled enough at whatever arcane art it would require to cause computers to spawn enormous man-eating pigs ~Rhuan

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Personlib 0.5
Reply #2
This looks really useful! The adjacent stuff can be useful for stuff like platform movement too. :)

Re: Personlib 0.5
Reply #3

Tip: If using ES6 with Cell:

Code: (javascript) [Select]

const { Person } = require('personlib');
var someone = new Person("some guy", 'guy.rss', false);
// etc...


Or .mjs module syntax:
Code: (javascript) [Select]

// at top of file
import { Person } from 'personlib';



I see, I've never really used Cell, but I should probably make use of that.


This looks really useful! The adjacent stuff can be useful for stuff like platform movement too. :)

The example has the COMMAND_MOVE_NORTH code because I originally started working on the library for while working on a SHMUP, but I had platforming in mind as well.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
Re: Personlib 0.5
Reply #4
@Eggbert: By the way, Sphere v2 Color has components in [0,1] range (floating point), not (0,255) like v1.  That was done for consistency with GLSL, since miniSphere supports shaders.
miniSphere 5.0b2 (stable: 4.8.8) - Cell compiler - SSj debugger - thread | on GitHub
For the sake of our continued health I very much hope that Fat Cerberus does not become skilled enough at whatever arcane art it would require to cause computers to spawn enormous man-eating pigs ~Rhuan

Re: Personlib 0.5
Reply #5
Did the old Color object require an integer or did it allow floating point numbers?

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
Re: Personlib 0.5
Reply #6
It might have been integer before miniSphere 4.0, because that was the first version that was "Sphere v2" proper.  Before that the constructors were just a convenience to make v1-compatible objects.
miniSphere 5.0b2 (stable: 4.8.8) - Cell compiler - SSj debugger - thread | on GitHub
For the sake of our continued health I very much hope that Fat Cerberus does not become skilled enough at whatever arcane art it would require to cause computers to spawn enormous man-eating pigs ~Rhuan

Re: Personlib 0.5
Reply #7
I meant the v1 Color objects. Given most graphics programs use integers for the RGBA values, should it be safe to limit it to that?

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
Re: Personlib 0.5
Reply #8
v1 Color is integer-only, if you try to pass floats you'll just get black.  There's no way to distinguish the two in a constructor because in JS all numbers are IEEE double per the language definition.

As for why v2 uses float colors, it's because GLSL shaders also do (colors are passed as vec4) and it would be confusing for users just learning about shaders if colors are float in the shader but integer in Sphere.  So it's just a matter of consistency.
miniSphere 5.0b2 (stable: 4.8.8) - Cell compiler - SSj debugger - thread | on GitHub
For the sake of our continued health I very much hope that Fat Cerberus does not become skilled enough at whatever arcane art it would require to cause computers to spawn enormous man-eating pigs ~Rhuan

Re: Personlib 0.5
Reply #9
I was experimenting a little on jsfiddle and I figured out how to easily convert between the two without any number type issues (as far as I know)

Code: (JavaScript) [Select]

function oldToNew(o) {
   alpha = 1;
   if(o.alpha) alpha = o.alpha;
   return new Color(
      Math.round(o.red * 100) / 100,
      Math.round(o.green * 100) / 100,
      Math.round(o.blue * 100) / 100,
      Math.round(alpha * 100) / 100
   );
}

function newToOld(n) {
    var alpha = 255;
    if(n.a) alpha = o.a;
    return CreateColor(
        Math.floor(n.r * 255),
        Math.floor(n.g * 255),
        Math.floor(n.g * 255),
         Math.floor(alpha * 255);
    );
}
  • Last Edit: July 19, 2017, 08:48:32 pm by Eggbert

Re: Personlib 0.6
Reply #10
Is it possible to detect whether or not a person will be destroyed with the map? I'm creating a function that will return an array of person objects based on the people already in the map, given an array of names to not include. If it isn't, does the map engine set destroy_with_map by default?

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
Re: Personlib 0.6
Reply #11
All persons defined in the map file are transient and get destroyed on a map change:
https://github.com/fatcerberus/minisphere/blob/v4.7.0/src/minisphere/map_engine.c#L1084-L1085
(note false is passed here, because the meaning of the flag is reversed internally)

The only way to have destroy_with_map == true is to do it yourself, using CreatePerson().  I don't think there's any way to detect it programmatically, though, at least not without comparing the results of GetPersonList() before and after a map change.

edit: Hm, your v1/v2 color conversion code is still wrong.  It's actually a lot easier than what you have:
Code: (javascript) [Select]

v2Color = new Color(v1c.red / 255, v1c.green / 255, v1c.blue / 255, v1c.alpha / 255);
v1Color = CreateColor(v2c.r * 255, v2c.g * 255, v2c.b * 255, v2c.a * 255);


You don't have to check whether the alpha channel exists, because it always will.  And Math.round(o.red * 100) / 100 just gives you back the original value.
  • Last Edit: July 22, 2017, 10:37:24 am by Fat Cerberus
miniSphere 5.0b2 (stable: 4.8.8) - Cell compiler - SSj debugger - thread | on GitHub
For the sake of our continued health I very much hope that Fat Cerberus does not become skilled enough at whatever arcane art it would require to cause computers to spawn enormous man-eating pigs ~Rhuan

Re: Personlib 1.0
Reply #12
Alright, Personlib is now at 1.0 and should be pretty stable. I'm working on a game called MonoBomber, and it uses it without any issues. The GeneratePersonObjects function returns an array of Person objects from persons already loaded in the map, given an include list of specific persons to use, or an exclude list of specific persons not to use (like the player's object, for example). GeneratePersonObjects() is mostly complete, but I wouldn't depend on it quite yet. if the Person() force parameter is set to true, you can set the spriteset to an empty string, and it will assume that the person already exists.