Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2001:
[Freeciv-Dev] Re: Development Strategies [Was Documentation, Usability a
Home

[Freeciv-Dev] Re: Development Strategies [Was Documentation, Usability a

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv development list <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: Development Strategies [Was Documentation, Usability and Development]
From: Reinier Post <rp@xxxxxxxxxx>
Date: Tue, 4 Dec 2001 17:56:47 +0100

On Mon, Dec 03, 2001 at 04:13:41PM +0100, Gregor Zeitlinger wrote:
> On Mon, 3 Dec 2001, Daniel L Speyer wrote:
> > (setq healer (make-object 'attack 0 
> >                           'defend 1 
> >                       'move 2 
> >                       'ignore-zoc t 
> >                       'max-health 10))
> > (setf (get-object healer 'done-hook)
> >       #'(lambda (me)
> >       (mapcar 
> >        #'(lambda (x) (setf (get-object x 'health) 
> >                            (get-object x 'max-health)))
> >        (get-unit-list (get-tile me)))))
> > (create-unit healer)
> I'm impressed. It's somewhat obscure for an OO programmer like me but
> since it is an easy example. How would that look in a Java scripting
> language? Not as pretty probably.

It's going to be prettier in Perl, Python or Ruby.

my $healer = new Freeciv::Unit {
  attack => 0, defend => 1, move => 2, ignorezcc => 1, maxhealth => 10
  done = sub {
    my $self = $_[0];
    map { $_->{health} = $_->{maxhealth} } $me->get_unit_list()
  }
}

This is a literal translation into Perl - in reality you'd probably
want to create a new unit type rather than an individual unit.

(file perl/lib/Freeciv/Unit/Healer.pm would contain:)

package Freeciv::Unit::Healer;

@ISA = qw( Freeciv::Unit; )

use Freeciv::Unit;

sub new {
  my $self = shift;
  my %defaults =
    { attack => 0, defend => 1, move => 2, ignorezcc => 1, maxhealth => 10,
       done => sub {
       my $self = $_[0];
       map { $_->{health} = $_->{maxhealth} } $me->get_unit_list()
    };
  $self = new Freeciv::Unit ( %defaults, @_ };
  bless($self,Freeciv::Unit::Healer);
}

1;

(end of file)

(There are umpteen alternative ways to do it, of course.)

The same can be done in some OO Lisp or any other OO scripting language.
I personally prefer Perl's syntax to that of any other language I've
seen, but it's OO support is weak: everything happens dynamically,
there is no static type checking.  This example wasn't given with the
purpose of advocating Perl, it's just a language I happen to know.

Perl also has a well-defined way to interface with C (in both directions).
I have no doubt that any other language under consideration has this, too.

> But - beauty of syntax istn't the only argument.

Neither is availability of an interfacing method.

From the point of cleanliness it would be better to make all scripting
cmmunnicate with the server through the Freeciv network protocol, or
some other network protocol.  No need to say that Perl has plenty of
standard libraries for networking.

> -- 
> Gregor Zeitlinger      
> gregor@xxxxxxxxxxxxx

-- 
Reinier


[Prev in Thread] Current Thread [Next in Thread]