- scriptified the declaration of AmbientSound

Most of the code is still native, but this means that PlayerPawn is the last remaining child of AActor.
This commit is contained in:
Christoph Oelckers 2019-01-02 01:04:50 +01:00
parent 68091db598
commit dc612703d5
4 changed files with 84 additions and 115 deletions

View file

@ -227,7 +227,6 @@ void FDynamicLight::ReleaseLight()
if (next != nullptr) next->prev = prev;
prev = nullptr;
FreeList.Push(this);
Printf("Releasing %p\n", this);
}

View file

@ -2178,43 +2178,6 @@ CCMD (playersounds)
}
}
// AAmbientSound implementation ---------------------------------------------
class AAmbientSound : public AActor
{
DECLARE_CLASS (AAmbientSound, AActor)
public:
void Serialize(FSerializer &arc);
void MarkPrecacheSounds () const;
void BeginPlay ();
void Tick ();
void Activate (AActor *activator);
void Deactivate (AActor *activator);
protected:
bool bActive;
private:
void SetTicker (struct FAmbientSound *ambient);
int NextCheck;
};
IMPLEMENT_CLASS(AAmbientSound, false, false)
//==========================================================================
//
// AmbientSound :: Serialize
//
//==========================================================================
void AAmbientSound::Serialize(FSerializer &arc)
{
Super::Serialize (arc);
arc("active", bActive)
("nextcheck", NextCheck);
}
//==========================================================================
//
// AmbientSound :: MarkAmbientSounds
@ -2223,7 +2186,7 @@ void AAmbientSound::Serialize(FSerializer &arc)
DEFINE_ACTION_FUNCTION(AAmbientSound, MarkAmbientSounds)
{
PARAM_SELF_PROLOGUE(AAmbientSound);
PARAM_SELF_PROLOGUE(AActor);
FAmbientSound *ambient = Ambients.CheckKey(self->args[0]);
if (ambient != NULL)
@ -2233,26 +2196,52 @@ DEFINE_ACTION_FUNCTION(AAmbientSound, MarkAmbientSounds)
return 0;
}
//==========================================================================
//
//
//
//==========================================================================
int GetTicker(struct FAmbientSound *ambient)
{
if ((ambient->type & CONTINUOUS) == CONTINUOUS)
{
return 1;
}
else if (ambient->type & RANDOM)
{
return (int)(((float)rand() / (float)RAND_MAX) *
(float)(ambient->periodmax - ambient->periodmin)) +
ambient->periodmin;
}
else
{
return ambient->periodmin;
}
}
//==========================================================================
//
// AmbientSound :: Tick
//
//==========================================================================
void AAmbientSound::Tick ()
DEFINE_ACTION_FUNCTION(AAmbientSound, Tick)
{
Super::Tick ();
PARAM_SELF_PROLOGUE(AActor);
if (!bActive || level.maptime < NextCheck)
return;
self->Tick();
if (!self->special2 || level.maptime < self->special1)
return 0;
FAmbientSound *ambient;
int loop = 0;
ambient = Ambients.CheckKey(args[0]);
ambient = Ambients.CheckKey(self->args[0]);
if (ambient == NULL)
{
return;
return 0;
}
if ((ambient->type & CONTINUOUS) == CONTINUOUS)
@ -2265,77 +2254,42 @@ void AAmbientSound::Tick ()
// The second argument scales the ambient sound's volume.
// 0 and 100 are normal volume. The maximum volume level
// possible is always 1.
float volscale = args[1] == 0 ? 1 : args[1] / 100.f;
float volscale = self->args[1] == 0 ? 1 : self->args[1] / 100.f;
float usevol = clamp(ambient->volume * volscale, 0.f, 1.f);
// The third argument is the minimum distance for audible fading, and
// the fourth argument is the maximum distance for audibility. Setting
// either of these to 0 or setting min distance > max distance will
// use the standard rolloff.
if ((args[2] | args[3]) == 0 || args[2] > args[3])
if ((self->args[2] | self->args[3]) == 0 || self->args[2] > self->args[3])
{
S_Sound(this, CHAN_BODY | loop, ambient->sound, usevol, ambient->attenuation);
S_Sound(self, CHAN_BODY | loop, ambient->sound, usevol, ambient->attenuation);
}
else
{
float min = float(args[2]), max = float(args[3]);
float min = float(self->args[2]), max = float(self->args[3]);
// The fifth argument acts as a scalar for the preceding two, if it's non-zero.
if (args[4] > 0)
if (self->args[4] > 0)
{
min *= args[4];
max *= args[4];
min *= self->args[4];
max *= self->args[4];
}
S_SoundMinMaxDist(this, CHAN_BODY | loop, ambient->sound, usevol, min, max);
S_SoundMinMaxDist(self, CHAN_BODY | loop, ambient->sound, usevol, min, max);
}
if (!loop)
{
SetTicker (ambient);
self->special1 += GetTicker (ambient);
}
else
{
NextCheck = INT_MAX;
self->special1 = INT_MAX;
}
}
else
{
Destroy ();
self->Destroy ();
}
}
//==========================================================================
//
// AmbientSound :: SetTicker
//
//==========================================================================
void AAmbientSound::SetTicker (struct FAmbientSound *ambient)
{
if ((ambient->type & CONTINUOUS) == CONTINUOUS)
{
NextCheck += 1;
}
else if (ambient->type & RANDOM)
{
NextCheck += (int)(((float)rand() / (float)RAND_MAX) *
(float)(ambient->periodmax - ambient->periodmin)) +
ambient->periodmin;
}
else
{
NextCheck += ambient->periodmin;
}
}
//==========================================================================
//
// AmbientSound :: BeginPlay
//
//==========================================================================
void AAmbientSound::BeginPlay ()
{
Super::BeginPlay ();
CallActivate (NULL);
return 0;
}
//==========================================================================
@ -2346,37 +2300,40 @@ void AAmbientSound::BeginPlay ()
//
//==========================================================================
void AAmbientSound::Activate (AActor *activator)
DEFINE_ACTION_FUNCTION(AAmbientSound, Activate)
{
Super::Activate (activator);
FAmbientSound *amb = Ambients.CheckKey(args[0]);
PARAM_SELF_PROLOGUE(AActor);
PARAM_OBJECT(activator, AActor);
self->Activate(activator);
FAmbientSound *amb = Ambients.CheckKey(self->args[0]);
if (amb == NULL)
{
Destroy ();
return;
self->Destroy ();
return 0;
}
if (!bActive)
if (!self->special2)
{
if ((amb->type & 3) == 0 && amb->periodmin == 0)
{
int sndnum = S_FindSound(amb->sound);
if (sndnum == 0)
{
Destroy ();
return;
self->Destroy ();
return 0;
}
amb->periodmin = ::Scale(S_GetMSLength(sndnum), TICRATE, 1000);
}
NextCheck = level.maptime;
self->special1 = level.maptime;
if (amb->type & (RANDOM|PERIODIC))
SetTicker (amb);
self->special1 += GetTicker (amb);
bActive = true;
self->special2 = true;
}
return 0;
}
//==========================================================================
@ -2388,18 +2345,22 @@ void AAmbientSound::Activate (AActor *activator)
//
//==========================================================================
void AAmbientSound::Deactivate (AActor *activator)
DEFINE_ACTION_FUNCTION(AAmbientSound, Deactivate)
{
Super::Deactivate (activator);
if (bActive)
PARAM_SELF_PROLOGUE(AActor);
PARAM_OBJECT(activator, AActor);
self->Deactivate(activator);
if (self->special2)
{
bActive = false;
FAmbientSound *ambient = Ambients.CheckKey(args[0]);
self->special2 = false;
FAmbientSound *ambient = Ambients.CheckKey(self->args[0]);
if (ambient != NULL && (ambient->type & CONTINUOUS) == CONTINUOUS)
{
S_StopSound (this, CHAN_BODY);
S_StopSound (self, CHAN_BODY);
}
}
return 0;
}

