⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 command.cpp

📁 一个三维打斗游戏
💻 CPP
字号:
// (C) Copyright 1996 by Anthony J. Carin.  All Rights Reserved.

#include "stdafx.h"
#include "command.h"
#include "levels.h"
#include <stdio.h>
#include <string.h>
#include <mmsystem.h>

static char *inststr[] =
{
"ENDSESSION",
"INSERTFILE",
"THEYDO",
"TILTBODYRIGHT",
"TILTBODYLEFT",
"TILTBODYFWD",
"TILTBODYBCK",
"TURNHEADLEFT",
"TURNHEADRIGHT",
"TURNHEADUP",
"TURNHEADDOWN",
"RLEGBENDKNEE",
"LLEGBENDKNEE",
"RLEGRAISEFWD",
"LLEGRAISEFWD",
"RLEGRAISEBCK",
"LLEGRAISEBCK",
"RLEGRAISESIDE",
"LLEGRAISESIDE",
"RARMBENDELBOW",
"LARMBENDELBOW",
"RARMRAISEFWD",
"LARMRAISEFWD",
"RARMRAISEBCK",
"LARMRAISEBCK",
"RARMRAISESIDE",
"LARMRAISESIDE",
"TURNRIGHT",
"TURNLEFT",
"MOVEUPWARD",
"MOVEFOWARD",
"MOVEBACKWARD",
"PLAYSOUND",
"PLAYGENERIC",
"BEGINPROTECT",
"ENDPROTECT",
"STRIKETOP",
"STRIKEMED",
"STRIKEBOT",
"TILTRIGHTWEAPON",
"TILTLEFTWEAPON",
"CLEAR",
"HURTME",
"RTHROW",
"LTHROW",
"PICKUP",
"NOMORE"
};

static insttype getinst(char *str);
static char     stringcommand(insttype inst);

instruction::instruction()
{
    m_inst   = NOMORE;
    m_value  = 0.0f;
    m_string = NULL;
    Next     = NULL;
}

instruction::instruction(instruction& i)
{
    m_inst   = i.m_inst;
    m_value  = i.m_value;
    m_string = NULL;
    setstring(i.string());
    Next     = NULL;
}

void instruction::operator=(instruction& i)
{
    m_inst   = i.m_inst;
    m_value  = i.m_value;
    setstring(i.string());
}

void instruction::setstring(char *str)
{
   if (str == 0 || *str == 0)
   {
       if (m_string)
            delete m_string;
       m_string = NULL;
       return;
   }
   if (m_string)
       delete m_string;
   m_string = new char[strlen(str) + 1];
   strcpy(m_string, str);
}

void instlist::purge()
{
   while (First)
   {
      Current = First;
      First = First->Next;
      delete Current;
   }
   First = Last = Current = 0;
   m_protected = FALSE;
}

void instlist::remove(insttype v)
{
   instruction itmp; 
   instruction *tmp = First, *hold;
   char removed = FALSE;
   hold = 0;
   while (tmp)
   {
      itmp = *tmp;
      if (itmp() == ENDPROTECT)
      {
          m_protected = FALSE;
          hold = tmp;
          tmp = tmp->Next;
      }
      else if (itmp() == BEGINPROTECT)
      {
          m_protected = TRUE;
          while (tmp)
          {
              itmp = *tmp;
              if (itmp() == ENDPROTECT)
                  break;
              tmp = tmp->Next;
          }
          if (tmp == NULL)
              break;
      }
      else if ((itmp() == v || v == NOMORE) &&
               m_protected == FALSE)
      {
          removed = TRUE;
          if (hold == 0)
          {
              First = tmp->Next;
              if (tmp == Current)
                  Current = tmp->Next;
              if (tmp == Last)
                  Last = 0;
              delete tmp;
              tmp = First;
          }
          else
          {
              hold->Next = tmp->Next;
              if (tmp == Current)
                  Current = tmp->Next;
              if (tmp == Last)
                  Last = hold;
              delete tmp;
              tmp = hold->Next;
          }
      }
      else
      {
          hold = tmp;
          tmp = tmp->Next;
      }
   }
   if (hold == 0 && removed)
       First = Current = Last = 0;
}

void instlist::add(instruction& i)
{
   instruction *tmp;
   tmp = new instruction(i);
   if (First == 0)
   {
      First = Last = Current = tmp;
   }
   else
   {
      Last->Next = tmp;
      Last = tmp;
   }
}

instruction& instlist::next()
{
   static instruction ret;
   if (Current == 0)
   {
      ret = NOMORE;
   }
   else
   {
      ret = *Current;
      if (m_purgeold && Current == First)
      {
         Current = Current->Next;
         delete First;
         First = Current;
      }
      else
         Current = Current->Next;

   }
   return ret;
}

command::command(LPCTSTR filename)
{
    FILE *fp;
    char comstr[40];
    float comval;
    instruction inst;
    m_instlist = new instlist();
    fp = fopen(getpath((CString)filename), "r");
    if (fp <= 0)
    {
        itoa(errno, comstr, 10);
        return;
    }
    while (!feof(fp))
    {
        fscanf(fp, "%s", comstr);
        inst = getinst(comstr);
        if (inst() == NOMORE)
           break;
        if (inst() == NOTEXIST)
        {
           fscanf(fp, "%s", comstr);
           continue;
        }
        if (inst() == INSERTFILE)
        {
           fscanf(fp, "%s", comstr);
           strcat(comstr, ".cmd");
           command other(comstr);
           append(other);
           continue;
        }
        if (stringcommand(inst()))
        {
           fscanf(fp, "%s", comstr);
           inst.setstring(comstr);
        }
        else
        {
           fscanf(fp, "%s", comstr);
           comval = (float) atof(comstr);
           inst.setvalue(comval/100.0f);
        }
        m_instlist->add(inst);
    }
    fclose(fp);
}

static insttype getinst(char *str)
{
    for (int i = ENDSESSION; i <= NOMORE; i++)
    {
        if (strcmp(str, inststr[i]) == 0)
            break;
    }
    return (insttype) i;
}

static char stringcommand(insttype inst)
{
    if (inst == PLAYSOUND ||
        inst == THEYDO)
        return TRUE;
    return FALSE;
}

void command::append(command& other)
{
    instruction tmp;
    tmp = other.next();
    while (tmp() != NOMORE)
    {
       m_instlist->add(tmp);
       tmp = other.next();
    }
}

void command::AddToValue(insttype instruct, short bonus)
{
    instruction tmp;
    m_instlist->restart();
    tmp = m_instlist->next();
    while (tmp() != NOMORE)
    {
       if (tmp() == instruct)
           tmp.setvalue(tmp.value()+bonus);
       tmp = m_instlist->next();
    }
    m_instlist->restart();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -