View/Edit #963!cars.js

File name: cars.js
var total_cars = 10;
var right_side = true;
var stationary = false;
var plane = false;
var chopper = false;

var args = '';
if (location.search.length > 1) {
  // The form was used to input new prefs.
  args = location.search.substring(1);
  // Save the prefs to a cookie.
  var nextyear = new Date();
  nextyear.setYear(nextyear.getYear()+1);
  document.cookie = "cars="+escape(args)+"; expires="+nextyear.toGMTString();
} else {
  // Load prefs from cookie.
  args = getCookie('cars');
}
if (args) {
  args = args.split('&');
  var sep, argname, argval;
  for (var i = 0; i < args.length; i++) {
    sep = args[i].indexOf('=')
    if (sep != -1) {
      argname = args[i].substring(0, sep);
      argval = args[i].substring(sep+1);
      if (argname == "cars") {
        argval = parseInt(argval);
        if (!isNaN(argval))
          total_cars = argval;
      } else if (argname == "mode") {
        if (argval == "0")
          stationary = true;
        else if (argval == "2")
          right_side = false;
      } else if (argname == "plane") {
        plane = true;
      } else if (argname == "chopper") {
        chopper = true;
      }
    }
  }
}
if (total_cars < 0) total_cars = 0;

// name - name of the desired cookie
// return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  begin = begin + prefix.length;
  return unescape(dc.substring(begin, end));
}

var road0  = new Image(16, 16);  road0.src  = 'http://files.moo.ca/9/6/3/road0.gif';
var road0a = new Image(16, 16);  road0a.src = 'http://files.moo.ca/9/6/3/road0a.gif';
var road0b = new Image(16, 16);  road0b.src = 'http://files.moo.ca/9/6/3/road0b.gif';
var road0c = new Image(16, 16);  road0c.src = 'http://files.moo.ca/9/6/3/road0c.gif';
var road0d = new Image(16, 16);  road0d.src = 'http://files.moo.ca/9/6/3/road0d.gif';
var road1  = new Image(16, 16);  road1.src  = 'http://files.moo.ca/9/6/3/road1.gif';
var road1a = new Image(16, 16);  road1a.src = 'http://files.moo.ca/9/6/3/road1a.gif';
var road1b = new Image(16, 16);  road1b.src = 'http://files.moo.ca/9/6/3/road1b.gif';
var road1c = new Image(16, 16);  road1c.src = 'http://files.moo.ca/9/6/3/road1c.gif';
var road2  = new Image(16, 16);  road2.src  = 'http://files.moo.ca/9/6/3/road2.gif';
var road2a = new Image(16, 16);  road2a.src = 'http://files.moo.ca/9/6/3/road2a.gif';
var road2b = new Image(16, 16);  road2b.src = 'http://files.moo.ca/9/6/3/road2b.gif';
var road2c = new Image(16, 16);  road2c.src = 'http://files.moo.ca/9/6/3/road2c.gif';

function Car(id) {
  if (id >= 0) {
    this.id = id;
    this.x = null;
    this.y = null;
    this.z = null;
    this.speed = Math.floor(Math.random()*1000) + 500;
    window.setTimeout('cars['+this.id+'].go()', this.speed);
  }
}
// Create and discard an initial Car object (for Netscape 3)
new Car(-1);

function Car_go() {
  if (this.z == null) {
    this.init();
  } else if (stationary) {
    // That's all folks.
    return;
  } else {
    var nextx = this.x;
    var nexty = this.y;
    var nextz = this.z;
    if (this.z == 1)
      nexty = nexty + (right_side ? -1: 1);
    else if (this.z == 2)
      nextx = nextx + (right_side ? -1: 1);
    else if (this.z == 3)
      nexty = nexty + (right_side ? 1: -1);
    else if (this.z == 4)
      nextx = nextx + (right_side ? 1: -1);

    var nextimg = eval('document.road_'+nextx+'_'+nexty);
    var thisimg = eval('document.road_'+this.x+'_'+this.y);
    var force = false;
    if (!nextimg) {
      // dead end, flip to other side of road
      nextx = this.x;
      nexty = this.y;
      nextz = nextz + (nextz > 2 ? -2 : 2);
    } else if (Math.random() < 0.1 && nextimg.src == road0.src) {
      // hang a right at the next intersection
      if (this.z == 1) {
        nextx = nextx + -1;
        nextz = (right_side ? 2: 4);
      } else if (this.z == 2) {
        nexty = nexty + 1;
        nextz = (right_side ? 3: 1);
      } else if (this.z == 3) {
        nextx = nextx + 1;
        nextz = (right_side ? 4: 2);
      } else if (this.z == 4) {
        nexty = nexty + -1;
        nextz = (right_side ? 1: 3);
      }
    } else if (Math.random() < 0.12 && (thisimg.src == road0a.src || thisimg.src == road0b.src || thisimg.src == road0c.src || thisimg.src == road0d.src)) {
      // hang a left in this intersection
      nextx = this.x;
      nexty = this.y;
      if (this.z == 1)
        nextz = (right_side ? 4: 2);
      else if (this.z == 2)
        nextz = (right_side ? 1: 3);
      else if (this.z == 3)
        nextz = (right_side ? 2: 4);
      else if (this.z == 4)
        nextz = (right_side ? 3: 1);
      force = true; // don't worry about colliding with yourself
    }
    if (force || spot_free(nextx, nexty, nextz)) {
      this.undraw();
      this.x = nextx;
      this.y = nexty;
      this.z = nextz;
      this.draw();
    }
  }

  if (Math.random() < 0.01)
    window.setTimeout('cars['+this.id+'].go()', Math.floor(Math.random()*15000)+ 5000);
  else
    window.setTimeout('cars['+this.id+'].go()', this.speed);
}
Car.prototype.go = Car_go;

