Files
AVA2/src/scripting/python_switchbot.cpp
T

76 lines
4.8 KiB
C++

// Direct port of AVA2 PythonSwitchbot.cpp. Start/Stop send requests only; server
// UPDATE controls active/finished. The exposed five slots sit over six native slots.
#include "switchbot_state.h"
#include "python_native_modules.h"
#include "python_gameplay_modules.h"
#include "eter_net/PythonNetworkStream.h"
#include "shared/common/PythonUtils.h"
#include <cstring>
namespace ava2 {
SwitchbotState& GetSwitchbotState() {static SwitchbotState state;return state;}
int SwitchbotState::AttributeSet(uint8_t slot) const {
if(slot>=SWITCHBOT_SLOT_COUNT)return -1;
int type=0,sub=0;if(!GetItemTypeAndSubType(GetPlayerItemData(11,slot).vnum,&type,&sub))return -1;
// ItemData.h: weapon=1, arrow=6, armor=2, costume=28, rings=36.
if(type==1)return sub==6?-1:ATTRIBUTE_SET_WEAPON;
if(type==36)return ATTRIBUTE_SET_NEW_RINGS;
if(type==2 || type==28) {
switch(sub) {
case 0:return ATTRIBUTE_SET_BODY;case 1:return ATTRIBUTE_SET_HEAD;case 2:return ATTRIBUTE_SET_SHIELD;
case 3:return ATTRIBUTE_SET_WRIST;case 4:return ATTRIBUTE_SET_FOOTS;case 5:return ATTRIBUTE_SET_NECK;
case 6:return ATTRIBUTE_SET_EAR;case 8:return ATTRIBUTE_SET_PENDANT;
}
}
return -1;
}
void SwitchbotState::ClearSlot(uint8_t slot) {
if(slot>=SWITCHBOT_SLOT_COUNT)return;
table.active[slot]=0;table.items[slot]=0;
std::memset(table.alternatives[slot],0,sizeof(table.alternatives[slot]));
// Original ClearSlot does not reset finished or the player's item data.
}
namespace {
PyObject* active(PyObject*,PyObject* args) {uint8_t slot;if(!PyTuple_GetByte(args,0,&slot))return Py_BuildException();return PyInt_FromLong(slot<6 && GetSwitchbotState().table.active[slot]!=0);}
PyObject* finished(PyObject*,PyObject* args) {uint8_t slot;if(!PyTuple_GetByte(args,0,&slot))return Py_BuildException();return PyInt_FromLong(slot<6 && GetSwitchbotState().table.finished[slot]!=0);}
PyObject* get(PyObject*,PyObject* args) {
uint8_t slot,alt,i;if(!PyTuple_GetByte(args,0,&slot)||!PyTuple_GetByte(args,1,&alt)||!PyTuple_GetByte(args,2,&i))return Py_BuildException();
if(slot>=6 || alt>=2 || i>=5)return Py_BuildValue("ii",0,0);
const auto a=GetSwitchbotState().table.alternatives[slot][alt].attributes[i];return Py_BuildValue("ii",int(a.bType),int(a.sValue));
}
PyObject* set(PyObject*,PyObject* args) {
uint8_t slot,alt,i,type;int value;
if(!PyTuple_GetByte(args,0,&slot)||!PyTuple_GetByte(args,1,&alt)||!PyTuple_GetByte(args,2,&i)||!PyTuple_GetByte(args,3,&type)||!PyTuple_GetInteger(args,4,&value))return Py_BuildException();
if(slot<6 && alt<2 && i<5)GetSwitchbotState().table.alternatives[slot][alt].attributes[i]={type,int16_t(value)};
Py_RETURN_NONE;
}
PyObject* clear(PyObject*,PyObject* args) {uint8_t slot;if(!PyTuple_GetByte(args,0,&slot))return Py_BuildException();GetSwitchbotState().ClearSlot(slot);Py_RETURN_NONE;}
PyObject* attributes(PyObject*,PyObject* args) {
uint8_t slot;if(!PyTuple_GetByte(args,0,&slot))return Py_BuildException();auto* list=PyList_New(0);
const auto& state=GetSwitchbotState();int set=state.AttributeSet(slot);if(set<0)return list;
auto it=state.attributes.find(uint8_t(set));if(it==state.attributes.end())return list;
for(const auto& [type,value]:it->second) {auto* pair=Py_BuildValue("(ii)",int(type),int(int16_t(value)));PyList_Append(list,pair);Py_DECREF(pair);}
return list;
}
PyObject* max_value(PyObject*,PyObject* args) {
uint8_t slot,type;if(!PyTuple_GetByte(args,0,&slot)||!PyTuple_GetByte(args,1,&type))return Py_BuildException();
const auto& state=GetSwitchbotState();int set=state.AttributeSet(slot);if(set<0)return PyInt_FromLong(0);
auto it=state.attributes.find(uint8_t(set));if(it==state.attributes.end())return PyInt_FromLong(0);
auto found=it->second.find(type);return PyInt_FromLong(found==it->second.end()?0:found->second);
}
template<bool Start> PyObject* request(PyObject*,PyObject* args) {
uint8_t slot;if(!PyTuple_GetByte(args,0,&slot))return Py_BuildException();
// Original wrapper uses > rather than >=; slot6 sends a header without alternatives.
if(slot<=6)PythonNetworkStream::Instance().SendSwitchbotPacket(Start,slot);
Py_RETURN_NONE;
}
}
void initSwitchbot() {
static PyMethodDef methods[]={
{"IsActive",active,METH_VARARGS,nullptr},{"IsFinished",finished,METH_VARARGS,nullptr},
{"GetAttribute",get,METH_VARARGS,nullptr},{"SetAttribute",set,METH_VARARGS,nullptr},
{"GetAttributesForSet",attributes,METH_VARARGS,nullptr},{"GetAttributeMaxValue",max_value,METH_VARARGS,nullptr},
{"ClearSlot",clear,METH_VARARGS,nullptr},{"Start",request<true>,METH_VARARGS,nullptr},{"Stop",request<false>,METH_VARARGS,nullptr},{nullptr,nullptr,0,nullptr}};
auto* m=Py_InitModule("switchbot",methods);PyModule_AddIntConstant(m,"SLOT_COUNT",SWITCHBOT_SLOT_COUNT_REAL);PyModule_AddIntConstant(m,"ALTERNATIVE_COUNT",SWITCHBOT_ALTERNATIVE_COUNT);
}
}