Kosmor Forum Index
 Search Imprint      SearchSearch     Log inLog in 
 Search Legal      MemberlistMemberlist     ProfileProfile   

Kosmor Scripts

 
Post new topic   Reply to topic    Kosmor Forum Index -> Questions and Answers
View previous topic :: View next topic  
Author Message
Locutus
Guest





PostPosted: Sun Feb 24, 2008 14:36    Post subject: Kosmor Scripts Reply with quote

Table of Contents
KTBOC
To navigate this rather long document, search the document for the search key (the string of 5 capital letters).


  • Table of Contents....................KTBOC
  • Instructions............................KINST
  • SVG Analyzer.........................KSVGA
  • Planet Population Calculator.....KPPOP
  • Changelog..............................KCHLG




INSTRUCTIONS:
KINST
To use any of these scripts, you need to have a PHP server installed on your computer. Apache is an excellent open-source server, and you can install it in less than 20 mins. After installing Apache, install PHP as well.

I will use this post to store the source code of each of these scripts until I can find cheap, reliable hosting.







KOSMOR SVG ANALYZER
KSVGA

Description
This is a rather simple tool that takes an SVG and outputs all the WP's on the SVG, in addition to its location, as well as whether it has jumped or carries ships. It also gives you a link to each WP and its position.

I have had this script for my own personal use for some time now, except it was hardcoded to find one anchor planet. Yesterday, I was really dumb and accidentally deleted it (along with the rest of my web stuff) while installing Ubuntu 8.04, so I rewrote the script from scratch and made it somewhat more user friendly.


Code:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
  <head>
<?php
  $sent_data = isset( $_POST["map_data"] );
  if ( $sent_data )
  {
?>
    <base href="http://www.kosmor.com/" />
<?php
  }
?>
    <title>Kosmor SVG Map Analyzer</title>
  </head>
 
 
  <body>
    <h1>Kosmor SVG Analyzer</h1>
<?php
 
  //if no data input - display form
  if( !$sent_data )
  {
?>
    <form action="" method="post">
      <div>
        <textarea name="map_data" rows="20" cols="60">Paste Map Source Here</textarea><br/>
        <input type="text" name="anchor_name" value="Planet Name" /><br/>
        <input type="text" name="anchor_x" value="X" /><br/>
        <input type="text" name="anchor_y" value="Y" /><br/>
        <input value="Submit" type="submit"/>
      </div>
    </form>
<?php
  }
 
 
 
  //else parse all of the input data
  else
  {

    //
    // GLOBAL VARIABLES
    //
   
    //flags
    $anchor_found = false;
    $wp = false;
    $no_ships = false;
    $jumped = false;
   
    //anchor stuff
    $anchor_name = "\n".$_POST["anchor_name"];
    $anchor_x = $_POST["anchor_x"] + 1;
    $anchor_y = $_POST["anchor_y"] - 1;
   
    //displacement of relative position with respect to given absolute position
    $dx;$dy;
   
    //temporary storage variables
    $x;$y;
   
   
   
   
    //
    // PARSE FUNCTION DECLARATIONS
    //
   
    //process the start tag of an element
    function start ( $parser, $tag, $attribs )
    {
     
      switch ( $tag ) //check what type of element it is
      {
        case "TEXT": //TEXT is either the name of a planet or wp
          global $anchor_found,$no_ships,$x,$y,$wp;
          if ( !$anchor_found ) //store location of planet if you have not found anchor
          {
            $x = $attribs["X"];
            $y = $attribs["Y"];
          }
         
          else //look for a wp
          {
            if ( $attribs["CLASS"] == "wpnameships" ) //if you find a WP with ships
              $wp = true;
           
            else if ( $attribs["CLASS"] == "wpname" ) //if you find a WP without ships
            {
              $wp = true;
              $no_ships = true;
            }
          }
         
          break;
       
       
        case "LINE": //LINE is the line of travel of the wp
       
          global $x,$y,$jumped;
         
          $x = $attribs["X2"];
          $y = $attribs["Y2"];
         
          $xmov = $x - $attribs["X1"];
          $ymov = $y - $attribs["Y1"];
         
          if ( ( $xmov * $xmov + $ymov * $ymov ) > 4000 ) $jumped = true;
         
          break;
         
      }
    }
   
    //process the data of an element
    function data ( $parser, $text )
    {
      global $anchor_found,$anchor_name,$wp,$x,$y,$dx,$dy;
      if ( !$anchor_found && $text == $anchor_name ) //if you find anchor
      {
        global $anchor_x,$anchor_y;
        $anchor_found = true;
        $dx = $anchor_x - $x;
        $dy = $anchor_y - $y;
      }
     
      else if ( $wp ) //if you find a wp, print out a link
      {
        global $no_ships,$jumped;
       
        $tx = $x + $dx;
        $ty = $y + $dy;
       
        echo "      <li><a href=\"?action=3000&amp;xpos=$tx&amp;ypos=$ty\">".$text."</a> (".$tx.",".$ty.")";
        if ( $jumped ) echo " (jumped)";
        if ( !$no_ships ) echo " (has ships)";
        echo "</li>\n";
       
        //reset flags
        $no_ships = false;
        $jumped = false;
        $wp = false;
      }
    }
   
    //parser 'stop' function - doesn't do anything in this situation
    function stop ( $parser, $data ) {}
   
   
   
   
    //
    // EXECUTION
    //
   
    //create parser, set parsing functions
    $parser = xml_parser_create();
    xml_set_element_handler( $parser, "start", "stop" );
    xml_set_character_data_handler( $parser, "data" );
   
    //parse input data
    echo "    <ul>\n";
    xml_parse( $parser, str_replace( "\\", "", $_POST["map_data"]) );
    echo "    </ul>\n";

    //free the parser
    xml_parser_free( $parser );
   
  }