View file

@ -1118,7 +1118,7 @@ void ZCCCompiler::CompileAllFields()
// Create copies of the arrays which can be altered
auto Classes = this->Classes;
auto Structs = this->Structs;
TMap<PClass*, bool> HasNativeChildren;
TMap<FName, bool> HasNativeChildren;
// first step: Look for native classes with native children.
// These may not have any variables added to them because it'd clash with the native definitions.
@ -1134,7 +1134,7 @@ void ZCCCompiler::CompileAllFields()
if (ac->ParentClass != nullptr && ac->ParentClass->VMType == c->Type() && ac->Size != TentativeClass)
{
// Only set a marker here, so that we can print a better message when the actual fields get added.
HasNativeChildren.Insert(ac, true);
HasNativeChildren.Insert(c->Type()->TypeName, true);
break;
}
}
@ -1180,7 +1180,7 @@ void ZCCCompiler::CompileAllFields()
else
type->MetaSize = 0;
if (CompileFields(type->VMType, Classes[i]->Fields, nullptr, &Classes[i]->TreeNodes, false, !!HasNativeChildren.CheckKey(type)))
if (CompileFields(type->VMType, Classes[i]->Fields, nullptr, &Classes[i]->TreeNodes, false, !!HasNativeChildren.CheckKey(type->TypeName)))
{
// Remove from the list if all fields got compiled.
Classes.Delete(i--);
@ -1348,7 +1348,7 @@ bool ZCCCompiler::CompileFields(PContainerType *type, TArray<ZCC_VarDeclarator *
}
}
}
else if (hasnativechildren)
else if (hasnativechildren && !(varflags & VARF_Meta))
{
Error(field, "Cannot add field %s to %s. %s has native children which means it size may not change", FName(name->Name).GetChars(), type->TypeName.GetChars(), type->TypeName.GetChars());
}

View file

@ -58,7 +58,7 @@
** selects which choice it picks.
*/
class AmbientSound : Actor native
class AmbientSound : Actor
{
default
{
@ -68,6 +68,15 @@ class AmbientSound : Actor native
}
native void MarkAmbientSounds();
override native void Tick();
override native void Activate(Actor activator);
override native void Deactivate(Actor activator);
override void BeginPlay ()
{
Super.BeginPlay ();
Activate (NULL);
}
override void MarkPrecacheSounds()
{