Archive

Monthly Archives: October 2011

There is a multiplayer server for realtime strategy game.
There is special Process thread to process behaviour of the game world.
It wake up every N milliseconds, do some changes in our object model (game world)
and then sleep for next N milliseconds.
Also, there are asynchronous network actions runned in another Socket thread.
So, the question is about synchronization.

Usually, I use ‘lock (object) { … }’ way to sync. work with data in object model.
There are a lot of locks, so, it needs to keep in mind
this multithreading nature of the server during thinking on the business logic.
As better approach, this time I implemented queue-based solution:
The Process thread may be in 3 modes:
    1) Process the world;
    2) Process the queue of collected actions;
    3) Do nothing.
Depends on the this mode, incoming Socket actions is processing like:
    1) Collected to special queue
    2) Waiting for #2 finish
    3) Process the action directly

Here is pseudocode of both methods (threads), where lock() has C# meaning.

ProcesssIncomingAction(action)
{
    lock (mode)
    {
        if (isProcess)
        {
            lock (queue)
            {
                queue.add(action);
            }
        } else {
            lock (process)
            {
                processAction(action);
            }
        }
    }
}

ProcessObjectModel()
{
    lock (mode)
    {
        isProcess := true;
    }
    lock (process)
    {
        _processObjectModel();
        lock (mode)
        {
            isProcess := false;
        }
        lock (queu)
        {
            processActions(queue);
        }
    }
}

As simpler desing, we can only collect actions in Socket thread
and process them only in the Process thread.
But such solution looks ugly.
In deed, we don’t need to transfer the action via queue,
we need to syncronize the process only.

Advertisements

If your XML has default name space like:
    <COLLADA xmlns=”http://www.collada.org/2005/11/COLLADASchema”></COLLADA&gt;
Then to use DOMXpath you should register this namespace with some prefix like:
    $xpath->registerNameSpace(‘dp’, ‘http://www.collada.org/2005/11/COLLADASchema&#8217;);
here ‘dp’ means default prefix;
Then use this prefix in your XPath query like:
    $nodes = $xpath->query(‘//fp:COLLADA’);

// real example
$DOMDocument = new DOMDocument;
$DOMDocument->strictErrorChecking = false;
//$sXml = ‘<COLLADA xmlns=”http://www.collada.org/2005/11/COLLADASchema”></COLLADA>&#8217;;
if (! $DOMDocument->loadXML($sXml))
    _error(“can’t parse XML”);

$xpath = new DOMXpath($DOMDocument);
$xpath->registerNameSpace(‘dp’, ‘http://www.collada.org/2005/11/COLLADASchema&#8217;);

foreach ($xpath->query(‘//dp:image/dp:init_from’) as $node)
{
    $node->nodeValue = $dir . “/model.jpg”;
}

$sXml = $DOMDocument->saveXML();

I’m working on multiplayer realtime strategy game. Today I implemented method for getting 3 units of the player which are nearst to the station creation point. See below. As you can see it’s regular problem of getting N min(max) elements of array. The task looks pretty regular, even standard. But I can’t say that such tasks are daily ones. And what about you?

                // get 3 units nearst to the x, y
                var distances = new int[StationCreationUnitsNumber];
                var units = new Unit[StationCreationUnitsNumber];
                var taken = 0;
                foreach (var unit in player.Units)
                {
                    var d = _distance2(unit.X, unit.Y, x, y);

                    // check new point against taken ones
                    for (var i = 0; i < taken; ++i)
                    {
                        // if new point should be taken
                        if (d <= distances[i])
                        {
                            // shift already taken points
                            for (var j = taken – 1; j > i; –j)
                            {
                                distances[j] = distances[j – 1];
                                units[j] = units[j – 1];
                            }

                            // take the point and jump to process next one
                            distances[i] = d;
                            units[i] = unit;
                            goto units_loop_end;
                        }
                    }

                    // take initial point anyway
                    if (taken < StationCreationUnitsNumber)
                    {
                        distances[taken] = d;
                        units[taken] = unit;
                        ++taken;
                    }

                units_loop_end:
                    ;
                }

<?php

// XML/XSL: Transform one tag into another
// You need to have php_xsl.dll in your php extenstions

function transform($xml, $xsl) {
   $xslt = new XSLTProcessor();
   $xslt->importStylesheet(new SimpleXMLElement($xsl));
   return $xslt->transformToXml(new SimpleXMLElement($xml));
}

$sXml  = ‘<xml><sudhir><image id=”model_jpg” name=”model_jpg”>
  <init_from>models/cersi_ships/model.jpg</init_from>
  </image></sudhir></xml>’;

$sXsl  = ‘<xsl:stylesheet
    xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221; version=”1.0″>
    <xsl:output method=”xml”/>

    <xsl:template match=”image/init_from”>
        <init_from2><xsl:value-of select=”.”/></init_from2>
    </xsl:template>

    <xsl:template match=”* | @*”>
        <xsl:copy><xsl:copy-of select=”@*”/><xsl:apply-templates/></xsl:copy>
    </xsl:template>
   
</xsl:stylesheet>’;

echo transform($sXml, $sXsl);

Yesterday I setup apache virtual host on my OSX computer. I’m going to setup web application based on ZF (Zend Framework) for further development. Fortunately, Mac already has Apache+PHP on board. So, I need only turn on apache, include php module in it and provide VirtualHosts.

The problem was with apache error ‘(13)Permission denied: access to’. That means that apache can get access to some folder/file.

But it was ok with permissions in that folder/file for apache user (_www in OSX). So, what’s wrong with it? 

In deed, apache needs reading or `chmod 755` permissions for all folders in the path to the destination folder/file. I found the folder and chmod it. For now, everything is just fine.

 

Today I was needed to check several hundreds of .fla files for existance of specified labels in movies timelines. I created jsfl script. See below. It loads all .fla files one by one, finds specified movie and checks its timeline.

fl.outputPanel.clear();

var dir = ‘file:///d|/Projects/civcity/flash/branches/stable/resources/fla/buildings/’;
var n = 442;

var re = new RegExp(“civcity.buildings.”, “”);

for (var f = 1; f <= n; ++f)
{
    var fileURI = dir + ‘b_’ + f + ‘.fla’;
    if (!fl.fileExists(fileURI))
        continue;
    fl.openDocument(fileURI);
    var items = fl.getDocumentDOM().library.items;
    for (var i=0; i < items.length; i++)
    {
        item = items[i];
        name = item.name;
        if (name.match(re))
        {
            if (item.symbolType == ‘movie clip’)
            {
                var hasPreview = false;
                var hasIdle = false;

                tim = item.timeline;
                for (var l = 0; l < tim.layers.length; ++l)
                {
                    var lay = tim.layers[l];
                    var labels = [];
                    for(var fr=0; fr<lay.frames.length;++fr)
                    {
                        if(lay.frames[fr].labelType == “name”)
                        {
                            if (lay.frames[fr].name == ‘Preview’)
                                hasPreview = true;
                            if (lay.frames[fr].name == ‘Idle’)
                                hasIdle = true;
                        }
                    }
                }

                if ((!hasPreview) || (!hasIdle))
                {
                    fl.trace(f);
                    fl.trace(name);
                }
            }
        }
    }
    fl.documents[0].close();
}

fl.trace(“Done”);