/*
 * PVVMUD a 3D MUD
 * Copyright (C) 1998-1999  Programvareverkstedet (pvv@pvv.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "gamefunction.H"

CGameFunctionObject :: CGameFunctionObject () {
  type=-1;
  func.c=NULL;
}

CGameFunctionObject :: ~CGameFunctionObject () {
  type=-1;
  func.c=NULL;
}

CGameFunction :: ~CGameFunction() {
  funchash::iterator i;
  for(i=hash.begin();i!=hash.end();i++) delete (*i).second;
  hash.clear();
}

void CGameFunction :: add(char *name, void (*function)()) {
  CGameFunctionObject *gfo;

  gfo=new CGameFunctionObject;

  gfo->type                = 0;
  gfo->func.c              = function;

  hash[name] = gfo;
}

void CGameFunction :: add(char *name, char *function) {
  CGameFunctionObject *gfo;

  gfo=new CGameFunctionObject;

  gfo->type                = 1;
  gfo->func.scriptSource   = function;

  hash[name] = gfo;
}

void CGameFunction :: add(char *name, WORD *function) {
  CGameFunctionObject *gfo;

  gfo=new CGameFunctionObject;

  gfo->type                = 2;
  gfo->func.scriptByteCode = function;

  hash[name] = gfo;
}

void CGameFunction :: remove(char *name) {
  CGameFunctionObject *gfo;

  free(hash[name]);
  hash.erase(name);
}

bool CGameFunction :: execute(char *name, CGameEvent *gameEvent) {
  CGameFunctionObject *gfo;

  gfo=getGameFuncObj(name);
  return(execute(gfo,gameEvent));
}

CGameFunctionObject *CGameFunction::getGameFuncObj(char *name) {
  CGameFunctionObject *gfo=NULL;

  gfo=hash[name];
  return gfo;

}


bool CGameFunctionObject :: execute (CGameEvent *gameEvent) {

  switch(type) {
  case 0:
    return(func->c());
//   case 1:
//     return(executeScript(func.scriptSource,gameEvent));
//   case 2:
//     return(executeByteCode(func.scriptByteCode,gameEvent));
  };

}

bool CGameFunction :: execute(CGameFunctionObject *gfo, 
          CGameEvent *gameEvent) {

  if (gfo == NULL) return;
  return (gfo->execute(gameEvent));

}


bool CGameFunction :: load(iostream &readFile) {
  return TRUE;
};

bool CGameFunction :: save(iostream &writeFile {
  return TRUE;
}