function Car_init() {
  var img = document.images[Math.floor(Math.random()*document.images.length)];
  if (img.name.indexOf('road_') == 0) {
    if (img.src == road1.src)
      this.z = Math.floor(Math.random()*2) * 2 + 2;
    else if (img.src == road2.src)
      this.z = Math.floor(Math.random()*2) * 2 + 1;
    else if (img.src == road1a.src)
      this.z = 2;
    else if (img.src == road1b.src)
      this.z = 4;
    else if (img.src == road2a.src)
      this.z = 3;
    else if (img.src == road2b.src)
      this.z = 1;
    if (this.z != null) {
      var us = img.name.lastIndexOf('_');
      this.x = parseInt(img.name.substring(5, us));
      this.y = parseInt(img.name.substring(us+1));
      this.draw();
    }
  }
}
Car.prototype.init = Car_init;

function Car_draw() {
  var img = eval('document.road_'+this.x+'_'+this.y);
  if (img.src == road0.src && this.z == 1)
    img.src = road0a.src;
  else if (img.src == road0.src && this.z == 2)
    img.src = road0b.src;
  else if (img.src == road0.src && this.z == 3)
    img.src = road0c.src;
  else if (img.src == road0.src && this.z == 4)
    img.src = road0d.src;
  else if (img.src == road1.src && this.z == 2)
    img.src = road1b.src;
  else if (img.src == road1.src && this.z == 4)
    img.src = road1a.src;
  else if (img.src == road1a.src && this.z == 2)
    img.src = road1c.src;
  else if (img.src == road1b.src && this.z == 4)
    img.src = road1c.src;
  else if (img.src == road2.src && this.z == 1)
    img.src = road2a.src;
  else if (img.src == road2.src && this.z == 3)
    img.src = road2b.src;
  else if (img.src == road2a.src && this.z == 3)
    img.src = road2c.src;
  else if (img.src == road2b.src && this.z == 1)
    img.src = road2c.src;
  else
    alert('Illegal draw: '+img.src+' '+this.z);
}
Car.prototype.draw = Car_draw;

function Car_undraw() {
  var img = eval('document.road_'+this.x+'_'+this.y);
  if (img.src == road0a.src && this.z == 1)
    img.src = road0.src;
  else if (img.src == road0b.src && this.z == 2)
    img.src = road0.src;
  else if (img.src == road0c.src && this.z == 3)
    img.src = road0.src;
  else if (img.src == road0d.src && this.z == 4)
    img.src = road0.src;
  else if (img.src == road1b.src && this.z == 2)
    img.src = road1.src;
  else if (img.src == road1a.src && this.z == 4)
    img.src = road1.src;
  else if (img.src == road1c.src && this.z == 2)
    img.src = road1a.src;
  else if (img.src == road1c.src && this.z == 4)
    img.src = road1b.src;
  else if (img.src == road2a.src && this.z == 1)
    img.src = road2.src;
  else if (img.src == road2b.src && this.z == 3)
    img.src = road2.src;
  else if (img.src == road2c.src && this.z == 3)
    img.src = road2a.src;
  else if (img.src == road2c.src && this.z == 1)
    img.src = road2b.src;
  else
    alert('Illegal undraw: '+img.src+' '+this.z);
}
Car.prototype.undraw = Car_undraw;

function spot_free(x, y, z) {
  var img = eval('document.road_'+x+'_'+y);
  if (!img)
    return false;
  else if (img.src == road0.src || img.src == road1.src || img.src == road2.src)
    return true;
  else if (img.src == road1a.src && z == 2)
    return true;
  else if (img.src == road1b.src && z == 4)
    return true;
  else if (img.src == road2a.src && z == 3)
    return true;
  else if (img.src == road2b.src && z == 1)
    return true;
  return false;
}

// Manufacture the cars and set 'em lose.
var cars = new Array();
for (var i = 0; i < total_cars; i++) {
  cars[cars.length] = new Car(i);
}

// Print the prefs form
document.write("<FORM METHOD='get'>");
document.write("Display <INPUT TYPE='text' NAME='cars' SIZE=3 MAXLENGTH=3 VALUE='"+total_cars+"'>");
document.write("<SELECT NAME='mode'>");
document.write("<OPTION VALUE=0>stationary cars");
document.write("<OPTION VALUE=1"+(right_side && !stationary ?" SELECTED":"")+">cars driving on the right");
document.write("<OPTION VALUE=2"+(!right_side && !stationary ?" SELECTED":"")+">cars driving on the left");
document.write("</SELECT>\n");
if (aero) {
  document.write("<BR><INPUT TYPE='checkbox' NAME='plane'"+(plane ?" CHECKED":"")+"> Airplane\n");
  document.write("<BR><INPUT TYPE='checkbox' NAME='chopper'"+(chopper ?" CHECKED":"")+"> Helicopter\n");
  document.write("<BR>\n");
}
document.write("<INPUT TYPE='submit' VALUE='Change'>");
document.write("</FORM>");

http://files.moo.ca:8081/9/6/3/cars.js
Size: 9,688 bytes.
File perms: Readable


You are not logged in.

[home | help | who | search | setup | code]