| 978 | } |
| 979 | |
| 980 | AIUnit* AIUnit::CreateObject(NetworkMessageContext& ctx) |
| 981 | { |
| 982 | AI_ERROR(dynamic_cast<const IndicesCreateAIUnit*>(ctx.GetIndices())) |
| 983 | const IndicesCreateAIUnit* indices = static_cast<const IndicesCreateAIUnit*>(ctx.GetIndices()); |
| 984 | |
| 985 | Person* person; |
| 986 | if (ctx.IdxTransferRef(indices->person, person) != TMOK) |
| 987 | { |
| 988 | return nullptr; |
| 989 | } |
| 990 | if (!person) |
| 991 | { |
| 992 | return nullptr; |
| 993 | } |
| 994 | AISubgroup* subgrp; |
| 995 | if (ctx.IdxTransferRef(indices->subgroup, subgrp) != TMOK) |
| 996 | { |
| 997 | return nullptr; |
| 998 | } |
| 999 | if (!subgrp) |
| 1000 | { |
| 1001 | return nullptr; |
| 1002 | } |
| 1003 | AIGroup* grp = subgrp->GetGroup(); |
| 1004 | if (!grp) |
| 1005 | { |
| 1006 | return nullptr; |
| 1007 | } |
| 1008 | int id; |
| 1009 | if (ctx.IdxTransfer(indices->id, id) != TMOK) |
| 1010 | { |
| 1011 | return nullptr; |
| 1012 | } |
| 1013 | AIUnit* unit = person->Brain(); |
| 1014 | unit->_id = id; |
| 1015 | grp->SetUnit(id - 1, unit); |
| 1016 | subgrp->AddUnit(unit); |
| 1017 | |
| 1018 | // temporary select leader (avoid diag output) |
| 1019 | if (!grp->Leader()) |
| 1020 | { |
| 1021 | grp->GetCenter()->SelectLeader(grp); |
| 1022 | } |
| 1023 | if (!subgrp->Leader()) |
| 1024 | { |
| 1025 | subgrp->SelectLeader(); |
| 1026 | } |
| 1027 | |
| 1028 | NetworkId objectId; |
| 1029 | if (ctx.IdxTransfer(indices->objectCreator, objectId.creator) != TMOK) |
| 1030 | { |
| 1031 | return nullptr; |
| 1032 | } |
| 1033 | if (ctx.IdxTransfer(indices->objectId, objectId.id) != TMOK) |
| 1034 | { |
| 1035 | return nullptr; |
| 1036 | } |
| 1037 | unit->SetNetworkId(objectId); |
no test coverage detected