proto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /** @file */
  2. /*
  3. Function/method modifiers:
  4. proto - prototyping of internal function (C++ side)
  5. native - native call convention of internal function (C++ side)
  6. volatile - internal function that may call back to script (hint for
  7. compiler that context need to be saved on stack)
  8. private - function may not be called from script
  9. event - hint for tools that the function should be exposed as
  10. Entity script event.
  11. Variable modifiers:
  12. owned - modifier for returing internal functions. Tells to script-VM,
  13. that returning variable (string or array) must not be released
  14. out - modifier for function parameters. It tells that variable will
  15. be changed by function call (used mainly by internal functions)
  16. inout - modifier for function parameters. It tells that variable will
  17. be used and then changed by function call (used mainly by internal functions)
  18. const - constants. May not be modified.
  19. reference - hint for tools (Material editor), that the variable may be used
  20. as parameter in material
  21. */
  22. /*===================================================================*/
  23. /* Enforce engine API */
  24. /*===================================================================*/
  25. //placeholder
  26. class AnimEvent
  27. {
  28. int type;
  29. int slot;
  30. };
  31. class SoundEvent
  32. {
  33. int type;
  34. int handle;
  35. };
  36. typedef int[] vobject;
  37. class vobject
  38. {
  39. proto native IEntitySource ToEntitySource();
  40. }
  41. #ifdef ENF_DONE
  42. //------------------------------------------
  43. // SOUND API
  44. //------------------------------------------
  45. //TODO:
  46. typedef int[] HSOUND;
  47. //flags
  48. //!Play once and invoke EntityEvent.SOUNDEND event
  49. //SFX_ONCE
  50. //!Music. Has separate volume control, can be replaced by custom music
  51. //SFX_MUSIC
  52. //!Ambient, not positional sound
  53. //SFX_AMBIENT
  54. //!Positional sound
  55. //SFX_3D
  56. //!Don't even start if not hearable. Valid only together with SFX_3D!
  57. //SFX_DISCARDABLE
  58. //!Sound is positional. XSoundSource::GetSoundOrientation is called. Valid only together with SFX_3D!
  59. //SFX_DIRECTIONAL
  60. ////!Sound is moveable. XSoundSource::GetSoundVelocity is called. Valid only together with SFX_3D!
  61. //SFX_DOPPLER
  62. //!Position is not changing, XSoundSource::GetSoundPosition/GetSoundOrientation is called only once upon start! Valid only together with SFX_3D!
  63. //SFX_STATIC
  64. //!Don't test hearability. Valid only together with SFX_3D!
  65. //SFX_NOTEST
  66. //proto volatile HSOUND PlaySound(int soundScene, IEntity source, string sshader, int flags);
  67. proto volatile native void EndSound(HSOUND snd);
  68. //volume 0...1. Logaritmic scale
  69. proto native int SetSoundVolume(HSOUND sound, float volume)
  70. proto native int SetSoundFrequency(HSOUND sound, int freq)
  71. //returns 0, if soundid is not valid
  72. proto native int GetSoundLength(HSOUND sound)
  73. //returns -1, if soundid is not valid
  74. proto native int GetSoundPosition(HSOUND sound)
  75. //defaultne se pouziva EAX prostredi nadefinovane v mape, ale lze ho prebit touto fci.
  76. // Mohou se michat dve ruzna prostredi v pomeru danem hodnotou fade (fade==0 -> 100% env1, fade==1 -> 100% env2).
  77. //pokud chceme michat aktualni prostredi s nejakym jinym, lze pouzit zastupny nazev "$current".
  78. //Tim lze dosahnout nafadovani vlastniho prostredi, kdyz fci postupne volame napr. takto:
  79. //SetEAXEnvironment("$current", "Drugged", fade)
  80. //pricemz hodnota fade postupne narusta od nuly do jedne
  81. //proto native bool SetEAXEnvironment(string env1, string env2, float fade)
  82. #endif
  83. class PacketOutputAdapter
  84. {
  85. proto native void WriteBool(bool value); //size: 1 byte
  86. proto native void WriteInt(int value); //size: 4 bytes
  87. proto native void WriteFloat(float value); //size: 4 bytes
  88. proto native void WriteString(string value); //size: n bytes (string length)
  89. proto native void WriteVector(vector value); //size: 12 bytes
  90. proto native void WriteMatrixAsQuaternionVector(vector mat[4]); //size: 28 bytes
  91. proto native void WriteIntAsByte(int value); //write int in range <-128, 127> size: 1 byte
  92. proto native void WriteIntAsUByte(int value); //write int in range <0, 255> size: 1 byte
  93. proto native void WriteIntAsHalf(int value); //write int in range <-32768, 32768> size: 2 bytes
  94. proto native void WriteIntAsUHalf(int value); //write int in range <0, 65535> size: 2 bytes
  95. proto native void WriteFloatAsByte(float value, float min, float max); // size: 1 byte
  96. proto native void WriteFloatAsHalf(float value, float min, float max); // size: 2 bytes
  97. };
  98. class PacketInputAdapter
  99. {
  100. proto native bool ReadBool();
  101. proto native int ReadInt();
  102. proto native float ReadFloat();
  103. proto string ReadString();
  104. proto native vector ReadVector();
  105. proto void ReadMatrixAsQuaternionVector(vector mat[4]);
  106. proto native int ReadIntAsByte();
  107. proto native int ReadIntAsUByte();
  108. proto native int ReadIntAsHalf();
  109. proto native int ReadIntAsUHalf();
  110. proto native float ReadFloatAsByte(float min, float max);
  111. proto native float ReadFloatAsHalf(float min, float max);
  112. };
  113. /*!
  114. makes screenshot and stores it in to a DDS format file
  115. if the name begins with '$' the screenshot in stored in the fullpath according to the name parameter
  116. otherwise the screenshot is stored in "$profile:ScreenShotes/DATE TIME-NAME.dds" where DATE, TIME AND NAME are replaced by actual values
  117. \param name name of the screenshot
  118. */
  119. proto native void MakeScreenshot(string name);
  120. /*!
  121. Returns actual fps (average in last 10 frames)
  122. */
  123. proto native int GetFPS();
  124. //----------------------------------------------
  125. /** \name SkyDome API */
  126. //@{
  127. /*!
  128. load all sky presets from xml file
  129. \param presetFile name of XML file with presets
  130. */
  131. proto native int LoadSkyPresets(string presetsFile);
  132. /*!
  133. initialize sky with preset, must be called to set the global planets setting
  134. \param presetName name of preset to be set (one of presets from file loaded using LoadSkyPresets)
  135. */
  136. proto native int InitSky(string presetName);
  137. /*!
  138. set sky preset to day time and stormy parameter
  139. \param presetName name of preset to be set (one of presets from file loaded using LoadSkyPresets)
  140. \param stormy stormy parameter <0, 1>
  141. \param dayTime day of time <0, 1>
  142. */
  143. proto native int SetSkyPreset(string presetName, float stormy, float dayTime);
  144. /*!
  145. lerp two sky presets and set the result using day time
  146. \param presetName1 name of preset1 to be set (one of presets from file loaded using LoadSkyPresets)
  147. \param presetName2 name of preset2 to be set (one of presets from file loaded using LoadSkyPresets)
  148. \param dayTime day of time <0, 1>
  149. \param stormy1 stormy parameter for preset1 <0, 1>
  150. \param stormy2 stormy parameter for preset2 <0, 1>
  151. \param lerpVal lerp value between two presets
  152. */
  153. proto native int LerpSkyPreset(string presetName1, string presetName2, float dayTime, float stormy1, float stormy2, float lerpVal);
  154. /*!
  155. lerp three sky presets using barycentric coordinates and set the result using day time
  156. \param presetName1 name of preset1 to be set (one of presets from file loaded using LoadSkyPresets)
  157. \param presetName2 name of preset2 to be set (one of presets from file loaded using LoadSkyPresets)
  158. \param presetName2 name of preset3 to be set (one of presets from file loaded using LoadSkyPresets)
  159. \param dayTime day of time <0, 1>
  160. \param stormy1 stormy parameter for preset1 <0, 1>
  161. \param stormy2 stormy parameter for preset2 <0, 1>
  162. \param stormy3 stormy parameter for preset3 <0, 1>
  163. \param w1 weight for preset1
  164. \param w2 weight for preset2
  165. \param w3 weight for preset3
  166. */
  167. proto native int LerpSkyPreset3(string presetName1, string presetName2, string presetName3, float dayTime, float stormy1, float stormy2, float stormy3, float w1, float w2, float w3);
  168. /*!
  169. user can set planets to its own positions on skydome using SetSkyPresetPlanet function,
  170. if disabled, the planets are positioned automatically using sky preset setting
  171. \param enabled enable/disable
  172. */
  173. proto native void SetSkyUserPlanets(bool enabled);
  174. /*!
  175. set planet position
  176. \param index index of planet, 0 = sun usually
  177. \param azimuthDeg azimuth position in degrees
  178. \param zenithDeg zenith position in degrees
  179. */
  180. proto native bool SetSkyPlanet(int index, float azimuthDeg, float zenithDeg);
  181. /*!
  182. set planet size in degrees
  183. \param index index of planet, 0 = sun usually
  184. \param angleDeg angle size in degrees
  185. */
  186. proto native bool SetSkyPlanetSize(int index, float angleDeg);
  187. /*!
  188. set UTC time for real time stars
  189. \param year
  190. \param month
  191. \param day
  192. \param hour
  193. \param minute
  194. \param sec
  195. \param offsetSec to take into account time difference between Greenwich and local time (position dependency, summer/winter time)
  196. */
  197. proto native void SetStarsObserverTime(int year, int month, int day, int hour, int minute, float sec, int offsetSec);
  198. /*!
  199. set observer position
  200. \param latitudeDeg latitude in degrees
  201. \param longitudeDeg longitude in degrees
  202. */
  203. proto native void SetStarsObserverPosition(float latitudeDeg, float longitudeDeg);
  204. /*!
  205. update of stars
  206. \param update true = automatic stars update, false = update is on user by SetStarsRotMatrix
  207. */
  208. proto native void SetRealStarAutoUpdate(bool update);
  209. /*!
  210. night sky layer rotation matrix
  211. \param mat rotation matrix
  212. */
  213. proto native void SetNightLayerRotMatrix(vector mat[3]);
  214. /*!
  215. stars rotation matrix, is different from night rotation matrix
  216. because we don't know the time when night layers was captured
  217. \param mat rotation matrix
  218. */
  219. proto native void SetStarsRotMatrix(vector mat[3]);
  220. //@}
  221. //----------------------------------------------
  222. /** \name Materials API */
  223. //@{
  224. typedef int[] Material;
  225. class Material
  226. {
  227. /*!
  228. set parametr of material by string name
  229. \param paramName name of parameter
  230. \param value value
  231. */
  232. proto bool SetParam(string propertyName, void value);
  233. /*!
  234. reset parametr of material to default value
  235. \param paramName name of parameter
  236. */
  237. proto native void ResetParam(string propertyName);
  238. /*!
  239. set parametr index for faster access to material properties
  240. \param paramName name of parameter
  241. \return parameter index
  242. */
  243. proto native int GetParamIndex(string paramName);
  244. /*!
  245. set parametr of material by index
  246. \param paramName name of parameter
  247. \param value value
  248. */
  249. proto void SetParamByIndex(int paramIndex, void value);
  250. };
  251. //@}
  252. int VectortoRGBA( vector vec, float h)
  253. {
  254. float x,y,z;
  255. int r,g,b,a,rgba;
  256. x = vec[0];
  257. y = vec[1];
  258. z = vec[2];
  259. x = x * 127.0 + 128.0;
  260. y = y * 127.0 + 128.0;
  261. z = z * 127.0 + 128.0;
  262. h = h * 255.0;
  263. a = (int)h << 24;
  264. r = (int)x << 16;
  265. g = (int)y << 8;
  266. b = z;
  267. return r | g | b | a;
  268. }
  269. //-----------------------------------------------------------------
  270. int ARGB(int a, int r, int g, int b)
  271. {
  272. a = a << 24;
  273. r = r << 16;
  274. g = g << 8;
  275. return a | r | g | b;
  276. }
  277. //-----------------------------------------------------------------
  278. //! Converts <0.0, 1.0> ARGB into color
  279. int ARGBF(float fa, float fr, float fg, float fb)
  280. {
  281. return ARGB((float)(fa * 255.0), (float)(fr * 255.0), (float)(fg * 255.0), (float)(fb * 255.0));
  282. }
  283. //-----------------------------------------------------------------
  284. int AWHITE(int a)
  285. {
  286. return a << 24 | 0xffffff;
  287. }
  288. //-----------------------------------------------------------------
  289. int LerpARGB(int c1, int c2)
  290. {
  291. int cb1, cb2;
  292. const int cmask = 0x00ff00ff;
  293. cb1 = c1 >> 8 & cmask;
  294. cb2 = c2 >> 8 & cmask;
  295. cb1 = cb1 + cb2 >> 1;
  296. c1 = c1 & cmask;
  297. c2 = c2 & cmask;
  298. c1 = c1 + c2 >> 1;
  299. return cb1 << 8 | c1;
  300. }
  301. //-------------------------------------------------------------------------
  302. class Link<Class T>
  303. {
  304. proto private native void Init(T init);
  305. proto private native Object Get();
  306. void Release()
  307. {
  308. T obj = Get();
  309. if(obj)
  310. obj.Release();
  311. }
  312. void Link(T init)
  313. {
  314. Init(init);
  315. }
  316. T Ptr()
  317. {
  318. return Get();
  319. }
  320. bool IsNull()
  321. {
  322. if(!Get())
  323. return true;
  324. return false;
  325. }
  326. };