?>

  </body>

</html>





PLANET POPULATION CALCULATOR
KPPOP

Description:
Basically, due to Kosmor's (and PHP's) integer rounding, it is possible to have such a large population on a planet that you lose no population even at 15% taxes.

The formula for population growth/decay is given on the second link (see below). If you do the calculations correctly, then you can calculate the population required to reach the 'sustainable' point:

Code:
P > 30*D - 250 / R (with leisure park)
P > 30*D - 100 / R (without leisure park)

P:= 'sustainable' population
D:= diameter of planet
R:= growth rate modifier from 0.6 to 1.6 (determined by the inhabitants of the planet)


For more information about this, see the following two threads:
http://www.kosmor.com/forum_int/viewtopic.php?t=9587
http://www.kosmor.com/forum_int/viewtopic.php?t=5034

Code:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <style type="text/css">
    .float
    {
      float:left;
    }
    div#result
    {
      border:1px solid #000000;
      margin-left:30px;
      padding:10px;
    }
    </style>
    <title>Kosmor Planet Population Calculator</title>
  </head>

  <body>
   
    <div>
      <h2>'Sustainable' Population Calculator</h2>
      <h4>Instructions:</h4>
      <p>
        Go to the <strong>"Overview"</strong> tab on the <strong>"Solar
        systems"</strong> menu and select all text (<code>CTRL+A</code> for most
        machines). Copy the text into the text box and click
        <strong>"Submit"</strong>.<br />
        <em>You can also copy information from more than one planet into the
        form.</em>
      </p>
    </div>
   
    <form class="float" action="" method="post">
      <div>
        <textarea cols="60" name="planetData" rows="20"></textarea><br />
        <input type="submit" value="Submit"/>
      </div>
    </form>
   
