2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.ZDoom
|
|
|
|
{
|
2010-08-22 11:36:09 +00:00
|
|
|
public sealed class StateStructure
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// All we care about is the first sprite in the sequence
|
2010-08-19 15:06:15 +00:00
|
|
|
private List<string> sprites;
|
|
|
|
private StateGoto gotostate;
|
|
|
|
private DecorateParser parser;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
2010-08-22 11:36:09 +00:00
|
|
|
|
|
|
|
public int SpritesCount { get { return sprites.Count; } }
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
2014-02-26 14:11:06 +00:00
|
|
|
internal StateStructure(ActorStructure actor, DecorateParser parser)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
string lasttoken = "";
|
2010-08-19 15:06:15 +00:00
|
|
|
|
|
|
|
this.gotostate = null;
|
|
|
|
this.parser = parser;
|
|
|
|
this.sprites = new List<string>();
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Skip whitespace
|
|
|
|
while(parser.SkipWhitespace(true))
|
|
|
|
{
|
|
|
|
// Read first token
|
|
|
|
string token = parser.ReadToken();
|
|
|
|
token = token.ToLowerInvariant();
|
|
|
|
|
|
|
|
// One of the flow control statements?
|
2010-08-19 15:06:15 +00:00
|
|
|
if((token == "loop") || (token == "stop") || (token == "wait") || (token == "fail"))
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Ignore flow control
|
|
|
|
}
|
2010-08-19 15:06:15 +00:00
|
|
|
// Goto?
|
2010-08-17 10:02:07 +00:00
|
|
|
else if(token == "goto")
|
|
|
|
{
|
2010-08-19 15:06:15 +00:00
|
|
|
gotostate = new StateGoto(actor, parser);
|
|
|
|
if(parser.HasError) return;
|
2010-08-17 10:02:07 +00:00
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
// Label?
|
|
|
|
else if(token == ":")
|
|
|
|
{
|
|
|
|
// Rewind so that this label can be read again
|
|
|
|
parser.DataStream.Seek(-(lasttoken.Length + 1), SeekOrigin.Current);
|
|
|
|
|
|
|
|
// Done here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// End of scope?
|
|
|
|
else if(token == "}")
|
|
|
|
{
|
|
|
|
// Rewind so that this scope end can be read again
|
|
|
|
parser.DataStream.Seek(-1, SeekOrigin.Current);
|
|
|
|
|
|
|
|
// Done here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// First part of the sprite name
|
2015-01-03 18:43:37 +00:00
|
|
|
if(string.IsNullOrEmpty(token))
|
|
|
|
{
|
|
|
|
parser.ReportError("Unexpected end of structure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//mxd. First part of the sprite name can be quoted
|
|
|
|
token = parser.StripTokenQuotes(token);
|
|
|
|
if(string.IsNullOrEmpty(token))
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
parser.ReportError("Unexpected end of structure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Frames of the sprite name
|
|
|
|
parser.SkipWhitespace(true);
|
|
|
|
string spriteframes = parser.ReadToken();
|
|
|
|
if(spriteframes == null)
|
|
|
|
{
|
|
|
|
parser.ReportError("Unexpected end of structure");
|
|
|
|
return;
|
|
|
|
}
|
2015-01-03 18:43:37 +00:00
|
|
|
|
|
|
|
//mxd. Frames can be quoted
|
|
|
|
spriteframes = parser.StripTokenQuotes(spriteframes);
|
|
|
|
if(spriteframes == null)
|
|
|
|
{
|
|
|
|
parser.ReportError("Unexpected end of structure");
|
|
|
|
return;
|
|
|
|
}
|
2014-09-12 12:19:58 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Label?
|
2014-09-12 12:19:58 +00:00
|
|
|
if(spriteframes == ":")
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Rewind so that this label can be read again
|
|
|
|
parser.DataStream.Seek(-(token.Length + 1), SeekOrigin.Current);
|
|
|
|
|
|
|
|
// Done here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No first sprite yet?
|
2010-08-19 15:06:15 +00:00
|
|
|
if(spriteframes.Length > 0)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Make the sprite name
|
2010-08-19 15:06:15 +00:00
|
|
|
string spritename = token + spriteframes[0];
|
|
|
|
spritename = spritename.ToUpperInvariant();
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2015-01-03 18:43:37 +00:00
|
|
|
// Ignore some odd ZDoom things
|
|
|
|
if (!spritename.StartsWith("TNT1") && !spritename.StartsWith("----") && !spritename.Contains("#"))
|
2010-08-19 15:06:15 +00:00
|
|
|
sprites.Add(spritename);
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Continue until the end of the line
|
|
|
|
string t = "";
|
|
|
|
while((t != "\n") && (t != null))
|
|
|
|
{
|
|
|
|
parser.SkipWhitespace(false);
|
|
|
|
t = parser.ReadToken();
|
2013-04-15 10:54:55 +00:00
|
|
|
|
|
|
|
//mxd. Because stuff like this is also valid: "Actor Oneliner { States { Spawn: WOOT A 1 A_FadeOut(0.1) Loop }}"
|
2014-12-03 23:15:26 +00:00
|
|
|
if(t == "}")
|
|
|
|
{
|
2013-04-15 10:54:55 +00:00
|
|
|
// Rewind so that this scope end can be read again
|
|
|
|
parser.DataStream.Seek(-1, SeekOrigin.Current);
|
|
|
|
|
|
|
|
// Done here
|
|
|
|
return;
|
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lasttoken = token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 11:35:56 +00:00
|
|
|
//mxd
|
2015-01-03 18:43:37 +00:00
|
|
|
internal StateStructure(string spritename)
|
2014-12-03 23:15:26 +00:00
|
|
|
{
|
2013-04-19 11:35:56 +00:00
|
|
|
this.gotostate = null;
|
2015-01-03 18:43:37 +00:00
|
|
|
this.sprites = new List<string> { spritename };
|
2013-04-19 11:35:56 +00:00
|
|
|
}
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
2010-08-19 15:06:15 +00:00
|
|
|
|
|
|
|
// This finds the first valid sprite and returns it
|
|
|
|
public string GetSprite(int index)
|
2010-08-28 16:48:17 +00:00
|
|
|
{
|
|
|
|
List<StateStructure> callstack = new List<StateStructure>();
|
|
|
|
return GetSprite(index, callstack);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This version of GetSprite uses a callstack to check if it isn't going into an endless loop
|
|
|
|
private string GetSprite(int index, List<StateStructure> prevstates)
|
2010-08-19 15:06:15 +00:00
|
|
|
{
|
2010-08-20 06:07:56 +00:00
|
|
|
// If we have sprite of our own, see if we can return this index
|
2010-08-19 15:06:15 +00:00
|
|
|
if(index < sprites.Count)
|
|
|
|
{
|
|
|
|
return sprites[index];
|
|
|
|
}
|
2010-08-28 16:48:17 +00:00
|
|
|
|
2010-08-20 06:07:56 +00:00
|
|
|
// Otherwise, continue searching where goto tells us to go
|
2010-08-28 16:48:17 +00:00
|
|
|
if(gotostate != null)
|
2010-08-19 15:06:15 +00:00
|
|
|
{
|
|
|
|
// Find the class
|
|
|
|
ActorStructure a = parser.GetArchivedActorByName(gotostate.ClassName);
|
|
|
|
if(a != null)
|
|
|
|
{
|
|
|
|
StateStructure s = a.GetState(gotostate.StateName);
|
2010-08-28 16:48:17 +00:00
|
|
|
if((s != null) && !prevstates.Contains(s))
|
2010-08-19 15:06:15 +00:00
|
|
|
{
|
2010-08-28 16:48:17 +00:00
|
|
|
prevstates.Add(this);
|
|
|
|
return s.GetSprite(gotostate.SpriteOffset, prevstates);
|
2010-08-19 15:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-28 16:48:17 +00:00
|
|
|
|
2010-08-20 06:07:56 +00:00
|
|
|
// If there is no goto keyword used, just give us one of our sprites if we can
|
2010-08-28 16:48:17 +00:00
|
|
|
if(sprites.Count > 0)
|
2010-08-20 06:07:56 +00:00
|
|
|
{
|
|
|
|
// The following behavior should really depend on the flow control keyword (loop or stop) but who cares.
|
|
|
|
return sprites[0];
|
|
|
|
}
|
2010-08-19 15:06:15 +00:00
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|