[+] Ability play unit's still animation while waiting

This commit is contained in:
cybermind 2014-02-04 22:45:27 +06:00
parent c40907fab2
commit a8448d0c97
14 changed files with 139 additions and 17 deletions

View file

@ -543,9 +543,18 @@ void COrder_Attack::AttackTarget(CUnit &unit)
Assert(this->HasGoal() || Map.Info.IsPointOnMap(this->goalPos));
if (unit.Wait) {
if (!unit.Waiting) {
unit.Waiting = 1;
unit.WaitBackup = unit.Anim;
}
UnitShowAnimation(unit, unit.Type->Animations->Still);
unit.Wait--;
return;
}
if (unit.Waiting) {
unit.Anim = unit.WaitBackup;
unit.Waiting = 0;
}
switch (this->State) {
case 0: { // First entry

View file

@ -416,8 +416,17 @@ bool COrder_Build::BuildFromOutside(CUnit &unit) const
/* virtual */ void COrder_Build::Execute(CUnit &unit)
{
if (unit.Wait) {
if (!unit.Waiting) {
unit.Waiting = 1;
unit.WaitBackup = unit.Anim;
}
UnitShowAnimation(unit, unit.Type->Animations->Still);
unit.Wait--;
return ;
return;
}
if (unit.Waiting) {
unit.Anim = unit.WaitBackup;
unit.Waiting = 0;
}
if (this->State <= State_MoveToLocationMax) {
if (this->MoveToLocation(unit)) {

View file

@ -37,6 +37,7 @@
#include "action/action_defend.h"
#include "animation.h"
#include "iolib.h"
#include "map.h"
#include "pathfinder.h"
@ -148,8 +149,17 @@ enum {
/* virtual */ void COrder_Defend::Execute(CUnit &unit)
{
if (unit.Wait) {
if (!unit.Waiting) {
unit.Waiting = 1;
unit.WaitBackup = unit.Anim;
}
UnitShowAnimation(unit, unit.Type->Animations->Still);
unit.Wait--;
return ;
return;
}
if (unit.Waiting) {
unit.Anim = unit.WaitBackup;
unit.Waiting = 0;
}
CUnit *goal = this->GetGoal();

View file

@ -37,6 +37,7 @@
#include "action/action_follow.h"
#include "animation.h"
#include "iolib.h"
#include "missile.h"
#include "pathfinder.h"
@ -155,8 +156,17 @@ enum {
/* virtual */ void COrder_Follow::Execute(CUnit &unit)
{
if (unit.Wait) {
if (!unit.Waiting) {
unit.Waiting = 1;
unit.WaitBackup = unit.Anim;
}
UnitShowAnimation(unit, unit.Type->Animations->Still);
unit.Wait--;
return ;
return;
}
if (unit.Waiting) {
unit.Anim = unit.WaitBackup;
unit.Waiting = 0;
}
CUnit *goal = this->GetGoal();
@ -182,10 +192,6 @@ enum {
return ;
}
// Reset frame to still frame while we wait
// FIXME: Unit doesn't animate.
unit.Frame = unit.Type->StillFrame;
UnitUpdateHeading(unit);
unit.Wait = 10;
if (this->Range > 1) {
this->Range = 1;

View file

@ -153,10 +153,6 @@ int DoActionMove(CUnit &unit)
unit.Moving = 0;
return d;
case PF_WAIT: // No path, wait
// Reset frame to still frame while we wait
// FIXME: Unit doesn't animate.
unit.Frame = unit.Type->StillFrame;
UnitUpdateHeading(unit);
unit.Wait = 10;
unit.Moving = 0;
@ -221,8 +217,17 @@ int DoActionMove(CUnit &unit)
Assert(unit.CanMove());
if (unit.Wait) {
if (!unit.Waiting) {
unit.Waiting = 1;
unit.WaitBackup = unit.Anim;
}
UnitShowAnimation(unit, unit.Type->Animations->Still);
unit.Wait--;
return ;
return;
}
if (unit.Waiting) {
unit.Anim = unit.WaitBackup;
unit.Waiting = 0;
}
// FIXME: (mr-russ) Make a reachable goal here with GoalReachable ...

View file

@ -37,6 +37,7 @@
#include "action/action_patrol.h"
#include "animation.h"
#include "iolib.h"
#include "map.h"
#include "pathfinder.h"
@ -133,8 +134,17 @@
/* virtual */ void COrder_Patrol::Execute(CUnit &unit)
{
if (unit.Wait) {
if (!unit.Waiting) {
unit.Waiting = 1;
unit.WaitBackup = unit.Anim;
}
UnitShowAnimation(unit, unit.Type->Animations->Still);
unit.Wait--;
return ;
return;
}
if (unit.Waiting) {
unit.Anim = unit.WaitBackup;
unit.Waiting = 0;
}
switch (DoActionMove(unit)) {

View file

@ -1126,10 +1126,18 @@ void COrder_Resource::Execute(CUnit &unit)
this->worker = &unit;
if (unit.Wait) {
// FIXME: show idle animation while we wait?
if (!unit.Waiting) {
unit.Waiting = 1;
unit.WaitBackup = unit.Anim;
}
UnitShowAnimation(unit, unit.Type->Animations->Still);
unit.Wait--;
return;
}
if (unit.Waiting) {
unit.Anim = unit.WaitBackup;
unit.Waiting = 0;
}
// Let's start mining.
if (this->State == SUB_START_RESOURCE) {

View file

@ -298,8 +298,17 @@ bool COrder_SpellCast::SpellMoveToTarget(CUnit &unit)
COrder_SpellCast &order = *this;
if (unit.Wait) {
if (!unit.Waiting) {
unit.Waiting = 1;
unit.WaitBackup = unit.Anim;
}
UnitShowAnimation(unit, unit.Type->Animations->Still);
unit.Wait--;
return ;
return;
}
if (unit.Waiting) {
unit.Anim = unit.WaitBackup;
unit.Waiting = 0;
}
const SpellType &spell = order.GetSpell();
switch (this->State) {

View file

@ -334,7 +334,7 @@ int UnitShowAnimationScaled(CUnit &unit, const CAnimation *anim, int scale)
// Changing animations
if (anim && unit.Anim.CurrAnim != anim) {
// Assert fails when transforming unit (upgrade-to).
Assert(!unit.Anim.Unbreakable);
Assert(!unit.Anim.Unbreakable && !unit.Waiting);
unit.Anim.Anim = unit.Anim.CurrAnim = anim;
unit.Anim.Wait = 0;
}
@ -423,6 +423,20 @@ static int GetAdvanceIndex(const CAnimation *base, const CAnimation *anim)
if (unit.Anim.Unbreakable) {
file.printf(" \"unbreakable\",");
}
file.printf("}, ");
// Wait backup info
file.printf("\"wait-anim-data\", {");
file.printf("\"anim-wait\", %d,", unit.WaitBackup.Wait);
for (int i = 0; i < NumAnimations; ++i) {
if (AnimationsArray[i] == unit.WaitBackup.CurrAnim) {
file.printf("\"curr-anim\", %d,", i);
file.printf("\"anim\", %d,", GetAdvanceIndex(unit.WaitBackup.CurrAnim, unit.WaitBackup.Anim));
break;
}
}
if (unit.WaitBackup.Unbreakable) {
file.printf(" \"unbreakable\",");
}
file.printf("}");
}
@ -464,6 +478,34 @@ static const CAnimation *Advance(const CAnimation *anim, int n)
}
}
/* static */ void CAnimations::LoadWaitUnitAnim(lua_State *l, CUnit &unit, int luaIndex)
{
if (!lua_istable(l, luaIndex)) {
LuaError(l, "incorrect argument");
}
const int nargs = lua_rawlen(l, luaIndex);
for (int j = 0; j != nargs; ++j) {
const char *value = LuaToString(l, luaIndex, j + 1);
++j;
if (!strcmp(value, "anim-wait")) {
unit.WaitBackup.Wait = LuaToNumber(l, luaIndex, j + 1);
} else if (!strcmp(value, "curr-anim")) {
const int animIndex = LuaToNumber(l, luaIndex, j + 1);
unit.WaitBackup.CurrAnim = AnimationsArray[animIndex];
} else if (!strcmp(value, "anim")) {
const int animIndex = LuaToNumber(l, luaIndex, j + 1);
unit.WaitBackup.Anim = Advance(unit.Anim.CurrAnim, animIndex);
} else if (!strcmp(value, "unbreakable")) {
unit.WaitBackup.Unbreakable = 1;
--j;
} else {
LuaError(l, "Unit anim-data: Unsupported tag: %s" _C_ value);
}
}
}
/**
** Add a label
*/

View file

@ -137,6 +137,7 @@ public:
static void SaveUnitAnim(CFile &file, const CUnit &unit);
static void LoadUnitAnim(lua_State *l, CUnit &unit, int luaIndex);
static void LoadWaitUnitAnim(lua_State *l, CUnit &unit, int luaIndex);
public:
CAnimation *Attack;

View file

@ -362,6 +362,7 @@ public:
unsigned CacheLock : 1; /// Unit is on lock by unitcache operations.
unsigned Summoned : 1; /// Unit is summoned using spells.
unsigned Waiting : 1; /// Unit is waiting and playing its still animation
unsigned TeamSelected; /// unit is selected by a team member.
CPlayer *RescuedFrom; /// The original owner of a rescued unit.
@ -397,7 +398,7 @@ unsigned ByPlayer : PlayerMax; /// Track unit seen by player
const CAnimation *CurrAnim; /// CurrAnim
int Wait; /// Wait
int Unbreakable; /// Unbreakable
} Anim;
} Anim, WaitBackup;
std::vector<COrder *> Orders; /// orders to process

View file

@ -380,6 +380,9 @@ static int CclUnit(lua_State *l)
} else if (!strcmp(value, "summoned")) {
unit->Summoned = 1;
--j;
} else if (!strcmp(value, "waiting")) {
unit->Summoned = 1;
--j;
} else if (!strcmp(value, "rescued-from")) {
unit->RescuedFrom = &Players[LuaToNumber(l, 2, j + 1)];
} else if (!strcmp(value, "seen-by-player")) {
@ -446,6 +449,10 @@ static int CclUnit(lua_State *l)
lua_rawgeti(l, 2, j + 1);
CAnimations::LoadUnitAnim(l, *unit, -1);
lua_pop(l, 1);
} else if (!strcmp(value, "wait-anim-data")) {
lua_rawgeti(l, 2, j + 1);
CAnimations::LoadWaitUnitAnim(l, *unit, -1);
lua_pop(l, 1);
} else if (!strcmp(value, "blink")) {
unit->Blink = LuaToNumber(l, 2, j + 1);
} else if (!strcmp(value, "moving")) {

View file

@ -456,7 +456,9 @@ void CUnit::Init()
ReCast = 0;
CacheLock = 0;
Summoned = 0;
Waiting = 0;
memset(&Anim, 0, sizeof(Anim));
memset(&WaitBackup, 0, sizeof(WaitBackup));
CurrentResource = 0;
Orders.clear();
delete SavedOrder;

View file

@ -180,6 +180,9 @@ void SaveUnit(const CUnit &unit, CFile &file)
if (unit.Summoned) {
file.printf(" \"summoned\",");
}
if (unit.Waiting) {
file.printf(" \"waiting\",");
}
if (unit.RescuedFrom) {
file.printf(" \"rescued-from\", %d,", unit.RescuedFrom->Index);
}