<?php

  //
  //START FUNCTION DECLARATIONS
  //
 
  //function for calculating the population, given name, diameter, growth rate, and whether or not the planet has a leisure park
  function outputPopulation ( $name, $diameter, $growthRate, $hasPark )
  {
    //if growthRate is invalid, you divide by zero
    //punish the user for user stupidity
    if ( !$growthRate ) return;
   
    //calculate population
    $pop = 30 * $diameter - ( $hasPark ? 25000 : 10000 ) / $growthRate;
   
    //output the planet name and the population
    return "      " . $name . ": " . ceil ( $pop ) . "<br />\n";
   
  }
 
  //function for parses the input string
  function parsePlanetData( $string )
  {
   
    //output
    $parsedString;

    //parsed variables - diameter, population, and leisure park
    $name;
    $diameter;
    $growthRate;
    $hasPark;
   
    //string parsing helpers
    //
    //very ugly coding, if anyone can write a more elegant version, it would
    //be greatly appreciated
    $isNameA=0;
    $isNameB=0;
    $isDiameter=0;
    $isGrowthA=0;
    $isGrowthB=0;
    $isPark=0;
   
    //splits string into array so you can parse each individual element
    $split_string = explode( " ", str_replace( array( "\n", "\r", "\t", "\v", "\f" ), " ", $string ) );
   
    //iterate through strings
    foreach ( $split_string as $data )
    {
   
      //if it's a valid string
      if ( $data )
        {
        //if the element is a valid integer, check if it matches diameter or growth rate
        if ( ctype_digit ( $data ) )
        {
          if ( $isDiameter ) //record the diameter
          {
            $diameter = $data;
            $isDiameter = 0;
          }
          else if ( $isGrowthB ) //record the growth rate
          {
            $growthRate = $data;
            $isGrowthB = 0;
          }
        }
       
        //if the data element is the name of the planet, record the planet name
        else if ( $isNameB )
        {
          $name = $data;
          $isNameB = 0;
        }
       
        //if the data element tells us whether or not the planet has a leisure park, record it
        //
        //this is the final data element that we need to collect - after it,
        //
        else if ( $isPark )
        {
          $hasPark = ( $data == "active");
          $isPark = 0;
         
          //add the individual planet's data to the output
          $parsedString .= outputPopulation ( $name, $diameter, $growthRate, $hasPark );
        }
       
        //else, check if the data element is one of the following strings
        else switch ( $data )
        {
          case "Solar": //"Solar" is the first part of the "Solar system:" string
            $isNameA = 1;
            break;
          case "system:": //if you detect "Solar system:", then the next string has to be the planet name
            $isNameB = $isNameA;
            $isNameA = 0;
            break;
          case "Diameter:": //if you detect "%Diameter:", then the next string has to be the diameter
            $isDiameter = 1;
            break;
          case "Growth": //"Growth" is the first part of the "Growth coefficient:" string
            $isGrowthA = 1;
            break;
          case "coefficient:": //if you detect "Growth coefficient:", then the next string has to be the growth coefficient
            $isGrowthB = $isGrowthA;
            $isGrowthA = 0;
            break;
          case "park:":
            $isPark = 1;
            break;
        }
      }
    }
   
    //return the output
    return $parsedString;
   
  }
 
  //
  //END FUNCTION DECLARATIONS
  //




  //
  //START EXECUTION
  //

  //executes the php code only if the user submitted some text
  if ( isset ( $_POST["planetData"] ) )
  {
    //parse the string
    $parsedData = parsePlanetData ( $_POST["planetData"]);
   
    //if the parsed string is valid, output it
    if ( $parsedData )
    {
      //start the new div
      echo "    <div class=\"float\" id=\"result\">\n      <strong>Result(s):</strong><br />\n";
     
      //spit out the parsed data
      echo $parsedData;
     
      //close the div
      echo "    </div>\n";
    }
    //else give an error
    else echo "    <div class=\"float\" id=\"result\">\n      <strong>Invalid Input</strong><br />\n    </div>\n";
   
  }
 
  //
  //END EXECUTION
  //
 
?>
  </body>
 
</html>



Changelog:
KCHLG
2008-2-27: it now works for non-pros as well, and is somewhat more readable.
2008-3-27: yet more readable
2008-4-26: added SVG analyzer, added Table of Contents, fixed up some stuff


Last edited by Locutus on Mon Apr 05, 2010 13:26; edited 9 times in total
Back to top
Locutus
Guest





PostPosted: Mon Feb 25, 2008 11:53    Post subject: Reply with quote

