Add mobile touch UI and AVA2 biology system with server tools

This commit is contained in:
shen
2026-10-14 19:12:38 -07:00
parent e7f2202a94
commit 6472d17306
68 changed files with 4447 additions and 197 deletions
@@ -0,0 +1,103 @@
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <map>
#include <string>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include "questpc.h"
#include "item.h"
// PORT point numbers are intentionally not AVA2 protocol numbers; effects are compared by named point.
POINT_ENUMS
typedef unsigned char BYTE;
template<class T>void str_to_number(T& result,const char* value){result=std::atoi(value);}
static int roll=1;
static int get_global_time(){return 1000;}
static int number(int,int){return roll;}
enum { CHAT_TYPE_COMMAND=5,CHAT_TYPE_INFO=1,AFFECT_COLLECT=515,AFF_NONE=0,INFINITE_AFFECT_DURATION=1892160000 };
struct CAffect {unsigned point;int lApplyValue;};
struct Character {
int level=105,gold=1000000000;
bool blocked=false;
std::map<std::string,int> flags;
std::map<unsigned,int> items;
std::map<unsigned,CAffect> effects;
std::vector<std::string> replies;
unsigned GetPlayerID(){return 123;}
int GetLevel(){return level;}
int GetGold(){return gold;}
int GetQuestFlag(const std::string& s){auto i=flags.find(s);return i==flags.end()?0:i->second;}
void SetQuestFlag(const std::string& s,int n){if(n)flags[s]=n;else flags.erase(s);}
int CountSpecifyItem(unsigned id){auto i=items.find(id);return i==items.end()?0:i->second;}
void RemoveSpecifyItem(unsigned id,int n){assert(CountSpecifyItem(id)>=n);items[id]-=n;}
CAffect* FindAffect(int,unsigned point){auto i=effects.find(point);return i==effects.end()?nullptr:&i->second;}
void RemoveAffect(CAffect* a){effects.erase(a->point);}
void AddAffect(int,unsigned point,int n,int,int,int,bool){effects[point]={point,n};}
void PointChange(int,int n,bool){gold+=n;}
LPITEM AutoGiveItem(unsigned id,unsigned count,int,bool){items[id]+=count;static FakeItem item;return &item;}
bool CanHandleItem(){return !blocked;}
bool IsDead(){return false;}bool IsStun(){return false;}bool IsWarping(){return false;}
bool GetExchange(){return false;}bool GetShop(){return false;}bool GetMyShop(){return false;}
bool IsOpenSafebox(){return false;}bool IsCubeOpen(){return false;}
void ChatPacket(int,const char* format,...){char b[1024];va_list ap;va_start(ap,format);vsnprintf(b,sizeof(b),format,ap);va_end(ap);replies.push_back(b);}
std::string Snapshot()const{
std::string s=std::to_string(gold);
for(const auto& p:flags)s+="|"+p.first+":"+std::to_string(p.second);
for(const auto& p:items)s+="|item"+std::to_string(p.first)+":"+std::to_string(p.second);
for(const auto& p:effects)s+="|point"+std::to_string(p.first)+":"+std::to_string(p.second.lApplyValue);
return s;
}
};
typedef Character* LPCHARACTER;
#define ACMD(name) void name(Character* ch,const char* argument,int cmd,int subcmd)
#include "../biolog_remote.h"
#define split_argument BioSplitArgument
static unsigned pointToApply(unsigned value){return value;}
REFERENCE_COMMAND
int main(){
int cases=0;
for(unsigned stage=1;stage<bio_max;++stage) {
for(unsigned count:{0U,bio_data[stage][2]-1,bio_data[stage][2],bio_data[stage][2]+1})
for(int material:{0,2})for(int stone:{0,2})for(int cooldown:{0,2000})
for(int enhance:{0,1})for(int skip:{0,1})for(int random:{1,60,61,100}) {
Character expected,actual;expected.flags={{"bio.level",int(stage)},{"bio.count",int(count)},{"bio.time",cooldown}};
expected.items={{bio_data[stage][1],material},{bio_data[stage][5],stone},{71035,2},{30250,2}};
// Preserve accumulation across different stages sharing the same reward point.
expected.effects[bio_data[stage][6]]={bio_data[stage][6],17};actual=expected;roll=random;
std::string command=" mission "+std::to_string(enhance)+" "+std::to_string(skip);
reference_do_bio(&expected,command.c_str(),0,0);
do_port_biolog(&actual,command.c_str(),0,0);
assert(expected.Snapshot()==actual.Snapshot());++cases;
}
if(bio_data[stage][14])for(unsigned choice=0;choice<4;++choice)if(bio_data[stage][6+choice*2]) {
Character expected,actual;expected.flags={{"bio.level",int(stage)},{"bio.count",int(bio_data[stage][2]+1)}};
expected.effects[bio_data[stage][6+choice*2]]={bio_data[stage][6+choice*2],23};actual=expected;
std::string command=" gift "+std::to_string(choice);
reference_do_bio(&expected,command.c_str(),0,0);do_port_biolog(&actual,command.c_str(),0,0);
assert(expected.Snapshot()==actual.Snapshot());++cases;
std::string awarded=actual.Snapshot();do_port_biolog(&actual,command.c_str(),0,0);assert(actual.Snapshot()==awarded);
}
}
Character invalid;invalid.flags={{"bio.level",4},{"bio.count",21}};
for(const char* command:{"mission 256 0","mission -1 0","gift 3","gift -1","gift 256","gift 1 extra"}) {
std::string before=invalid.Snapshot();do_port_biolog(&invalid,command,0,0);assert(invalid.Snapshot()==before);
}
Character fresh;SendBioState(&fresh);assert(fresh.GetQuestFlag("bio.level")==1);
Character completed;completed.flags={{"bio.level",14},{"bio.bonus13",2}};
SendBioState(&completed);assert(completed.replies.back().find("biobonuses")==0 && completed.replies.back().back()=='2');
for(unsigned i=0;i<sizeof(kBioShop)/sizeof(kBioShop[0]);++i) {
Character buyer;auto offer=kBioShop[i];buyer.items[offer.cost]=offer.amount;
int gold=buyer.gold;assert(BioShopBuy(&buyer,i));assert(buyer.CountSpecifyItem(offer.vnum)==int(offer.count));
if(offer.cost)assert(buyer.CountSpecifyItem(offer.cost)==0);else assert(buyer.gold==gold-int(offer.gold));
}
// Reward reselection transfers only the selected stage's value and charges exactly one note.
Character reset;reset.flags={{"bio.level",14},{"bio.bonus9",1}};reset.items[70028]=2;
reset.effects[POINT_MAX_HP]={POINT_MAX_HP,2200};
BioResetBonus(&reset,9,1);assert(reset.GetQuestFlag("bio.bonus9")==2);
assert(reset.effects[POINT_MAX_HP].lApplyValue==1000);
assert(reset.effects[POINT_DEF_GRADE_BONUS].lApplyValue==120 && reset.CountSpecifyItem(70028)==1);
BioResetBonus(&reset,9,1);assert(reset.CountSpecifyItem(70028)==1);
std::printf("PASS: %d AVA2 differential scenarios; invalid requests, repeat rewards, 22 shop offers and reselection\n",cases);
}
+3
View File
@@ -0,0 +1,3 @@
#pragma once
struct FakeItem {};
typedef FakeItem* LPITEM;
+2
View File
@@ -0,0 +1,2 @@
#pragma once
struct ITEM_MANAGER { static ITEM_MANAGER& instance(){static ITEM_MANAGER m;return m;} template<class T>void FlushDelayedSave(T*){} };
+23
View File
@@ -0,0 +1,23 @@
#pragma once
// Small test double for the PORT command's interaction with the existing quest engine.
#include <map>
#include <string>
namespace quest {
struct PC {
std::map<std::string, int> flags;
bool running = false, loaded = true;
int GetFlag(const std::string& key) { return flags[key]; }
void SetFlag(const std::string& key, int value, bool = false) { flags[key] = value; }
bool IsRunning() const { return running; }
bool IsLoaded() const { return loaded; }
};
struct CQuestManager {
PC pc;
int dispatched = 0;
bool available = true;
static CQuestManager& instance() { static CQuestManager manager; return manager; }
PC* GetPCForce(unsigned) { return &pc; }
unsigned GetQuestIndexByName(const std::string&) { return available ? 1 : 0; }
void QuestButton(unsigned, unsigned) { ++dispatched; pc.running = true; }
};
}
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
import importlib.util,json,re,subprocess,sys,tempfile
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
reference=Path(sys.argv[1]);template=ROOT/'tests/ava2_core_test.cpp'
# Compare the generated 13-stage table with both upstream server and Python UI tables.
spec=importlib.util.spec_from_file_location('import_ava2',ROOT/'import_ava2.py');module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
rows=module.extract(reference);catalog=json.loads((ROOT/'catalog.json').read_text())
assert len(rows)==14 and len(catalog)==13
for row,c in zip(rows[1:],catalog):
assert row[:6]==[c['level'],c['material'],c['count'],c['cooldown'],c['chance'],c['stone']]
ui=ROOT.parents[2]/'src/platform/ScriptLib/BiologScript.inc';source=ui.read_text();data=re.search(r'^CATALOG = (.*)$',source,re.M).group(1)
import ast
assert ast.literal_eval(data)=={c['index']:c for c in catalog}
points=sorted({v for row in rows for v in row if isinstance(v,str)})
raw=(reference/'cmd_general.cpp').read_bytes().decode('latin1').replace('\r','');start=raw.index('ACMD(do_bio)\n');end=raw.index('\n#endif',start)
original=raw[start:end].replace('ACMD(do_bio)','static ACMD(reference_do_bio)')
code=template.read_text().replace('POINT_ENUMS','enum {'+', '.join([points[0]+'=1']+points[1:]+['POINT_GOLD'])+'};').replace('REFERENCE_COMMAND',original)
with tempfile.TemporaryDirectory(prefix='biolog-ava2-') as folder:
folder=Path(folder);cpp=folder/'test.cpp';cpp.write_text(code)
# Keep relative production include paths resolvable when compiling the generated source.
cpp.write_text(cpp.read_text().replace('#include "../biolog_remote.h"','#include "'+str(ROOT/'biolog_remote.h')+'"'))
subprocess.run(['c++','-std=c++17','-I'+str(ROOT/'tests'),str(cpp),'-o',str(folder/'test')],check=True)
subprocess.run([str(folder/'test')],check=True)
print('PASS: 13-stage catalog and embedded client table match extracted AVA2 server constants')
+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""Compile the actual patched CalcAttBonus and assert the biology bonuses in real arithmetic."""
from pathlib import Path
import re,subprocess,sys,tempfile
source=Path(sys.argv[1]).read_bytes().decode('latin1');start=source.index('int CalcAttBonus(');end=source.index('\nvoid Item_GetDamage',start);body=source[start:end]
assert 'PORT AVA2 biology combat' in body
names=sorted(set(re.findall(r'\b(?:POINT_|RACE_FLAG_|JOB_|UNIQUE_ITEM_)[A-Z_0-9]+',body)))
code=r'''
#include <map>
#include <set>
#include <cassert>
#include <cstdio>
NAMES
enum {MOB_RANK_BOSS=4,AFFECT_COLLECT=515};
struct CAffect {int lApplyValue;};
struct Character {
bool pc=true,stone=false;int rank=1,job=-1;
std::map<int,int> points;std::set<int> races;CAffect research{0};
bool IsPC(){return pc;}bool IsNPC(){return !pc;}bool IsStone(){return stone;}
int GetPoint(int n){return points[n];}int GetJob(){return job;}int GetMobRank(){return rank;}
int GetMarriageBonus(int){return 0;}bool IsRaceFlag(int n){return races.count(n);}
CAffect* FindAffect(int,int){return research.lApplyValue?&research:nullptr;}
};
struct CHARACTER_MANAGER {
static CHARACTER_MANAGER& instance(){static CHARACTER_MANAGER m;return m;}
int GetMobDamageRate(Character*){return 100;}
};
typedef Character* LPCHARACTER;
BODY
int main(){
Character attacker,victim;victim.pc=false;victim.stone=true;
attacker.points[POINT_ATTBONUS_STONE]=10;
assert(CalcAttBonus(&attacker,&victim,1000)==1100);
victim.stone=false;victim.rank=MOB_RANK_BOSS;attacker.points.clear();attacker.points[POINT_ATTBONUS_BOSS]=6;
assert(CalcAttBonus(&attacker,&victim,1000)==1060);
// AVA2's research monster bonus excludes bosses; unrelated existing equipment still applies.
attacker.points.clear();attacker.points[POINT_ATTBONUS_MONSTER]=15;attacker.research.lApplyValue=5;
assert(CalcAttBonus(&attacker,&victim,1000)==1100);
victim.rank=1;assert(CalcAttBonus(&attacker,&victim,1000)==1150);
attacker.points.clear();attacker.research.lApplyValue=0;victim.pc=true;victim.points[POINT_RESIST_HUMAN]=10;
assert(CalcAttBonus(&attacker,&victim,1000)==900);
attacker.pc=false;attacker.races.insert(RACE_FLAG_HUMAN);
assert(CalcAttBonus(&attacker,&victim,1000)==970);
attacker.races.clear();assert(CalcAttBonus(&attacker,&victim,1000)==1000);
std::puts("PASS: patched combat applies stone/boss bonuses, excludes research monster bonus on bosses, and applies PvP/NPC human resistance");
}
'''
code=code.replace('NAMES','enum {'+', '.join([names[0]+'=1']+names[1:])+'};').replace('BODY',body)
with tempfile.TemporaryDirectory(prefix='bio-combat-') as folder:
folder=Path(folder);(folder/'test.cpp').write_text(code)
subprocess.run(['c++','-std=c++17',str(folder/'test.cpp'),'-o',str(folder/'test')],check=True)
subprocess.run([str(folder/'test')],check=True)