Soon I'll be posting some code for other scripts (all PHP) which might be very helpful. Like I said, I would like run these on my web server, but I'm not sure if it'll ever be functional again, and I don't have the funds to buy a new one. I may have to find hosting somewhere else.
Back to top
alphabravo
Major


Joined: 15 Feb 2004
Posts: 827
Location: UK

PostPosted: Tue Feb 26, 2008 12:06    Post subject: Reply with quote

Though i hate to say it, wouldn't that script be almost more useful written in javascript?

If you want I will happily go through and have a go. That way, it would be downloadable by the user, and then wouldn't require a webserver. Normally stuff written in javascript is skanky, but this time I think it would be feasible.
_________________
Major alphabravo in the house Ashera
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Locutus
Guest





PostPosted: Tue Feb 26, 2008 14:42    Post subject: Reply with quote

Yeah, normally I leave Javascript alone. I think you do have a point, though. All we actually need to do is parse a string, and it's not too difficult in this case.
Back to top
alphabravo
Major


Joined: 15 Feb 2004
Posts: 827
Location: UK

PostPosted: Tue Feb 26, 2008 21:27    Post subject: Reply with quote

note that you should get a different value for P for a pro and a non-pro (which you don't appear to get)

I think I'm going to rewrite that script in perl using the many scripts I already have, and just modifying them.
_________________
Major alphabravo in the house Ashera
Back to top
View user's profile Send private message Send e-mail MSN Messenger
alphabravo
Major


Joined: 15 Feb 2004
Posts: 827
Location: UK

PostPosted: Tue Feb 26, 2008 22:37    Post subject: Reply with quote

I just rewrote it, before realising that my maths might have been slightly wrong Razz

http://www.rebelstudentalliance.co.uk/cgi-bin/sustain.pl

now, i'm almost certain that there's an error in there somewhere, i'll check tomorrow against the correct growth formula
_________________
Major alphabravo in the house Ashera
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Aurion
Member of the House


Joined: 31 Jan 2004
Posts: 591
Location: Alpha Section

PostPosted: Wed Feb 27, 2008 13:01    Post subject: Reply with quote

There is an error, yes. My planet does not have any growth at 15%, although he does not exceed your calculated population.
_________________
Member of the House Aurion in the house Ashera


Last edited by Aurion on Wed Feb 27, 2008 16:03; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
alphabravo
Major


Joined: 15 Feb 2004
Posts: 827
Location: UK

PostPosted: Wed Feb 27, 2008 13:13    Post subject: Reply with quote

what do you expect for 4:30am? Razz
_________________
Major alphabravo in the house Ashera
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Locutus
Guest





PostPosted: Wed Feb 27, 2008 14:19    Post subject: Reply with quote

Hmm... I need to fix the pro/non-pro thing. However, I do like the fact that my script just allows you to select-all/paste the planet overview and process multiple planets, just for the really lazy users.

Edit: pro/non pro thing fixed
Back to top
Aurion
Member of the House


Joined: 31 Jan 2004
Posts: 591
Location: Alpha Section

PostPosted: Wed Feb 27, 2008 16:06    Post subject: Reply with quote

alphabravo wrote:
what do you expect for 4:30am? Razz


The same as usual. Nothing (: Wink
_________________
Member of the House Aurion in the house Ashera
Back to top
View user's profile Send private message Visit poster's website
alphabravo
Major


Joined: 15 Feb 2004
Posts: 827
Location: UK

PostPosted: Wed Feb 27, 2008 18:30    Post subject: Reply with quote

Locutus wrote:
just for the really lazy users.

I might pop that on mine, when i can get the maths right.

I've just been through the formula for growth that I had with paper and pencil, rearranging for the population which gives a growth of 1.
The problem is that I don't end up with the right thing :-\


[EDIT: It turned out I just had a slightly flawed formula for growth!]

Dear all with 0 growth planets, can you see if it works?
_________________
Major alphabravo in the house Ashera
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Paul_Atreides
Guest





PostPosted: Mon Oct 03, 2011 21:27    Post subject: Reply with quote

bump
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Kosmor Forum Index -> Questions and Answers All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group